aboutsummaryrefslogtreecommitdiffstats
path: root/mumlib/src/config.rs
diff options
context:
space:
mode:
authorEskil <eskilq@kth.se>2020-11-03 22:02:32 +0100
committerEskil <eskilq@kth.se>2020-11-03 22:02:32 +0100
commitd6496cb0f6abba855b04338fa8bc5aaa89487c29 (patch)
tree929f5c18babe5bc27676c88aae6b9d3795b09917 /mumlib/src/config.rs
parent831182b69eb1bbfedfad1288b73a822241f18d25 (diff)
parent972c11fe66c17728981ec57796c78fb70c7bd180 (diff)
downloadmum-d6496cb0f6abba855b04338fa8bc5aaa89487c29.tar.gz
Merge branch 'audio-volume' into 'main'
Refactor of config and add support for changing global output volume and individual user's volume See merge request gustav/mum!28
Diffstat (limited to 'mumlib/src/config.rs')
-rw-r--r--mumlib/src/config.rs47
1 files changed, 26 insertions, 21 deletions
diff --git a/mumlib/src/config.rs b/mumlib/src/config.rs
index ae569aa..3a2fa27 100644
--- a/mumlib/src/config.rs
+++ b/mumlib/src/config.rs
@@ -15,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 {
@@ -46,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)]
@@ -127,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| {
@@ -136,7 +137,8 @@ impl TryFrom<TOMLConfig> for Config {
.map(|s| s.try_into::<ServerConfig>())
.collect()
})
- .transpose()?,
+ .transpose()?
+ .unwrap_or(Vec::new()),
})
}
}
@@ -144,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
}