diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2020-10-29 21:28:12 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2020-10-29 21:28:12 +0100 |
| commit | 4010e0f5abb28bae3207c78dba74f0896eedea51 (patch) | |
| tree | 2e1c215705d04f86e47a1a0a9015dfd6ed6feeaf /mumlib/src/config.rs | |
| parent | 3e7e375e65760a03b6692106ab0ed806ca65e470 (diff) | |
| download | mum-4010e0f5abb28bae3207c78dba74f0896eedea51.tar.gz | |
cargo fmt
Diffstat (limited to 'mumlib/src/config.rs')
| -rw-r--r-- | mumlib/src/config.rs | 61 |
1 files changed, 36 insertions, 25 deletions
diff --git a/mumlib/src/config.rs b/mumlib/src/config.rs index 5c37a2b..e6b97fd 100644 --- a/mumlib/src/config.rs +++ b/mumlib/src/config.rs @@ -1,9 +1,9 @@ use serde::{Deserialize, Serialize}; use std::convert::TryFrom; use std::fs; -use toml::Value; -use toml::value::Array; use std::path::Path; +use toml::value::Array; +use toml::Value; #[derive(Debug, Deserialize, Serialize)] struct TOMLConfig { @@ -19,7 +19,11 @@ pub struct Config { impl Config { pub fn write_default_cfg(&self, create: bool) -> Result<(), std::io::Error> { - let path = if create { get_creatable_cfg_path() } else { get_cfg_path() }; + let path = if create { + get_creatable_cfg_path() + } else { + get_cfg_path() + }; let path = std::path::Path::new(&path); // Possible race here. It's fine since it shows when: // 1) the file doesn't exist when checked and is then created @@ -33,7 +37,10 @@ impl Config { return Ok(()); } - fs::write(path, toml::to_string(&TOMLConfig::from(self.clone())).unwrap()) + fs::write( + path, + toml::to_string(&TOMLConfig::from(self.clone())).unwrap(), + ) } } @@ -107,11 +114,15 @@ impl TryFrom<TOMLConfig> for Config { fn try_from(config: TOMLConfig) -> Result<Self, Self::Error> { Ok(Config { audio: config.audio, - servers: config.servers.map(|servers| servers - .into_iter() - .map(|s| s.try_into::<ServerConfig>()) - .collect()) - .transpose()?, + servers: config + .servers + .map(|servers| { + servers + .into_iter() + .map(|s| s.try_into::<ServerConfig>()) + .collect() + }) + .transpose()?, }) } } @@ -120,25 +131,25 @@ impl From<Config> for TOMLConfig { fn from(config: Config) -> Self { TOMLConfig { audio: config.audio, - servers: config.servers.map(|servers| servers - .into_iter() - .map(|s| Value::try_from::<ServerConfig>(s).unwrap()) - .collect()), + servers: config.servers.map(|servers| { + servers + .into_iter() + .map(|s| Value::try_from::<ServerConfig>(s).unwrap()) + .collect() + }), } } } pub fn read_default_cfg() -> Option<Config> { - Some(Config::try_from( - toml::from_str::<TOMLConfig>( - &match fs::read_to_string(get_cfg_path()) { - Ok(f) => { - f.to_string() - }, - Err(_) => { - return None - } - } - ).expect("invalid TOML in config file") //TODO - ).expect("invalid config in TOML")) //TODO + Some( + Config::try_from( + toml::from_str::<TOMLConfig>(&match fs::read_to_string(get_cfg_path()) { + Ok(f) => f.to_string(), + Err(_) => return None, + }) + .expect("invalid TOML in config file"), //TODO + ) + .expect("invalid config in TOML"), + ) //TODO } |
