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 | |
| parent | ec323df881c3aad82ed963fbfbdd9ade9f96e830 (diff) | |
| download | mum-e552035142b36fa1da78faed5cf83ff89f4506c5.tar.gz | |
initial reading of config file
| -rw-r--r-- | mumd/Cargo.toml | 1 | ||||
| -rw-r--r-- | mumd/src/state.rs | 13 | ||||
| -rw-r--r-- | mumlib/Cargo.toml | 1 | ||||
| -rw-r--r-- | mumlib/src/config.rs | 60 | ||||
| -rw-r--r-- | mumlib/src/lib.rs | 1 |
5 files changed, 74 insertions, 2 deletions
diff --git a/mumd/Cargo.toml b/mumd/Cargo.toml index 9101b43..ffb463a 100644 --- a/mumd/Cargo.toml +++ b/mumd/Cargo.toml @@ -27,6 +27,5 @@ tokio = { version = "0.2", features = ["full"] } tokio-tls = "0.3" tokio-util = { version = "0.3", features = ["codec", "udp"] } -#clap = "2.33" #compressor = "0.3" #daemonize = "0.4" diff --git a/mumd/src/state.rs b/mumd/src/state.rs index 55fd8ae..e2892fc 100644 --- a/mumd/src/state.rs +++ b/mumd/src/state.rs @@ -6,6 +6,7 @@ use mumble_protocol::control::msgs; use mumble_protocol::control::ControlPacket; use mumble_protocol::voice::Serverbound; use mumlib::command::{Command, CommandResponse}; +use mumlib::config::Config; use mumlib::error::{ChannelIdentifierError, Error}; use serde::{Deserialize, Serialize}; use std::collections::hash_map::Entry; @@ -21,6 +22,7 @@ pub enum StatePhase { } pub struct State { + config: Config, server: Option<Server>, audio: Audio, @@ -28,6 +30,7 @@ pub struct State { connection_info_sender: watch::Sender<Option<ConnectionInfo>>, phase_watcher: (watch::Sender<StatePhase>, watch::Receiver<StatePhase>), + } impl State { @@ -35,9 +38,17 @@ impl State { packet_sender: mpsc::UnboundedSender<ControlPacket<Serverbound>>, connection_info_sender: watch::Sender<Option<ConnectionInfo>>, ) -> Self { + let config = mumlib::config::read_default_cfg(); + let audio = Audio::new(); + if let Some(ref audio_config) = config.audio { + if let Some(input_volume) = audio_config.input_volume { + audio.set_input_volume(input_volume); + } + } Self { + config, server: None, - audio: Audio::new(), + audio, packet_sender, connection_info_sender, phase_watcher: watch::channel(StatePhase::Disconnected), 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<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; |
