feat: cleaned up remaining unwraps, and then clippy+fmt

This commit is contained in:
silver 2025-06-16 06:14:53 +01:00
parent 652dd6ff1c
commit 9134feee4e
Signed by untrusted user: silver
GPG key ID: 36F93D61BAD3FD7D
3 changed files with 105 additions and 33 deletions

View file

@ -43,8 +43,16 @@ pub mod get_config_icons {
}
pub fn minimal() -> ConfigTomlLocal {
let toml_raw_min = include_str!("../../.server-icons.toml");
let config_min: ConfigTomlLocal = toml::from_str(toml_raw_min).unwrap();
config_min
toml::from_str::<ConfigTomlLocal>(toml_raw_min).unwrap_or_else(|e| {
dbg!(e);
ConfigTomlLocal {
source: ConfigTomlSource {
repo: "".to_string(),
directory: "".to_string(),
file: "".to_string(),
},
}
})
}
// since a copy of the festival file is in the repo we just need to get to it
@ -52,12 +60,21 @@ pub mod get_config_icons {
let config_source = minimal();
let file_path = format!("{}/open-governance/{}/{}", &config.home, &config_source.source.directory, &config_source.source.file);
let contents = fs::read_to_string(file_path).expect("Should have been able to read the file");
let config_festivals: ConfigTomlRemote = toml::from_str(&contents).unwrap();
let contents = fs::read_to_string(file_path).unwrap_or_else(|e| {
dbg!(e);
"".to_string()
});
let festivals = match toml::from_str::<ConfigTomlRemote>(&contents) {
Ok(config_festivals) => config_festivals.festivals,
Err(e) => {
dbg!(e);
vec![]
}
};
ConfigToml {
source: config_source.source,
festivals: config_festivals.festivals,
festivals,
}
}
}
@ -154,15 +171,26 @@ pub mod update_icon {
let url = &config_toml.source.repo;
let folder = format!("{}/open-governance", &config.home);
Command::new("git").arg("clone").arg(url).arg(&folder).output().expect("failed to execute process");
if let Err(e) = Command::new("git")
// clone the repo, gracefully "fails"
.arg("clone")
.arg(url)
.arg(&folder)
.output()
{
dbg!(e);
}
Command::new("git")
if let Err(e) = Command::new("git")
// Update the repo
.arg("pull")
.arg("origin")
.arg("main")
.current_dir(&folder)
.output()
.expect("failed to execute process");
{
dbg!(e);
}
}
fn get_logos(config: &Config, config_toml: &ConfigToml) -> Vec<LogoData> {
@ -170,7 +198,13 @@ pub mod update_icon {
let folder_path = PathBuf::from(&folder);
let mut folder_output = folder_path.clone();
folder_output.push("converted");
let paths = fs::read_dir(folder).unwrap();
let paths = match fs::read_dir(folder) {
Ok(x) => x,
Err(e) => {
dbg!(e);
return vec![];
}
};
let args = Args {
input: folder_path.clone(),
@ -179,14 +213,26 @@ pub mod update_icon {
width: 1024,
height: 1024,
};
let mut r = Renderer::new(&args).unwrap();
let mut r = match Renderer::new(&args) {
Ok(x) => x,
Err(e) => {
let _ = dbg!(e);
return vec![];
}
};
let mut logos = vec![];
for tmp in paths.flatten() {
let path_local = tmp.path().to_owned();
let path_local2 = tmp.path().to_owned();
let name = path_local2.file_name().unwrap().to_owned();
let name = match path_local2.file_name() {
None => {
dbg!(path_local2);
continue;
}
Some(x) => x.to_owned(),
};
let mut path = tmp.path();
if path.is_dir() {
@ -200,7 +246,13 @@ pub mod update_icon {
let mut path_new = path_local.clone();
path_new.set_extension("png");
let filename_tmp = path_new.clone();
let filename = filename_tmp.file_name().unwrap_or_default();
let filename = match filename_tmp.file_name() {
None => {
dbg!(filename_tmp);
continue;
}
Some(x) => x,
};
path_new.pop();
path_new.push("converted");
path_new.push(filename);
@ -266,18 +318,36 @@ pub mod update_icon {
async fn logo_set(ctx: &Context, db: &Pool<Sqlite>, server: &GuildId, logo_selected: &LogoData) {
// add to teh database
logo_set_db(db, logo_selected).await;
if !logo_set_db(db, logo_selected).await {
// something went wrong
return;
}
let icon = CreateAttachment::path(logo_selected.path.to_str().unwrap_or_default()).await.unwrap();
// assuming a `guild` has already been bound
let builder = EditGuild::new().icon(Some(&icon));
server.edit(ctx, builder).await.unwrap();
if let Some(logo_path) = logo_selected.path.to_str() {
match CreateAttachment::path(logo_path).await {
Ok(icon) => {
// assuming a `guild` has already been bound
let builder = EditGuild::new().icon(Some(&icon));
if let Err(e) = server.edit(ctx, builder).await {
dbg!(e);
}
}
Err(e) => {
dbg!(e);
}
}
}
}
async fn logo_set_db(db: &Pool<Sqlite>, logo_selected: &LogoData) {
let name = logo_selected.name.to_str().unwrap_or_default();
let path = logo_selected.path.to_str().unwrap_or_default();
async fn logo_set_db(db: &Pool<Sqlite>, logo_selected: &LogoData) -> bool {
let name = match logo_selected.name.to_str() {
None => return false,
Some(x) => x,
};
let path = match logo_selected.path.to_str() {
None => return false,
Some(x) => x,
};
match sqlx::query_as::<_, ServerIcons>(
"
@ -294,7 +364,9 @@ pub mod update_icon {
Ok(_) => {}
Err(e) => {
dbg!(e);
return false;
}
}
true
}
}