diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2020-10-19 02:40:43 +0200 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2020-10-19 02:40:43 +0200 |
| commit | 3e61f2d4a48b621c4cef820e24cd2515cbc1c919 (patch) | |
| tree | f9b4ee83f2eb0db00d265174ae60c9c97ce15f47 /mumlib/src | |
| parent | e552035142b36fa1da78faed5cf83ff89f4506c5 (diff) | |
| download | mum-3e61f2d4a48b621c4cef820e24cd2515cbc1c919.tar.gz | |
convert config to string
Diffstat (limited to 'mumlib/src')
| -rw-r--r-- | mumlib/src/config.rs | 53 |
1 files changed, 47 insertions, 6 deletions
diff --git a/mumlib/src/config.rs b/mumlib/src/config.rs index 0012cc6..d6550c1 100644 --- a/mumlib/src/config.rs +++ b/mumlib/src/config.rs @@ -1,25 +1,34 @@ -use serde::Deserialize; +use log::*; +use serde::{Deserialize, Serialize}; use std::fs; +use toml::Value; use toml::value::Array; -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Serialize)] struct TOMLConfig { audio: Option<AudioConfig>, servers: Option<Array>, } -#[derive(Debug, Deserialize)] +#[derive(Debug)] pub struct Config { pub audio: Option<AudioConfig>, pub servers: Option<Vec<ServerConfig>>, } -#[derive(Debug, Deserialize)] +impl Config { + pub fn write_default_cfg(&self) { + debug!("{}", toml::to_string(&(TOMLConfig::from(self))).unwrap()); + //fs::write(, get_cfg_path()) + } +} + +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct AudioConfig { pub input_volume: Option<f32>, } -#[derive(Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct ServerConfig { pub name: String, pub host: String, @@ -29,7 +38,7 @@ pub struct ServerConfig { } fn get_cfg_path() -> String { - "~/.mumdrc".to_string() //TODO XDG_CONFIG and whatever + ".mumdrc".to_string() //TODO XDG_CONFIG and whatever } impl From<TOMLConfig> for Config { @@ -48,6 +57,38 @@ impl From<TOMLConfig> for Config { } } +impl From<Config> 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::<ServerConfig>(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::<ServerConfig>(s).unwrap()) + .collect()) + } else { + None + }, + } + } +} + pub fn read_default_cfg() -> Config { //TODO ignore when config file doesn't exist Config::from( |
