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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use std::collections::HashMap;
use std::fmt;
use std::str::FromStr;
use std::sync::RwLock;

use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub enum PrefValue {
    Float(f64),
    Int(i64),
    Str(String),
    Bool(bool),
    Array(Vec<PrefValue>),
    Missing,
}

impl PrefValue {
    pub fn as_str(&self) -> Option<&str> {
        if let PrefValue::Str(val) = self {
            Some(val)
        } else {
            None
        }
    }

    pub fn as_i64(&self) -> Option<i64> {
        if let PrefValue::Int(val) = self {
            Some(*val)
        } else {
            None
        }
    }

    pub fn as_f64(&self) -> Option<f64> {
        if let PrefValue::Float(val) = self {
            Some(*val)
        } else {
            None
        }
    }

    pub fn as_bool(&self) -> Option<bool> {
        if let PrefValue::Bool(val) = self {
            Some(*val)
        } else {
            None
        }
    }

    pub fn is_missing(&self) -> bool {
        matches!(self, PrefValue::Missing)
    }

    pub fn from_json_value(value: &Value) -> Option<Self> {
        match value {
            Value::Bool(b) => Some(PrefValue::Bool(*b)),
            Value::Number(n) if n.is_i64() => Some(PrefValue::Int(n.as_i64().unwrap())),
            Value::Number(n) if n.is_f64() => Some(PrefValue::Float(n.as_f64().unwrap())),
            Value::String(s) => Some(PrefValue::Str(s.to_owned())),
            _ => None,
        }
    }
}

impl FromStr for PrefValue {
    type Err = PrefError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s == "false" {
            Ok(PrefValue::Bool(false))
        } else if s == "true" {
            Ok(PrefValue::Bool(true))
        } else if let Ok(float) = s.parse::<f64>() {
            Ok(PrefValue::Float(float))
        } else if let Ok(integer) = s.parse::<i64>() {
            Ok(PrefValue::Int(integer))
        } else {
            Ok(PrefValue::from(s))
        }
    }
}

macro_rules! impl_pref_from {
    ($($t: ty => $variant: path,)*) => {
        $(
            impl From<$t> for PrefValue {
                fn from(other: $t) -> Self {
                    $variant(other.into())
                }
            }
        )+
        $(
            impl From<Option<$t>> for PrefValue {
                fn from(other: Option<$t>) -> Self {
                    other.map(|val| $variant(val.into())).unwrap_or(PrefValue::Missing)
                }
            }
        )+
    }
}

macro_rules! impl_from_pref {
    ($($variant: path => $t: ty,)*) => {
        $(
            impl From<PrefValue> for $t {
                #[allow(unsafe_code)]
                fn from(other: PrefValue) -> Self {
                    if let $variant(value) = other {
                        value.into()
                    } else {
                        panic!("Cannot convert {:?} to {:?}", other, std::any::type_name::<$t>())
                    }
                }
            }
        )+
        $(
            impl From<PrefValue> for Option<$t> {
                fn from(other: PrefValue) -> Self {
                    if let PrefValue::Missing = other {
                        None
                    } else {
                        Some(other.into())
                    }
                }
            }
        )+
    }
}

impl_pref_from! {
    f64 => PrefValue::Float,
    i64 => PrefValue::Int,
    String => PrefValue::Str,
    &str => PrefValue::Str,
    bool => PrefValue::Bool,
}

impl_from_pref! {
    PrefValue::Float => f64,
    PrefValue::Int => i64,
    PrefValue::Str => String,
    PrefValue::Bool => bool,
}

impl From<[f64; 4]> for PrefValue {
    fn from(other: [f64; 4]) -> PrefValue {
        PrefValue::Array(IntoIterator::into_iter(other).map(|v| v.into()).collect())
    }
}

impl From<PrefValue> for [f64; 4] {
    fn from(other: PrefValue) -> [f64; 4] {
        match other {
            PrefValue::Array(values) if values.len() == 4 => {
                let f = values.into_iter().map(Into::into).collect::<Vec<f64>>();
                if f.len() == 4 {
                    [f[0], f[1], f[2], f[3]]
                } else {
                    panic!(
                        "Cannot convert PrefValue to {:?}",
                        std::any::type_name::<[f64; 4]>()
                    )
                }
            },
            _ => panic!(
                "Cannot convert {:?} to {:?}",
                other,
                std::any::type_name::<[f64; 4]>()
            ),
        }
    }
}

#[derive(Debug)]
pub enum PrefError {
    NoSuchPref(String),
    InvalidValue(String),
    JsonParseErr(serde_json::error::Error),
}

