aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2020-10-19 02:40:43 +0200
committerGustav Sörnäs <gustav@sornas.net>2020-10-19 02:40:43 +0200
commit3e61f2d4a48b621c4cef820e24cd2515cbc1c919 (patch)
treef9b4ee83f2eb0db00d265174ae60c9c97ce15f47
parente552035142b36fa1da78faed5cf83ff89f4506c5 (diff)
downloadmum-3e61f2d4a48b621c4cef820e24cd2515cbc1c919.tar.gz
convert config to string
-rw-r--r--mumd/src/main.rs1
-rw-r--r--mumd/src/state.rs4
-rw-r--r--mumlib/src/config.rs53
3 files changed, 52 insertions, 6 deletions
diff --git a/mumd/src/main.rs b/mumd/src/main.rs
index 75726f8..5307a09 100644
--- a/mumd/src/main.rs
+++ b/mumd/src/main.rs
@@ -35,6 +35,7 @@ async fn main() {
watch::channel::<Option<ConnectionInfo>>(None);
let state = State::new(packet_sender, connection_info_sender);
+ state.config().write_default_cfg();
let state = Arc::new(Mutex::new(state));
let (_, _, _, e) = join!(
diff --git a/mumd/src/state.rs b/mumd/src/state.rs
index e2892fc..e199552 100644
--- a/mumd/src/state.rs
+++ b/mumd/src/state.rs
@@ -215,6 +215,10 @@ impl State {
.unwrap();
}
+ pub fn config(&self) -> &Config {
+ &self.config
+ }
+
pub fn audio(&self) -> &Audio {
&self.audio
}
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<AudioConfig>,
servers: Option<Array>,
}
-#[derive(Debug, Deserialize)]
+#[derive(Debug)]
pub struct Config {
pub audio: Option<AudioConfig>,
pub servers: Option<Vec<ServerConfig>>,
}
-#[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<f32>,
}
-#[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<TOMLConfig> for Config {
@@ -48,6 +57,38 @@ impl From<TOMLConfig> for Config {
}
}
+impl From<Config> 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::<ServerConfig>(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::<ServerConfig>(s).unwrap())
+ .collect())
+ } else {
+ None
+ },
+ }
+ }
+}
+
pub fn read_default_cfg() -> Config {
//TODO ignore when config file doesn't exist
Config::from(