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
#[derive(Eq, PartialEq, Clone, Debug)]
pub struct HostMapping {
pub hostname: String,
pub address: IpAddr,
}
impl HostMapping {
pub fn new(hostname: &str, address: &IpAddr) -> HostMapping {
HostMapping{hostname: hostname.to_owned(),
address: address.to_owned(),}
}
}
impl_interpolatable_value! (HostMapping);
impl fmt::Display for HostMapping {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f , "{}:{}" , & self . hostname , & self . address)
}
}
impl FromStr for HostMapping {
type
Err
=
InvalidValueError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
lazy_static!{static ref HOST_ADDRESS : Regex = Regex :: new (
"^([^:]+):(.+)$" ) . unwrap ( ) ;
}
let caps =
try!(HOST_ADDRESS . captures ( s ) . ok_or_else (
|| { InvalidValueError :: new ( "host mapping" , s ) } ));
let addr: IpAddr =
try!(FromStr :: from_str ( caps . at ( 2 ) . unwrap ( ) ) .
map_err (
| _ | { InvalidValueError :: new ( "IP address" , s ) } ));
Ok(HostMapping::new(caps.at(1).unwrap(), &addr))
}
}
#[test]
fn host_mapping_supports_string_serialization() {
let localhost: IpAddr = FromStr::from_str("127.0.0.1").unwrap();
assert_eq!(HostMapping :: new ( "foo.example.com" , & localhost ) ,
HostMapping :: from_str ( "foo.example.com:127.0.0.1" ) .
unwrap ( ));
assert_eq!(HostMapping :: new ( "foo.example.com" , & localhost ) .
to_string ( ) , "foo.example.com:127.0.0.1");
}