From e552035142b36fa1da78faed5cf83ff89f4506c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 19 Oct 2020 01:32:50 +0200 Subject: initial reading of config file --- mumlib/Cargo.toml | 1 + mumlib/src/config.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ mumlib/src/lib.rs | 1 + 3 files changed, 62 insertions(+) create mode 100644 mumlib/src/config.rs (limited to 'mumlib') diff --git a/mumlib/Cargo.toml b/mumlib/Cargo.toml index a2627d4..471a1fe 100644 --- a/mumlib/Cargo.toml +++ b/mumlib/Cargo.toml @@ -13,3 +13,4 @@ fern = "0.5" log = "0.4" mumble-protocol = "0.3" serde = { version = "1.0", features = ["derive"] } +toml = "0.5" 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, + servers: Option, +} + +#[derive(Debug, Deserialize)] +pub struct Config { + pub audio: Option, + pub servers: Option>, +} + +#[derive(Debug, Deserialize)] +pub struct AudioConfig { + pub input_volume: Option, +} + +#[derive(Debug, Deserialize)] +pub struct ServerConfig { + pub name: String, + pub host: String, + pub port: u16, + pub username: Option, + pub password: Option, +} + +fn get_cfg_path() -> String { + "~/.mumdrc".to_string() //TODO XDG_CONFIG and whatever +} + +impl From 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::().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::( + &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; -- cgit v1.2.1