// // Parsing the config file // use anyhow; use std::fs; use std::sync::{Arc, Mutex}; use serde_derive::Deserialize; use crate::types::errors::HuskError; #[derive(Deserialize, Debug, Clone)] pub struct HuskConfig { pub connection: String, pub sequoia_home: String, logfile_config: Option, pub keyservers: Option>, } impl HuskConfig { pub fn load(path: &String) -> anyhow::Result { match fs::read_to_string(path) { Ok(data) => { match toml::from_str(data.as_str()) { Ok(configuration) => Ok(configuration), Err(e) => { Err(HuskError::ConfigFileParseError(e.to_string()).into()) } } }, Err(e) => { Err(HuskError::ConfigFileOpenError(e.to_string()).into()) } } } pub fn logfile_config(&self) -> String { match &self.logfile_config { Some(c) => c.clone(), None => { eprintln!("Using logging configuration from 'log4rs.yml'"); "log4rs.yml".to_string() }, } } pub fn get_keyservers(&self) -> Vec { if let Some(kss) = &self.keyservers { kss.clone() } else { Vec::new() } } } pub type HuskConfigContainer = Arc>; impl From for HuskConfig { fn from(container: HuskConfigContainer) -> Self { let config = container.lock().unwrap(); config.clone() } }