fix: remove except from config loading and fall to default

This commit is contained in:
MeexReay 2025-07-05 18:26:58 +03:00
parent 2aff95cddf
commit 3fc9bd7bca
2 changed files with 12 additions and 13 deletions

View File

@ -1,7 +1,7 @@
use clap::Parser; use clap::Parser;
use serde_default::DefaultFromSerde; use serde_default::DefaultFromSerde;
use serde_yml; use serde_yml;
use std::{fs, path::PathBuf}; use std::{fs, path::PathBuf, error::Error};
const MESSAGE_FORMAT: &str = "\u{B9AC}\u{3E70}<{name}> {text}"; const MESSAGE_FORMAT: &str = "\u{B9AC}\u{3E70}<{name}> {text}";
@ -89,23 +89,22 @@ pub fn get_config_path() -> PathBuf {
.join("config.yml") .join("config.yml")
} }
pub fn load_config(path: PathBuf) -> Config { pub fn load_config(path: PathBuf) -> Result<Config, Box<dyn Error>> {
if !fs::exists(&path).unwrap_or_default() { if !fs::exists(&path).unwrap_or_default() {
let config = Config::default(); let config = Config::default();
let config_text = serde_yml::to_string(&config).expect("Config save error"); save_config(path, &config)?;
fs::create_dir_all(&path.parent().expect("Config save error")).expect("Config save error"); Ok(config)
fs::write(&path, config_text).expect("Config save error");
config
} else { } else {
let config = &fs::read_to_string(&path).expect("Config load error"); let config = &fs::read_to_string(&path)?;
serde_yml::from_str(config).expect("Config load error") Ok(serde_yml::from_str(config)?)
} }
} }
pub fn save_config(path: PathBuf, config: &Config) { pub fn save_config(path: PathBuf, config: &Config) -> Result<(), Box<dyn Error>> {
let config_text = serde_yml::to_string(config).expect("Config save error"); let config_text = serde_yml::to_string(config)?;
fs::create_dir_all(&path.parent().expect("Config save error")).expect("Config save error"); fs::create_dir_all(&path.parent().ok_or::<Box<dyn Error>>("cant find parent".into())?)?;
fs::write(&path, config_text).expect("Config save error"); fs::write(&path, config_text)?;
Ok(())
} }
#[derive(Parser, Debug)] #[derive(Parser, Debug)]

View File

@ -22,7 +22,7 @@ fn main() {
return; return;
} }
let mut config = load_config(config_path); let mut config = load_config(config_path).unwrap_or_default();
args.patch_config(&mut config); args.patch_config(&mut config);