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
use super::{Value, BaseValue, InvalidValue};

define_value!(BoolValue, bool);


impl std::str::FromStr for BoolValue {
  type Err = InvalidValue;

  fn from_str(s: &str) -> Result<Self, Self::Err> {
    match &s.to_lowercase()[..] {
      "true" => Ok(BoolValue::new(true)),
      "false" => Ok(BoolValue::new(false)),
      _ => Err(InvalidValue::WrongValue),
    }
  }
}

#[cfg(test)]
mod tests {
  use super::{BoolValue, InvalidValue};

  #[test]
  fn from_str() {
    let true_val = "tRuE".parse::<BoolValue>().unwrap();
    assert_eq!(*true_val.val(), true);

    let false_val = "FaLse".parse::<BoolValue>().unwrap();
    assert_eq!(*false_val.val(), false);

    let bad_val_result = "hiya".parse::<BoolValue>();
    assert_eq!(bad_val_result, Err(InvalidValue::WrongValue));
  }
}