diff --git a/testing/mozbase/rust/mozprofile/src/preferences.rs b/testing/mozbase/rust/mozprofile/src/preferences.rs index 573610ed8307..619b708fee11 100644 --- a/testing/mozbase/rust/mozprofile/src/preferences.rs +++ b/testing/mozbase/rust/mozprofile/src/preferences.rs @@ -1,13 +1,11 @@ use std::collections::BTreeMap; -use std::string; -use std::convert::From; pub type Preferences = BTreeMap; #[derive(Debug, PartialEq, Clone)] pub enum PrefValue { Bool(bool), - String(string::String), + String(String), Int(i64), } @@ -29,32 +27,102 @@ impl From<&'static str> for PrefValue { } } +impl From for PrefValue { + fn from(value: i8) -> Self { + PrefValue::Int(value.into()) + } +} + +impl From for PrefValue { + fn from(value: u8) -> Self { + PrefValue::Int(value.into()) + } +} + +impl From for PrefValue { + fn from(value: i16) -> Self { + PrefValue::Int(value.into()) + } +} + +impl From for PrefValue { + fn from(value: u16) -> Self { + PrefValue::Int(value.into()) + } +} + +impl From for PrefValue { + fn from(value: i32) -> Self { + PrefValue::Int(value.into()) + } +} + +impl From for PrefValue { + fn from(value: u32) -> Self { + PrefValue::Int(value.into()) + } +} + impl From for PrefValue { fn from(value: i64) -> Self { PrefValue::Int(value) } } +// Implementing From for PrefValue wouldn't be safe +// because it might overflow. + #[derive(Debug, PartialEq, Clone)] pub struct Pref { pub value: PrefValue, - pub sticky: bool + pub sticky: bool, } impl Pref { pub fn new(value: T) -> Pref - where T: Into { + where + T: Into, + { Pref { value: value.into(), - sticky: false + sticky: false, } } pub fn new_sticky(value: T) -> Pref - where T: Into { + where + T: Into, + { Pref { value: value.into(), - sticky: true + 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)); + } +}