impl fmt::Display for PrefError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            PrefError::NoSuchPref(s) | PrefError::InvalidValue(s) => f.write_str(s),
            PrefError::JsonParseErr(e) => e.fmt(f),
        }
    }
}

impl std::error::Error for PrefError {}

pub struct Accessor<P, V> {
    pub getter: Box<dyn Fn(&P) -> V + Sync + Send>,
    #[allow(clippy::type_complexity)]
    pub setter: Box<dyn Fn(&mut P, V) + Sync + Send>,
}

impl<P, V> Accessor<P, V> {
    pub fn new<G, S>(getter: G, setter: S) -> Self
    where
        G: Fn(&P) -> V + Sync + Send + 'static,
        S: Fn(&mut P, V) + Sync + Send + 'static,
    {
        Accessor {
            getter: Box::new(getter),
            setter: Box::new(setter),
        }
    }
}

pub struct Preferences<'m, P> {
    user_prefs: RwLock<P>,
    default_prefs: P,
    accessors: &'m HashMap<String, Accessor<P, PrefValue>>,
}

impl<'m, P: Clone> Preferences<'m, P> {
    /// Create a new `Preferences` object. The values provided in `default_prefs` are immutable and
    /// can always be restored using `reset` or `reset_all`.
    pub fn new(default_prefs: P, accessors: &'m HashMap<String, Accessor<P, PrefValue>>) -> Self {
        Self {
            user_prefs: RwLock::new(default_prefs.clone()),
            default_prefs,
            accessors,
        }
    }

    /// Access to the data structure holding the preference values.
    pub fn values(&self) -> &RwLock<P> {
        &self.user_prefs
    }

    /// Retrieve a preference using its key
    pub fn get(&self, key: &str) -> PrefValue {
        if let Some(accessor) = self.accessors.get(key) {
            let prefs = self.user_prefs.read().unwrap();
            (accessor.getter)(&prefs)
        } else {
            PrefValue::Missing
        }
    }

    /// Has the preference been modified from its original value?
    pub fn is_default(&self, key: &str) -> Result<bool, PrefError> {
        if let Some(accessor) = self.accessors.get(key) {
            let user = (accessor.getter)(&self.default_prefs);
            let default = (accessor.getter)(&self.user_prefs.read().unwrap());
            Ok(default == user)
        } else {
            Err(PrefError::NoSuchPref(String::from(key)))
        }
    }

    /// Creates an iterator over all keys and values
    pub fn iter(&self) -> impl Iterator<Item = (String, PrefValue)> + '_ {
        let prefs = self.user_prefs.read().unwrap();
        self.accessors
            .iter()
            .map(move |(k, accessor)| (k.clone(), (accessor.getter)(&prefs)))
    }

    /// Creates an iterator over all keys
    pub fn keys(&self) -> impl Iterator<Item = &'_ str> {
        self.accessors.keys().map(String::as_str)
    }

    fn set_inner<V>(&self, key: &str, prefs: &mut P, val: V) -> Result<(), PrefError>
    where
        V: Into<PrefValue>,
    {
        if let Some(accessor) = self.accessors.get(key) {
            (accessor.setter)(prefs, val.into());
            Ok(())
        } else {
            Err(PrefError::NoSuchPref(String::from(key)))
        }
    }

    /// Set a new value for a preference, using its key.
    pub fn set<V>(&self, key: &str, val: V) -> Result<(), PrefError>
    where
        V: Into<PrefValue>,
    {
        let mut prefs = self.user_prefs.write().unwrap();
        self.set_inner(key, &mut prefs, val)
    }

    pub fn set_all<M>(&self, values: M) -> Result<(), PrefError>
    where
        M: IntoIterator<Item = (String, PrefValue)>,
    {
        let mut prefs = self.user_prefs.write().unwrap();
        for (k, v) in values.into_iter() {
            self.set_inner(&k, &mut prefs, v)?;
        }
        Ok(())
    }

    pub fn reset(&self, key: &str) -> Result<PrefValue, PrefError> {
        if let Some(accessor) = self.accessors.get(key) {
            let mut prefs = self.user_prefs.write().unwrap();
            let old_pref = (accessor.getter)(&prefs);
            let default_pref = (accessor.getter)(&self.default_prefs);
            (accessor.setter)(&mut prefs, default_pref);
            Ok(old_pref)
        } else {
            Err(PrefError::NoSuchPref(String::from(key)))
        }
    }

    pub fn reset_all(&self) {
        *self.user_prefs.write().unwrap() = self.default_prefs.clone();
    }
}