| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
use std::collections::BTreeMap; |
| 6 |
|
| 7 |
pub type Preferences = BTreeMap<String, Pref>; |
| 8 |
|
| 9 |
#[derive(Debug, PartialEq, Clone)] |
| 10 |
pub enum PrefValue { |
| 11 |
Bool(bool), |
| 12 |
String(String), |
| 13 |
Int(i64), |
| 14 |
} |
| 15 |
|
| 16 |
impl From<bool> for PrefValue { |
| 17 |
fn from(value: bool) -> Self { |
| 18 |
PrefValue::Bool(value) |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
impl From<String> for PrefValue { |
| 23 |
fn from(value: String) -> Self { |
| 24 |
PrefValue::String(value) |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
impl From<&'static str> for PrefValue { |
| 29 |
fn from(value: &'static str) -> Self { |
| 30 |
PrefValue::String(value.into()) |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
impl From<i8> for PrefValue { |
| 35 |
fn from(value: i8) -> Self { |
| 36 |
PrefValue::Int(value.into()) |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
impl From<u8> for PrefValue { |
| 41 |
fn from(value: u8) -> Self { |
| 42 |
PrefValue::Int(value.into()) |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
impl From<i16> for PrefValue { |
| 47 |
fn from(value: i16) -> Self { |
| 48 |
PrefValue::Int(value.into()) |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
impl From<u16> for PrefValue { |
| 53 |
fn from(value: u16) -> Self { |
| 54 |
PrefValue::Int(value.into()) |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
impl From<i32> for PrefValue { |
| 59 |
fn from(value: i32) -> Self { |
| 60 |
PrefValue::Int(value.into()) |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
impl From<u32> for PrefValue { |
| 65 |
fn from(value: u32) -> Self { |
| 66 |
PrefValue::Int(value.into()) |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
impl From<i64> for PrefValue { |
| 71 |
fn from(value: i64) -> Self { |
| 72 |
PrefValue::Int(value) |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
// Implementing From<u64> for PrefValue wouldn't be safe |
| 77 |
// because it might overflow. |
| 78 |
|
| 79 |
#[derive(Debug, PartialEq, Clone)] |
| 80 |
pub struct Pref { |
| 81 |
pub value: PrefValue, |
| 82 |
pub sticky: bool, |
| 83 |
} |
| 84 |
|
| 85 |
impl Pref { |
| 86 |
/// Create a new preference with `value`. |
| 87 |
pub fn new<T>(value: T) -> Pref |
| 88 |
where |
| 89 |
T: Into<PrefValue>, |
| 90 |
{ |
| 91 |
Pref { |
| 92 |
value: value.into(), |
| 93 |
sticky: false, |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/// Create a new sticky, or locked, preference with `value`. |
| 98 |
/// These cannot be changed by the user in `about:config`. |
| 99 |
pub fn new_sticky<T>(value: T) -> Pref |
| 100 |
where |
| 101 |
T: Into<PrefValue>, |
| 102 |
{ |
| 103 |
Pref { |
| 104 |
value: value.into(), |
| 105 |
sticky: true, |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
#[cfg(test)] |
| 111 |
mod test { |
| 112 |
use super::PrefValue; |
| 113 |
|
| 114 |
#[test] |
| 115 |
fn test_bool() { |
| 116 |
assert_eq!(PrefValue::from(true), PrefValue::Bool(true)); |
| 117 |
} |
| 118 |
|
| 119 |
#[test] |
| 120 |
fn test_string() { |
| 121 |
assert_eq!(PrefValue::from("foo"), PrefValue::String("foo".to_string())); |
| 122 |
assert_eq!( |
| 123 |
PrefValue::from("foo".to_string()), |
| 124 |
PrefValue::String("foo".to_string()) |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
#[test] |
| 129 |
fn test_int() { |
| 130 |
assert_eq!(PrefValue::from(42i8), PrefValue::Int(42i64)); |
| 131 |
assert_eq!(PrefValue::from(42u8), PrefValue::Int(42i64)); |
| 132 |
assert_eq!(PrefValue::from(42i16), PrefValue::Int(42i64)); |
| 133 |
assert_eq!(PrefValue::from(42u16), PrefValue::Int(42i64)); |
| 134 |
assert_eq!(PrefValue::from(42i32), PrefValue::Int(42i64)); |
| 135 |
assert_eq!(PrefValue::from(42u32), PrefValue::Int(42i64)); |
| 136 |
assert_eq!(PrefValue::from(42i64), PrefValue::Int(42i64)); |
| 137 |
} |
| 138 |
} |