hex_patch/app/settings/
settings.rs

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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#![allow(clippy::module_inception)]
use std::{
    collections::HashMap,
    io,
    path::{Path, PathBuf},
};

use ratatui::style::Style;
use serde::de::Visitor;
use termbg::Theme;

use super::{
    app_settings::AppSettings, color_settings::ColorSettings, key_settings::KeySettings,
    settings_value::SettingsValue,
};

#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub struct Settings {
    pub color: ColorSettings,
    pub key: KeySettings,
    pub app: AppSettings,
    pub custom: HashMap<String, SettingsValue>,
}

impl Settings {
    pub fn load(path: Option<&Path>, terminal_theme: Theme) -> Result<Settings, io::Error> {
        let path = match path {
            Some(path) => path.to_path_buf(),
            None => Self::get_default_settings_path().ok_or(io::Error::new(
                io::ErrorKind::Other,
                "Could not get default settings path",
            ))?,
        };

        if !path.exists() {
            return Err(io::Error::new(
                io::ErrorKind::NotFound,
                "Settings file not found",
            ));
        }

        let settings = std::fs::read_to_string(&path)?;

        let mut deserializer = serde_json::Deserializer::from_str(&settings);

        Ok(
            match Settings::custom_deserialize(&mut deserializer, terminal_theme) {
                Ok(settings) => settings,
                Err(e) => {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidData,
                        format!("Could not parse settings file: {}", e),
                    ))
                }
            },
        )
    }

    fn get_default_settings_path() -> Option<PathBuf> {
        let config = dirs::config_dir()?;
        Some(config.join("HexPatch").join("settings.json"))
    }

    pub fn load_or_create(path: Option<&Path>, terminal_theme: Theme) -> Result<Settings, String> {
        match Self::load(path, terminal_theme) {
            Ok(settings) => Ok(settings),
            Err(e) => {
                if e.kind() != io::ErrorKind::NotFound {
                    Err(format!("Could not load settings: {}", e))
                } else {
                    let settings = Settings::empty(terminal_theme);
                    if path.is_some() {
                        settings
                            .save(path)
                            .ok_or(format!("Could not save default settings: {}", e))?;
                    }
                    Ok(settings)
                }
            }
        }
    }

    pub fn save(&self, path: Option<&Path>) -> Option<()> {
        let path = match path {
            Some(path) => path.to_path_buf(),
            None => Self::get_default_settings_path()?,
        };

        let settings = serde_json::to_string_pretty(self).ok()?;
        std::fs::create_dir_all(path.parent()?).ok()?;
        std::fs::write(&path, settings).ok()?;
        Some(())
    }

    pub fn empty(terminal_theme: Theme) -> Self {
        Self {
            color: ColorSettings::get_default_theme(terminal_theme),
            key: KeySettings::default(),
            app: AppSettings::default(),
            custom: HashMap::new(),
        }
    }
}

struct SettingsVisitor {
    theme: Theme,
}

impl<'de> Visitor<'de> for SettingsVisitor {
    type Value = Settings;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        formatter.write_str("a valid Settings struct")
    }

    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
    where
        A: serde::de::MapAccess<'de>,
    {
        let mut color_settings: Option<HashMap<String, Style>> = None;
        let mut key_settings: Option<KeySettings> = None;
        let mut app_settings: Option<AppSettings> = None;
        let mut custom_settings: Option<HashMap<String, SettingsValue>> = None;

        while let Some(key) = map.next_key()? {
            match key {
                "color" => {
                    if color_settings.is_some() {
                        return Err(serde::de::Error::duplicate_field("color"));
                    }
                    color_settings = Some(map.next_value()?);
                }
                "key" => {
                    if key_settings.is_some() {
                        return Err(serde::de::Error::duplicate_field("key"));
                    }
                    key_settings = Some(map.next_value()?);
                }
                "app" => {
                    if app_settings.is_some() {
                        return Err(serde::de::Error::duplicate_field("app"));
                    }
                    app_settings = Some(map.next_value()?);
                }
                "custom" => {
                    if custom_settings.is_some() {
                        return Err(serde::de::Error::duplicate_field("custom"));
                    }
                    custom_settings = Some(map.next_value()?);
                }
                _ => {
                    return Err(serde::de::Error::unknown_field(
                        key,
                        &["color", "key", "app", "custom"],
                    ));
                }
            }
        }
        let key_settings = key_settings.unwrap_or_default();
        let app_settings = app_settings.unwrap_or_default();
        let custom_settings = custom_settings.unwrap_or_default();
        let color_settings = ColorSettings::from_map(
            &color_settings.unwrap_or_default(),
            &app_settings,
            self.theme,
        )
        .map_err(serde::de::Error::custom)?;

        Ok(Self::Value {
            color: color_settings,
            key: key_settings,
            app: app_settings,
            custom: custom_settings,
        })
    }
}

impl Settings {
    fn custom_deserialize<'de, D>(deserializer: D, theme: Theme) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        deserializer.deserialize_map(SettingsVisitor { theme })
    }
}

impl Default for Settings {
    fn default() -> Self {
        Self {
            color: ColorSettings::get_default_theme(Theme::Dark),
            key: KeySettings::default(),
            app: AppSettings::default(),
            custom: HashMap::new(),
        }
    }
}

#[cfg(test)]
mod test {
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
    use ratatui::style::{Color, Style};

    use super::*;

    #[test]
    fn test_settings_load() {
        let settings = Settings::load(Some(Path::new("test/default_settings.json")), Theme::Dark);
        if let Err(e) = settings {
            panic!("Could not load settings: {}", e);
        }
        assert_eq!(settings.unwrap(), Settings::default());
    }

    #[test]
    fn test_settings_partial_load() {
        let settings = Settings::load(
            Some(Path::new("test/partial_default_settings.json")),
            Theme::Dark,
        );
        if let Err(e) = settings {
            panic!("Could not load settings: {}", e);
        }
        assert_eq!(settings.unwrap(), Settings::default());
    }

    #[test]
    fn test_settings_load_custom() {
        let settings = Settings::load(Some(Path::new("test/custom_settings.json")), Theme::Dark);
        if let Err(e) = settings {
            panic!("Could not load settings: {}", e);
        }
        let mut expected = Settings::default();
        expected
            .custom
            .insert("plugin1.value1".to_string(), SettingsValue::from("value1"));
        expected
            .custom
            .insert("plugin1.value2".to_string(), SettingsValue::from(2));
        expected
            .custom
            .insert("plugin2.value1".to_string(), SettingsValue::from(3.0));
        expected
            .custom
            .insert("plugin2.value2".to_string(), SettingsValue::from(true));
        expected.custom.insert(
            "plugin3.value1".to_string(),
            SettingsValue::from(Style::default().fg(Color::Red)),
        );
        expected.custom.insert(
            "plugin3.value2".to_string(),
            SettingsValue::from(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)),
        );
    }
}