diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2020-10-19 01:32:50 +0200 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2020-10-19 01:32:50 +0200 |
| commit | e552035142b36fa1da78faed5cf83ff89f4506c5 (patch) | |
| tree | ef576829909de84f2ddc3ecac78d7a0b87d317b4 /mumlib/src | |
| parent | ec323df881c3aad82ed963fbfbdd9ade9f96e830 (diff) | |
| download | mum-e552035142b36fa1da78faed5cf83ff89f4506c5.tar.gz | |
initial reading of config file
Diffstat (limited to 'mumlib/src')
| -rw-r--r-- | mumlib/src/config.rs | 60 | ||||
| -rw-r--r-- | mumlib/src/lib.rs | 1 |
2 files changed, 61 insertions, 0 deletions
diff --git a/mumlib/src/config.rs b/mumlib/src/config.rs new file mode 100644 index 0000000..0012cc6 --- /dev/null +++ b/mumlib/src/config.rs @@ -0,0 +1,60 @@ +use serde::Deserialize; +use std::fs; +use toml::value::Array; + +#[derive(Debug, Deserialize)] +struct TOMLConfig { + audio: Option<AudioConfig>, + servers: Option<Array>, +} + +#[derive(Debug, Deserialize)] +pub struct Config { + pub audio: Option<AudioConfig>, + pub servers: Option<Vec<ServerConfig>>, +} + +#[derive(Debug, Deserialize)] +pub struct AudioConfig { + pub input_volume: Option<f32>, +} + +#[derive(Debug, Deserialize)] +pub struct ServerConfig { + pub name: String, + pub host: String, + pub port: u16, + pub username: Option<String>, + pub password: Option<String>, +} + +fn get_cfg_path() -> String { + "~/.mumdrc".to_string() //TODO XDG_CONFIG and whatever +} + +impl From<TOMLConfig> for Config { + fn from(config: TOMLConfig) -> Self { + Config { + audio: config.audio, + servers: if let Some(servers) = config.servers { + Some(servers + .into_iter() + .map(|s| s.try_into::<ServerConfig>().expect("invalid server config format")) + .collect()) + } else { + None + }, + } + } +} + +pub fn read_default_cfg() -> Config { + //TODO ignore when config file doesn't exist + Config::from( + toml::from_str::<TOMLConfig>( + &fs::read_to_string( + get_cfg_path()) + .expect("config file not found") + .to_string()) + .unwrap()) +} diff --git a/mumlib/src/lib.rs b/mumlib/src/lib.rs index b26db13..93b7682 100644 --- a/mumlib/src/lib.rs +++ b/mumlib/src/lib.rs @@ -1,4 +1,5 @@ pub mod command; +pub mod config; pub mod error; pub mod state; |
