From 3e61f2d4a48b621c4cef820e24cd2515cbc1c919 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 19 Oct 2020 02:40:43 +0200 Subject: convert config to string --- mumlib/src/config.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 6 deletions(-) (limited to 'mumlib/src') diff --git a/mumlib/src/config.rs b/mumlib/src/config.rs index 0012cc6..d6550c1 100644 --- a/mumlib/src/config.rs +++ b/mumlib/src/config.rs @@ -1,25 +1,34 @@ -use serde::Deserialize; +use log::*; +use serde::{Deserialize, Serialize}; use std::fs; +use toml::Value; use toml::value::Array; -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Serialize)] struct TOMLConfig { audio: Option, servers: Option, } -#[derive(Debug, Deserialize)] +#[derive(Debug)] pub struct Config { pub audio: Option, pub servers: Option>, } -#[derive(Debug, Deserialize)] +impl Config { + pub fn write_default_cfg(&self) { + debug!("{}", toml::to_string(&(TOMLConfig::from(self))).unwrap()); + //fs::write(, get_cfg_path()) + } +} + +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct AudioConfig { pub input_volume: Option, } -#[derive(Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct ServerConfig { pub name: String, pub host: String, @@ -29,7 +38,7 @@ pub struct ServerConfig { } fn get_cfg_path() -> String { - "~/.mumdrc".to_string() //TODO XDG_CONFIG and whatever + ".mumdrc".to_string() //TODO XDG_CONFIG and whatever } impl From for Config { @@ -48,6 +57,38 @@ impl From for Config { } } +impl From for TOMLConfig { + fn from(config: Config) -> Self { + TOMLConfig { + audio: config.audio, + servers: if let Some(servers) = config.servers { + Some(servers + .into_iter() + .map(|s| Value::try_from::(s).unwrap()) + .collect()) + } else { + None + }, + } + } +} + +impl From<&Config> for TOMLConfig { + fn from(config: &Config) -> Self { + TOMLConfig { + audio: config.audio.clone(), + servers: if let Some(servers) = config.servers.clone() { + Some(servers + .into_iter() + .map(|s| Value::try_from::(s).unwrap()) + .collect()) + } else { + None + }, + } + } +} + pub fn read_default_cfg() -> Config { //TODO ignore when config file doesn't exist Config::from( -- cgit v1.2.1