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
use std::collections::BTreeMap;

/// Denotes the type for elements that serve as keys in settings files.
///
/// This type is an alias for keys, e.g. the part of a line of a settings file
/// in front of the first `=`. Even though this is just an alias to `String`
/// for now, this provides better type safety and lets the compiler know what
/// you intend to do with a particular variable. This type is used in the
/// complex type [`Settings`].
///
/// For an example how to use `Key` please refer to the [example section of `Settings`](crate::Settings#how-to-use-settings).
pub type Key = String;

/// Denotes the type for elements that serve as values in settings files.
///
/// This type is an alias for optional values, e.g. the part of a line of a settings
/// file after the first `=`. It's an `Option` because a settings line might end
/// with the first `=` which is when the value of that line would be `None`. As soon
/// as there is any contents after the first `=`, the value will be `Some(X)`, where `X`
/// represents that data.
///
/// For an example how to use `Value` please refer to the [example section of `Settings`](crate::Settings#how-to-use-settings).
pub type Value = Option<String>;

/// Denotes a set of settings as a simple sorted map of Key-Value pairs.
///
/// Each entry in this map consists of a key of type [`Key`] and a value of type [`Option<Value>`].
/// For more information about why values are wrapped in [`Option`] please refer to the
/// documentation of the type [`Value`].
///
/// ## How to use `Settings`
///
/// Since `Settings` is just an alias for [`BTreeMap`], you can construct them fairly easily from an array of tuples:
///
/// ```
/// // Create a new Settings object with 1 key-value pair "url=https://example.com"
/// let my_settings_one = renvy::Settings::from([("url".into(), Some("https://example.com".into()))]);
/// assert_eq!(my_settings_one.get("url").unwrap(), &Some("https://example.com".into()));
///
/// // Create a new Settings object with 1 key-value pair where the value is empty "url="
/// let my_settings_one = renvy::Settings::from([("url".into(), None)]);
/// assert_eq!(my_settings_one.get("url").unwrap(), &None);
/// ```
///
/// Besides that, [`crate::deserialize()`] will also return an instance of `Settings`:
///
/// ```
/// // Settings objects will also be returned from renvy::deserialize()
/// let my_settings_deserialized = renvy::deserialize("url=https://example.com".into());
/// assert_eq!(my_settings_deserialized.get("url".into()).unwrap(), &Some("https://example.com".into()));
/// ```
pub type Settings = BTreeMap<Key, Value>;

/// Merges two instances of [`Settings`] together so that the following rules are satisfied:
/// - all key-value pairs on `defaults` that are missing on `settings` will be added
/// - existing key-value pairs of `settings` retain their value
/// - if the parameter `clean` receives `Some(true)`, then any key-value pair on `settings`
///   which is missing from `defaults` will be removed
///
/// ## Examples
///
/// Default behaviour when `clean` is `None`. This is the same behaviour like passing
/// `Some(false)` explicitly.
///
/// ```
/// // "ssl" exists in both objects, it's "true" here
/// let settings = renvy::Settings::from([
///     ("url".into(), Some(String::from("https://example.com"))),
///     ("ssl".into(), Some(String::from("true")))
/// ]);
///
/// // "ssl" is "false" here
/// let defaults = renvy::Settings::from([
///     ("port".into(), None),
///     ("ssl".into(), Some(String::from("false")))
/// ]);
///
/// let merged = renvy::merge(settings, defaults, None);
///
/// // "ssl" remains "true", "port" is added, and "url" is left intact
/// assert_eq!(merged.get("url".into()).unwrap(), &Some(String::from("https://example.com")));
/// assert_eq!(merged.get("ssl".into()).unwrap(), &Some(String::from("true")));
/// assert_eq!(merged.get("port".into()).unwrap(), &None);
/// ```
///
/// Behaviour when `clean` is disabled with `Some(false)`: Extra keys in `settings`
/// remain untouched. This is the default behaviour that is also applied when `clean`
/// is empty (`None`).
///
/// ```
/// // "ssl" exists in both objects, it's "true" here
/// let settings = renvy::Settings::from([
///     ("url".into(), Some(String::from("https://example.com"))),
///     ("ssl".into(), Some(String::from("true")))
/// ]);
///
/// // "ssl" is "false" here
/// let defaults = renvy::Settings::from([
///     ("port".into(), None),
///     ("ssl".into(), Some(String::from("false")))
/// ]);
///
/// let merged = renvy::merge(settings, defaults, Some(false));
///
/// // "ssl" remains "true", "port" is added, and "url" is left intact
/// assert_eq!(merged.get("url".into()).unwrap(), &Some(String::from("https://example.com")));
/// assert_eq!(merged.get("ssl".into()).unwrap(), &Some(String::from("true")));
/// assert_eq!(merged.get("port".into()).unwrap(), &None);
/// ```
///
/// Behaviour when `clean` is enabled with `Some(true)`: Extra keys in `settings`
/// are being removed so that only keys that exist in `defaults` remain in `settings`.
///
/// ```
/// // "ssl" exists in both objects, it's "true" here
/// // "url" exists only in "settings".
/// let settings = renvy::Settings::from([
///     ("url".into(), Some(String::from("https://example.com"))),
///     ("ssl".into(), Some(String::from("true")))
/// ]);
///
/// // "ssl" is "false" here
/// let defaults = renvy::Settings::from([
///     ("port".into(), None),
///     ("ssl".into(), Some(String::from("false")))
/// ]);
///
/// let merged = renvy::merge(settings, defaults, Some(true));
///
/// // "ssl" remains "true", "port" is added, and "url" is removed
/// assert_eq!(merged.get("url".into()), None);
/// assert_eq!(merged.get("ssl".into()).unwrap(), &Some(String::from("true")));
/// assert_eq!(merged.get("port".into()).unwrap(), &None);
/// ```
///
pub fn merge(settings: Settings, defaults: Settings, clean: Option<bool>) -> Settings {
    let mut result: Settings = settings;
    let clean = if let Some(x) = clean { x } else { false };

    if clean {
        result.retain(|key, _| defaults.contains_key(key));
    }

    for default in defaults {
        let entry = result.entry(default.0);
        entry.or_insert(default.1);
    }

    result
}

