From 11c823701b12f10933b40044a12cc4048ccf8bd2 Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Sat, 31 Oct 2020 02:27:26 +0100 Subject: add support for mumctl server list --- mumlib/src/config.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'mumlib/src/config.rs') diff --git a/mumlib/src/config.rs b/mumlib/src/config.rs index e6b97fd..e7d107a 100644 --- a/mumlib/src/config.rs +++ b/mumlib/src/config.rs @@ -4,6 +4,8 @@ use std::fs; use std::path::Path; use toml::value::Array; use toml::Value; +use std::net::{SocketAddr, ToSocketAddrs}; +use crate::DEFAULT_PORT; #[derive(Debug, Deserialize, Serialize)] struct TOMLConfig { @@ -58,6 +60,15 @@ pub struct ServerConfig { pub password: Option, } +impl ServerConfig { + pub fn to_socket_addr(&self) -> Option { + match (self.host.as_str(), self.port.unwrap_or(DEFAULT_PORT)).to_socket_addrs().map(|mut e| e.next()) { + Ok(Some(addr)) => Some(addr), + _ => None, + } + } +} + pub fn get_cfg_path() -> String { if let Ok(var) = std::env::var("XDG_CONFIG_HOME") { let path = format!("{}/mumdrc", var); -- cgit v1.2.1 From d72b0fe5862a99d9ce1a0ef37938f4517de36ed7 Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Sat, 31 Oct 2020 02:37:24 +0100 Subject: cargo fmt --- mumlib/src/config.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'mumlib/src/config.rs') diff --git a/mumlib/src/config.rs b/mumlib/src/config.rs index e7d107a..ae569aa 100644 --- a/mumlib/src/config.rs +++ b/mumlib/src/config.rs @@ -1,11 +1,11 @@ +use crate::DEFAULT_PORT; use serde::{Deserialize, Serialize}; use std::convert::TryFrom; use std::fs; +use std::net::{SocketAddr, ToSocketAddrs}; use std::path::Path; use toml::value::Array; use toml::Value; -use std::net::{SocketAddr, ToSocketAddrs}; -use crate::DEFAULT_PORT; #[derive(Debug, Deserialize, Serialize)] struct TOMLConfig { @@ -62,7 +62,10 @@ pub struct ServerConfig { impl ServerConfig { pub fn to_socket_addr(&self) -> Option { - match (self.host.as_str(), self.port.unwrap_or(DEFAULT_PORT)).to_socket_addrs().map(|mut e| e.next()) { + match (self.host.as_str(), self.port.unwrap_or(DEFAULT_PORT)) + .to_socket_addrs() + .map(|mut e| e.next()) + { Ok(Some(addr)) => Some(addr), _ => None, } -- cgit v1.2.1 From 0cb39d13bba3dc5ffa3231e6021066e4191a43a4 Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Mon, 2 Nov 2020 21:47:47 +0100 Subject: refactor and add audio out config command --- mumlib/src/config.rs | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) (limited to 'mumlib/src/config.rs') diff --git a/mumlib/src/config.rs b/mumlib/src/config.rs index e6b97fd..a971b2d 100644 --- a/mumlib/src/config.rs +++ b/mumlib/src/config.rs @@ -13,7 +13,7 @@ struct TOMLConfig { #[derive(Clone, Debug, Default)] pub struct Config { - pub audio: Option, + pub audio: AudioConfig, pub servers: Option>, } @@ -44,9 +44,10 @@ impl Config { } } -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Default, Deserialize, Serialize)] pub struct AudioConfig { pub input_volume: Option, + pub output_volume: Option, } #[derive(Clone, Debug, Deserialize, Serialize)] @@ -113,7 +114,7 @@ impl TryFrom for Config { fn try_from(config: TOMLConfig) -> Result { Ok(Config { - audio: config.audio, + audio: config.audio.unwrap_or_default(), servers: config .servers .map(|servers| { @@ -130,7 +131,11 @@ impl TryFrom for Config { impl From for TOMLConfig { fn from(config: Config) -> Self { TOMLConfig { - audio: config.audio, + audio: if config.audio.output_volume.is_some() || config.audio.input_volume.is_some() { + Some(config.audio) + } else { + None + }, servers: config.servers.map(|servers| { servers .into_iter() @@ -141,15 +146,11 @@ impl From for TOMLConfig { } } -pub fn read_default_cfg() -> Option { - Some( - Config::try_from( - toml::from_str::(&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 +pub fn read_default_cfg() -> Config { + Config::try_from( + toml::from_str::(&match fs::read_to_string(get_cfg_path()) { + Ok(f) => f, + Err(_) => return Config::default(), + }).expect("invalid TOML in config file"), //TODO + ).expect("invalid config in TOML") //TODO } -- cgit v1.2.1 From 9d60f06ae05c5de08a026c7f9067c1a339bc24be Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Mon, 2 Nov 2020 22:57:52 +0100 Subject: remove redundancy --- mumlib/src/config.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'mumlib/src/config.rs') diff --git a/mumlib/src/config.rs b/mumlib/src/config.rs index a971b2d..210dc7b 100644 --- a/mumlib/src/config.rs +++ b/mumlib/src/config.rs @@ -14,7 +14,7 @@ struct TOMLConfig { #[derive(Clone, Debug, Default)] pub struct Config { pub audio: AudioConfig, - pub servers: Option>, + pub servers: Vec, } impl Config { @@ -123,7 +123,8 @@ impl TryFrom for Config { .map(|s| s.try_into::()) .collect() }) - .transpose()?, + .transpose()? + .unwrap_or(Vec::new()), }) } } @@ -136,12 +137,9 @@ impl From for TOMLConfig { } else { None }, - servers: config.servers.map(|servers| { - servers - .into_iter() - .map(|s| Value::try_from::(s).unwrap()) - .collect() - }), + servers: Some(config.servers.into_iter() + .map(|s| Value::try_from::(s).unwrap()) + .collect()), } } } -- cgit v1.2.1 From 36b5d69929d15212f5845f42d0239ba50e46a69c Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Mon, 2 Nov 2020 23:01:08 +0100 Subject: cargo fmt --- mumlib/src/config.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'mumlib/src/config.rs') diff --git a/mumlib/src/config.rs b/mumlib/src/config.rs index 210dc7b..2eca104 100644 --- a/mumlib/src/config.rs +++ b/mumlib/src/config.rs @@ -137,9 +137,13 @@ impl From for TOMLConfig { } else { None }, - servers: Some(config.servers.into_iter() - .map(|s| Value::try_from::(s).unwrap()) - .collect()), + servers: Some( + config + .servers + .into_iter() + .map(|s| Value::try_from::(s).unwrap()) + .collect(), + ), } } } @@ -149,6 +153,8 @@ pub fn read_default_cfg() -> Config { toml::from_str::(&match fs::read_to_string(get_cfg_path()) { Ok(f) => f, Err(_) => return Config::default(), - }).expect("invalid TOML in config file"), //TODO - ).expect("invalid config in TOML") //TODO + }) + .expect("invalid TOML in config file"), //TODO + ) + .expect("invalid config in TOML") //TODO } -- cgit v1.2.1