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
use std::borrow::{Borrow, Cow};
use super::{Value, BaseValue, InvalidValue};
use http::Uri;

#[derive(Debug, PartialEq, Clone)]
pub struct UriValue {
  val: Cow<'static, str>,
}

impl UriValue {
  pub fn try_new<STR>(val: STR) -> Result<Self, InvalidValue> 
      where STR: Into<Cow<'static, str>>
  {
    let val = val.into();
    Self::validate(&val)?;
    Ok(Self { val })
  }

  pub fn validate(val: &Cow<'static, str>) -> Result<(), InvalidValue> {
    let _uri: Uri = val.parse().map_err(|_e| InvalidValue::BadFormat)?;
    Ok(())
  }

  pub fn val(&self) -> &str {
    self.val.borrow()
  }

  pub fn uri_val(&self) -> Uri {
    self.val.parse::<Uri>().unwrap()
  }

  pub fn boxed(self) -> Box<dyn Value> {
    Box::new(self)
  }
}

define_value_impl!(UriValue);

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

  fn from_str(s: &str) -> Result<Self, Self::Err> {
    UriValue::try_new(s.to_owned())
  }
}


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


  const GOOD_URI:&str = "/hi";
  const BAD_URI: &str = "$$$å";

  #[test]
  fn test_good_uri() {
    let uri_value = UriValue::try_new(GOOD_URI).unwrap();
    assert_eq!(uri_value.val(), GOOD_URI);
  }

  #[test]
  fn test_bad_uri() {
    assert_eq!(UriValue::try_new(BAD_URI), Err(InvalidValue::BadFormat));
  }

  #[test]
  fn test_fromstr() {
    assert!(matches!(BAD_URI.parse::<UriValue>(), Err(_)));
    assert_eq!(GOOD_URI.parse::<UriValue>().unwrap(), UriValue::try_new(GOOD_URI).unwrap());
  }
}