use std::fs; use std::env; use serde::{Deserialize, Serialize}; use toml; #[derive(Debug, Deserialize, Serialize)] #[serde(rename_all = "lowercase")] pub enum Loglevel { Trace, Debug, Info, Warn, Error } /// `mesk.toml` configuration fields here #[derive(Deserialize, Debug, Serialize)] pub struct Config { pub repo: Repo, pub log: Log, pub paths: Paths, } #[derive(Deserialize, Debug, Serialize)] pub struct Log { #[serde(rename = "log_file")] pub log_file: String, #[serde(rename = "log_level")] pub log_level: Loglevel, } // Rename needed for editing mesk.toml file fields but dont touch code. #[derive(Deserialize, Debug, Serialize)] pub struct Repo { #[serde(rename = "repo_url")] pub repo_url: String, #[serde(rename = "auto_update")] pub auto_update: bool, } #[derive(Deserialize, Debug, Serialize)] pub struct Paths { #[serde(rename = "cache_dir")] pub cache_dir: String, #[serde(rename = "build_dir")] pub build_dir: String, } impl Config { /// Parse the /etc/mesk.toml file and return the Config object. /// /// This function reads the /etc/mesk.toml file, parses it and returns the Config object. pub fn parse() -> Result { let contents = fs::read_to_string("/etc/mesk.toml").unwrap(); let result: Config = toml::from_str(&contents)?; Ok(result) } /// Return the default configuration as a toml string. /// /// This function returns the default configuration as a toml string. pub fn default() -> Result { let default: Config = Config { repo: Repo { repo_url: format!("https://mesk.anthrill.i2p/repo/{}/", std::env::consts::ARCH), auto_update: true, }, log: Log { log_file: String::from("/var/log/mesk.log"), log_level: Loglevel::Info, }, paths: Paths { cache_dir: String::from("/var/cache/mesk"), build_dir: String::from("/var/lib/mesk"), }, }; let toml_str = toml::to_string(&default)?; Ok(toml_str) } }