From 3fc9bd7bca2cfe6b7c59b620c6275a2f9a3bb140 Mon Sep 17 00:00:00 2001 From: MeexReay Date: Sat, 5 Jul 2025 18:26:58 +0300 Subject: [PATCH] fix: remove except from config loading and fall to default --- src/chat/config.rs | 23 +++++++++++------------ src/main.rs | 2 +- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/chat/config.rs b/src/chat/config.rs index 4da4e6a..bc03be8 100644 --- a/src/chat/config.rs +++ b/src/chat/config.rs @@ -1,7 +1,7 @@ use clap::Parser; use serde_default::DefaultFromSerde; 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}"; @@ -89,23 +89,22 @@ pub fn get_config_path() -> PathBuf { .join("config.yml") } -pub fn load_config(path: PathBuf) -> Config { +pub fn load_config(path: PathBuf) -> Result> { if !fs::exists(&path).unwrap_or_default() { let config = Config::default(); - let config_text = serde_yml::to_string(&config).expect("Config save error"); - fs::create_dir_all(&path.parent().expect("Config save error")).expect("Config save error"); - fs::write(&path, config_text).expect("Config save error"); - config + save_config(path, &config)?; + Ok(config) } else { - let config = &fs::read_to_string(&path).expect("Config load error"); - serde_yml::from_str(config).expect("Config load error") + let config = &fs::read_to_string(&path)?; + Ok(serde_yml::from_str(config)?) } } -pub fn save_config(path: PathBuf, config: &Config) { - let config_text = serde_yml::to_string(config).expect("Config save error"); - fs::create_dir_all(&path.parent().expect("Config save error")).expect("Config save error"); - fs::write(&path, config_text).expect("Config save error"); +pub fn save_config(path: PathBuf, config: &Config) -> Result<(), Box> { + let config_text = serde_yml::to_string(config)?; + fs::create_dir_all(&path.parent().ok_or::>("cant find parent".into())?)?; + fs::write(&path, config_text)?; + Ok(()) } #[derive(Parser, Debug)] diff --git a/src/main.rs b/src/main.rs index e2071a6..30924dd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,7 @@ fn main() { return; } - let mut config = load_config(config_path); + let mut config = load_config(config_path).unwrap_or_default(); args.patch_config(&mut config);