use std::path::{Path, PathBuf}; use serde::{Deserialize, Serialize}; use crate::cli::CliArgs; #[derive(Debug, Serialize, Deserialize, Default)] pub struct Config { pub seperator: String, pub refresh_ms: usize, pub sock_path: String, pub plugins: ConfigPlugins, #[serde(skip)] pub config_dir: PathBuf, } #[derive(Debug, Serialize, Deserialize, Default)] pub struct ConfigPlugins { pub path: PathBuf, pub blacklist: Vec, pub as_whitelist: bool, pub template: Vec, } impl Config { pub fn new(cli: CliArgs) -> Result { let cfg_path = cli.config_dir.join("main.toml"); if !cfg_path.exists() { println!("ERROR: {:?} doesnt exist", cli.config_dir); } let mut cfg: Self = toml::from_str( &std::fs::read_to_string(&cfg_path)? )?; cfg.config_dir = cli.config_dir.as_std_path().to_path_buf(); cfg.plugins.path = cli.plugin_dir.as_std_path().to_path_buf(); Ok(cfg) } } #[allow(dead_code)] #[derive(Debug)] pub enum ConfigError { IoError(std::io::Error), TomlDeserialiseError(toml::de::Error) } impl From for ConfigError { fn from(e: std::io::Error) -> Self { Self::IoError(e) } } impl From for ConfigError { fn from(e: toml::de::Error) -> Self { Self::TomlDeserialiseError(e) } }