Change using Result

This commit is contained in:
fromost
2025-10-09 18:28:35 +08:00
parent 6f8a1eada1
commit 473ef8452b
3 changed files with 31 additions and 25 deletions

View File

@@ -1,21 +1,22 @@
use std::path::{PathBuf};
use ini::Ini;
use color_eyre::Result;
use crate::config::types::{ApplicationConfig, BasicConfig};
use crate::constants::{APP_CONFIG_DIR, APP_CONIFG_FILE_PATH, APP_DATA_DIR};
use crate::constants::{APP_CONIFG_FILE_PATH, APP_DATA_DIR};
pub mod types;
impl ApplicationConfig {
pub fn from_file(path: &PathBuf) -> Self {
let conf = Ini::load_from_file(path).unwrap();
pub fn from_file(path: &PathBuf) -> Result<Self> {
let conf = Ini::load_from_file(path)?;
let basic_conf_section = conf.section(Some("Basic")).unwrap();
let basic_conf = BasicConfig {
db_path: basic_conf_section.get("DBPath").unwrap().to_string(),
tick_rate: basic_conf_section.get("TickRate").unwrap().parse().unwrap(),
tick_rate: basic_conf_section.get("TickRate").unwrap().parse()?,
};
Self {
Ok(Self {
basic_config: basic_conf
}
})
}
pub fn new() -> Self {
@@ -25,15 +26,16 @@ impl ApplicationConfig {
tick_rate: 250
}
};
conf.clone().write_to_file(APP_CONIFG_FILE_PATH.to_path_buf());
conf.clone().write_to_file(APP_CONIFG_FILE_PATH.to_path_buf()).unwrap();
conf
}
pub fn write_to_file(self, path: PathBuf) {
pub fn write_to_file(self, path: PathBuf) -> Result<()> {
let mut conf = Ini::new();
conf.with_section(Some("Basic"))
.set("DBPath", self.basic_config.db_path)
.set("TickRate", self.basic_config.tick_rate.to_string());
conf.write_to_file(path).unwrap();
conf.write_to_file(path)?;
Ok(())
}
}