use serde::{de::DeserializeOwned, Serialize}; use std::{ error::Error, fs::{self, OpenOptions}, io::Write, }; use crate::{config::WriteConfig, global::functions::paths}; pub const EXTENSION: &str = "yml"; /// Trait for loading, saving config files pub trait ConfigTrait { const LABEL: &'static str; fn load(write: WriteConfig) -> Result, Box> where Self: Serialize + DeserializeOwned + Default + Clone, { let config_path = paths::config_dir().join(format!("{}.{}", Self::LABEL, EXTENSION)); // The config struct let config: Self = { || -> Result> { let content = match fs::read_to_string(&config_path) { Ok(content) => content, // If the config file does not exist returns Self::defult() Err(_) => return Ok(Self::default()), }; match serde_yaml_ng::from_str::(&content) { Ok(config) => Ok(config), // If there is error parsing the json file, create a backup with current time and returns // Self::default() Err(_) => { let mut new_path = config_path.clone(); new_path.pop(); new_path.push(format!( "{}-{}.{}", Self::LABEL, chrono::offset::Local::now(), EXTENSION )); fs::rename(&config_path, &new_path)?; Ok(Self::default()) } } } }()?; // Overwrites the old config file with added options (if any), // but it also removes things like comments in the old config file match write { WriteConfig::Must => { let mut file = OpenOptions::new() .write(true) .create(true) .truncate(true) .open(&config_path)?; file.write_all(serde_yaml_ng::to_string(&config)?.as_bytes())? } WriteConfig::Try => { if let Ok(mut file) = OpenOptions::new() .write(true) .create(true) .truncate(true) .open(&config_path) { let _ = file.write_all(serde_yaml_ng::to_string(&config)?.as_bytes()); } } WriteConfig::Dont => {} } Ok(Box::new(config)) } }