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
|
/* 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 http://mozilla.org/MPL/2.0/. */
use std::collections::BTreeMap;
pub type Preferences = BTreeMap<String, Pref>;
#[derive(Debug, PartialEq, Clone)]
pub enum PrefValue {
Bool(bool),
String(String),
Int(i64),
}
impl From<bool> for PrefValue {
fn from(value: bool) -> Self {
PrefValue::Bool(value)
}
}
impl From<String> for PrefValue {
fn from(value: String) -> Self {
PrefValue::String(value)
}
}
impl From<&'static str> for PrefValue {
fn from(value: &'static str) -> Self {
PrefValue::String(value.into())
}
}
impl From<i8> for PrefValue {
fn from(value: i8) -> Self {
PrefValue::Int(value.into())
}
}
impl From<u8> for PrefValue {
fn from(value: u8) -> Self {
PrefValue::Int(value.into())
}
}
impl From<i16> for PrefValue {
fn from(value: i16) -> Self {
PrefValue::Int(value.into())
}
}
impl From<u16> for PrefValue {
fn from(value: u16) -> Self {
PrefValue::Int(value.into())
}
}
impl From<i32> for PrefValue {
fn from(value: i32) -> Self {
PrefValue::Int(value.into())
}
}
impl From<u32> for PrefValue {
fn from(value: u32) -> Self {
PrefValue::Int(value.into())
}
}
impl From<i64> for PrefValue {
fn from(value: i64) -> Self {
PrefValue::Int(value)
}
}
// Implementing From<u64> for PrefValue wouldn't be safe
// because it might overflow.
#[derive(Debug, PartialEq, Clone)]
pub struct Pref {
pub value: PrefValue,
pub sticky: bool,
}
impl Pref {
/// Create a new preference with `value`.
pub fn new<T>(value: T) -> Pref
where
T: Into<PrefValue>,
{
Pref {
value: value.into(),
sticky: false,
}
}
/// Create a new sticky, or locked, preference with `value`.
/// These cannot be changed by the user in `about:config`.
pub fn new_sticky<T>(value: T) -> Pref
where
T: Into<PrefValue>,
{
Pref {
value: value.into(),
sticky: true,
}
}
}
#[cfg(test)]
mod test {
use super::PrefValue;
#[test]
fn test_bool() {
assert_eq!(PrefValue::from(true), PrefValue::Bool(true));
}
#[test]
fn test_string() {
assert_eq!(PrefValue::from("foo"), PrefValue::String("foo".to_string()));
assert_eq!(
PrefValue::from("foo".to_string()),
PrefValue::String("foo".to_string())
);
}
#[test]
fn test_int() {
assert_eq!(PrefValue::from(42i8), PrefValue::Int(42i64));
assert_eq!(PrefValue::from(42u8), PrefValue::Int(42i64));
assert_eq!(PrefValue::from(42i16), PrefValue::Int(42i64));
assert_eq!(PrefValue::from(42u16), PrefValue::Int(42i64));
assert_eq!(PrefValue::from(42i32), PrefValue::Int(42i64));
assert_eq!(PrefValue::from(42u32), PrefValue::Int(42i64));
assert_eq!(PrefValue::from(42i64), PrefValue::Int(42i64));
}
}
|