From d290a6926b2c495d04396924985ac87e632de1da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 19 Oct 2020 23:35:21 +0200 Subject: some changes They got a bit bundled together but they at least do the following: - read_default_cfg returns a result - command to print all server config values - command to print a single server config value - command to remove servers - Option::map instead of if let Some()-... - TryFrom instead of Try --- mumlib/src/config.rs | 64 ++++++++++++++++++---------------------------------- 1 file changed, 22 insertions(+), 42 deletions(-) (limited to 'mumlib/src/config.rs') diff --git a/mumlib/src/config.rs b/mumlib/src/config.rs index d93b172..3e55607 100644 --- a/mumlib/src/config.rs +++ b/mumlib/src/config.rs @@ -1,4 +1,5 @@ use serde::{Deserialize, Serialize}; +use std::convert::TryFrom; use std::fs; use toml::Value; use toml::value::Array; @@ -9,7 +10,7 @@ struct TOMLConfig { servers: Option, } -#[derive(Debug)] +#[derive(Clone, Debug)] pub struct Config { pub audio: Option, pub servers: Option>, @@ -17,7 +18,7 @@ pub struct Config { impl Config { pub fn write_default_cfg(&self) { - fs::write(get_cfg_path(), toml::to_string(&TOMLConfig::from(self)).unwrap()).unwrap(); + fs::write(get_cfg_path(), toml::to_string(&TOMLConfig::from(self.clone())).unwrap()).unwrap(); } } @@ -30,7 +31,7 @@ pub struct AudioConfig { pub struct ServerConfig { pub name: String, pub host: String, - pub port: u16, + pub port: Option, pub username: Option, pub password: Option, } @@ -39,19 +40,18 @@ fn get_cfg_path() -> String { ".mumdrc".to_string() //TODO XDG_CONFIG and whatever } -impl From for Config { - fn from(config: TOMLConfig) -> Self { - Config { +impl TryFrom for Config { + type Error = toml::de::Error; + + fn try_from(config: TOMLConfig) -> Result { + Ok(Config { audio: config.audio, - servers: if let Some(servers) = config.servers { - Some(servers - .into_iter() - .map(|s| s.try_into::().expect("invalid server config format")) - .collect()) - } else { - None - }, - } + servers: config.servers.map(|servers| servers + .into_iter() + .map(|s| s.try_into::()) + .collect()) + .transpose()?, + }) } } @@ -59,37 +59,17 @@ impl From for TOMLConfig { fn from(config: Config) -> Self { TOMLConfig { audio: config.audio, - servers: if let Some(servers) = config.servers { - Some(servers - .into_iter() - .map(|s| Value::try_from::(s).unwrap()) - .collect()) - } else { - None - }, - } - } -} - -impl From<&Config> for TOMLConfig { - fn from(config: &Config) -> Self { - TOMLConfig { - audio: config.audio.clone(), - servers: if let Some(servers) = config.servers.clone() { - Some(servers - .into_iter() - .map(|s| Value::try_from::(s).unwrap()) - .collect()) - } else { - None - }, + servers: config.servers.map(|servers| servers + .into_iter() + .map(|s| Value::try_from::(s).unwrap()) + .collect()), } } } -pub fn read_default_cfg() -> Config { - //TODO ignore when config file doesn't exist - Config::from( +pub fn read_default_cfg() -> Result { + //TODO return None if file doesn't exist (Option::map) + Config::try_from( toml::from_str::( &fs::read_to_string( get_cfg_path()) -- cgit v1.2.1