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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use std::collections::HashMap;
use config::{Config, ConfigError, File, FileFormat, Value};
use xdg::BaseDirectories;

#[derive(Clone, Debug)]
pub struct GlobalSettings {
    pub color: String,
    pub focused_color: Option<String>,
    pub icon: String,
    pub size: String,
    pub separator: String,
}

impl GlobalSettings {
    pub fn new(
        color: String,
        focused_color: Option<String>,
        icon: String,
        size: String,
        separator: String,
    ) -> GlobalSettings {
        GlobalSettings {
            color,
            focused_color,
            icon,
            size,
            separator,
        }
    }

    pub fn from(mut config: HashMap<String, String>) -> GlobalSettings {
        let color = config.remove("color").unwrap();
        let focused_color = match config.remove("focused_color") {
            Some(color) => if color.len() == 7 && color.starts_with("#") {
                Some(color)
            } else {
                None
            },
            None => None
        };
        let icon = config.remove("icon").unwrap();
        let size = config.remove("size").unwrap();
        let separator = config
            .remove("separator")
            .unwrap_or(String::from(" "));
        GlobalSettings {
            color,
            focused_color,
            icon,
            size,
            separator,
        }
    }
}

#[derive(Clone, Debug)]
pub struct BaseSettings {
    pub color: Option<String>,
    pub icon: Option<String>,
    pub size: Option<String>,
}

impl BaseSettings {
    pub fn new(mut config: HashMap<String, Value>) -> BaseSettings {
        let color = match config.remove("color") {
            Some(color) => match color.into_str() {
                Ok(color) => Some(color),
                Err(_e) => None,
            },
            None => None,
        };
        let icon = match config.remove("icon") {
            Some(icon) => match icon.into_str() {
                Ok(icon) => Some(icon),
                Err(_e) => None,
            },
            None => None,
        };
        let size = match config.remove("size") {
            Some(size) => match size.into_str() {
                Ok(size) => Some(size),
                Err(_e) => None,
            },
            None => None,
        };
        BaseSettings { color, icon, size }
    }
}

#[derive(Clone, Debug)]
pub struct TitleSettings {
    pub app_id: Option<Vec<String>>,
    pub base: BaseSettings,
}

impl TitleSettings {
    pub fn new(mut config: HashMap<String, Value>) -> TitleSettings {
        let app_id: Option<Vec<String>> = match config.remove("app_id") {
            Some(app_id) => {
                match app_id.try_into() {
                    Ok(app_id) => Some(app_id),
                    Err(_e) => None,
                }
            }
            None => None,
        };
        let base = BaseSettings::new(config);
        TitleSettings { app_id, base }
    }
}

#[derive(Clone, Debug)]
pub struct Settings {
    pub global: GlobalSettings,
    pub title: HashMap<String, TitleSettings>,
    pub app_id: HashMap<String, BaseSettings>,
}

static DEFAULT_SETTINGS: &str = include_str!(r"./config.toml");

pub fn get_settings() -> Result<Settings, ConfigError> {
    let xdg_dirs = BaseDirectories::with_prefix("swaycons").unwrap();
    let config_path = xdg_dirs
        .place_config_file("config.toml").unwrap();
    let mut settings = Config::new();
    settings.merge(File::from_str(DEFAULT_SETTINGS, FileFormat::Toml))?;
    settings.merge(File::from(config_path).required(false))?;
    let global_map: HashMap<String, String> = settings.get("global").unwrap();
    let global = GlobalSettings::from(global_map);
    let title_config: HashMap<String, HashMap<String, Value>> = settings.get("title").unwrap();
    let mut title = HashMap::new();
    for (key, value) in title_config {
        title.insert(key, TitleSettings::new(value));
    }
    let mut app_id = HashMap::new();
    let app_id_config: HashMap<String, HashMap<String, Value>> = settings.get("app_id").unwrap();
    for (key, value) in app_id_config {
        app_id.insert(key, BaseSettings::new(value));
    }
    Ok(Settings {
        global,
        title,
        app_id
    })
}