#[cfg(test)]
mod test {
    use crate::merge;

    #[test]
    fn merge_adds_new_defaults() {
        let settings =
            merge::Settings::from([("domain".into(), Some("https://benjaminsattler.net".into()))]);

        let defaults = merge::Settings::from([("port".into(), Some("433".into()))]);

        let merged = merge::merge(settings, defaults, None);

        assert!(merged.get("port").is_some());
        assert_eq!(merged.get("port").unwrap(), &Some(String::from("433")));
    }

    #[test]
    fn merge_keeps_existing_settings_with_defaults() {
        let settings =
            merge::Settings::from([("domain".into(), Some("https://benjaminsattler.net".into()))]);

        let defaults = merge::Settings::from([("domain".into(), Some("https://example".into()))]);

        let merged = merge::merge(settings, defaults, None);

        assert!(merged.get("domain").is_some());
        assert_eq!(
            merged.get("domain").unwrap(),
            &Some(String::from("https://benjaminsattler.net"))
        );
    }

    #[test]
    fn merge_keeps_settings_without_defaults_if_cleaning_is_default() {
        let settings =
            merge::Settings::from([("domain".into(), Some("https://benjaminsattler.net".into()))]);

        let defaults = merge::Settings::from([("port".into(), Some("433".into()))]);

        let merged = merge::merge(settings, defaults, None);

        assert!(merged.get("domain").is_some());
        assert_eq!(
            merged.get("domain").unwrap(),
            &Some(String::from("https://benjaminsattler.net"))
        );
    }

    #[test]
    fn merge_keeps_settings_without_defaults_if_not_cleaning() {
        let settings =
            merge::Settings::from([("domain".into(), Some("https://benjaminsattler.net".into()))]);

        let defaults = merge::Settings::from([("port".into(), Some("433".into()))]);

        let merged = merge::merge(settings, defaults, Some(false));

        assert!(merged.get("domain").is_some());
        assert_eq!(
            merged.get("domain").unwrap(),
            &Some(String::from("https://benjaminsattler.net"))
        );
    }

    #[test]
    fn merge_discards_settings_without_defaults_if_cleaning() {
        let settings =
            merge::Settings::from([("domain".into(), Some("https://benjaminsattler.net".into()))]);

        let defaults = merge::Settings::from([("port".into(), Some("433".into()))]);

        let merged = merge::merge(settings, defaults, Some(true));

        assert!(merged.get("domain").is_none());
    }
}