Change using Result
This commit is contained in:
@@ -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(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user