aboutsummaryrefslogtreecommitdiffstats
path: root/mumlib/src/config.rs
diff options
context:
space:
mode:
Diffstat (limited to 'mumlib/src/config.rs')
-rw-r--r--mumlib/src/config.rs61
1 files changed, 40 insertions, 21 deletions
diff --git a/mumlib/src/config.rs b/mumlib/src/config.rs
index e6b97fd..3a2fa27 100644
--- a/mumlib/src/config.rs
+++ b/mumlib/src/config.rs
@@ -1,6 +1,8 @@
+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;
@@ -13,8 +15,8 @@ struct TOMLConfig {
#[derive(Clone, Debug, Default)]
pub struct Config {
- pub audio: Option<AudioConfig>,
- pub servers: Option<Vec<ServerConfig>>,
+ pub audio: AudioConfig,
+ pub servers: Vec<ServerConfig>,
}
impl Config {
@@ -44,9 +46,10 @@ impl Config {
}
}
-#[derive(Clone, Debug, Deserialize, Serialize)]
+#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct AudioConfig {
pub input_volume: Option<f32>,
+ pub output_volume: Option<f32>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
@@ -58,6 +61,18 @@ pub struct ServerConfig {
pub password: Option<String>,
}
+impl ServerConfig {
+ pub fn to_socket_addr(&self) -> Option<SocketAddr> {
+ 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);
@@ -113,7 +128,7 @@ impl TryFrom<TOMLConfig> for Config {
fn try_from(config: TOMLConfig) -> Result<Self, Self::Error> {
Ok(Config {
- audio: config.audio,
+ audio: config.audio.unwrap_or_default(),
servers: config
.servers
.map(|servers| {
@@ -122,7 +137,8 @@ impl TryFrom<TOMLConfig> for Config {
.map(|s| s.try_into::<ServerConfig>())
.collect()
})
- .transpose()?,
+ .transpose()?
+ .unwrap_or(Vec::new()),
})
}
}
@@ -130,26 +146,29 @@ impl TryFrom<TOMLConfig> for Config {
impl From<Config> for TOMLConfig {
fn from(config: Config) -> Self {
TOMLConfig {
- audio: config.audio,
- servers: config.servers.map(|servers| {
- servers
+ audio: if config.audio.output_volume.is_some() || config.audio.input_volume.is_some() {
+ Some(config.audio)
+ } else {
+ None
+ },
+ servers: Some(
+ config
+ .servers
.into_iter()
.map(|s| Value::try_from::<ServerConfig>(s).unwrap())
- .collect()
- }),
+ .collect(),
+ ),
}
}
}
-pub fn read_default_cfg() -> Option<Config> {
- Some(
- Config::try_from(
- toml::from_str::<TOMLConfig>(&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::<TOMLConfig>(&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
}