aboutsummaryrefslogtreecommitdiffstats
path: root/mumlib/src/config.rs
blob: 0012cc62a60b9af9962144b8961365cea25da761 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use serde::Deserialize;
use std::fs;
use toml::value::Array;

#[derive(Debug, Deserialize)]
struct TOMLConfig {
    audio: Option<AudioConfig>,
    servers: Option<Array>,
}

#[derive(Debug, Deserialize)]
pub struct Config {
    pub audio: Option<AudioConfig>,
    pub servers: Option<Vec<ServerConfig>>,
}

#[derive(Debug, Deserialize)]
pub struct AudioConfig {
    pub input_volume: Option<f32>,
}

#[derive(Debug, Deserialize)]
pub struct ServerConfig {
    pub name: String,
    pub host: String,
    pub port: u16,
    pub username: Option<String>,
    pub password: Option<String>,
}

fn get_cfg_path() -> String {
    "~/.mumdrc".to_string() //TODO XDG_CONFIG and whatever
}

impl From<TOMLConfig> for Config {
    fn from(config: TOMLConfig) -> Self {
        Config {
            audio: config.audio,
            servers: if let Some(servers) = config.servers {
                Some(servers
                    .into_iter()
                    .map(|s| s.try_into::<ServerConfig>().expect("invalid server config format"))
                    .collect())
            } else {
                None
            },
        }
    }
}

pub fn read_default_cfg() -> Config {
    //TODO ignore when config file doesn't exist
    Config::from(
        toml::from_str::<TOMLConfig>(
            &fs::read_to_string(
                get_cfg_path())
                .expect("config file not found")
                .to_string())
            .unwrap())
}