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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use anyhow::Context;
#[derive(Debug)]
pub struct ServiceDefinition {
hostname: String,
port: u16,
}
impl ServiceDefinition {
pub fn from_parts<T: ToString>(hostname: T, port: u16) -> Result<Self, anyhow::Error> {
let hostname = hostname.to_string();
trust_dns_resolver::Name::from_ascii(&hostname)
.map_err(anyhow::Error::from)
.context("invalid 'hostname'")?;
Ok(Self { hostname, port })
}
pub fn hostname(&self) -> &str {
&self.hostname
}
pub fn port(&self) -> u16 {
self.port
}
}
impl TryFrom<(&str, u16)> for ServiceDefinition {
type Error = anyhow::Error;
fn try_from((hostname, port): (&str, u16)) -> Result<Self, Self::Error> {
Self::from_parts(hostname, port)
}
}
impl TryFrom<(String, u16)> for ServiceDefinition {
type Error = anyhow::Error;
fn try_from((hostname, port): (String, u16)) -> Result<Self, Self::Error> {
Self::from_parts(hostname, port)
}
}
#[cfg(test)]
mod test {
use super::*;
use proptest::prop_compose;
prop_compose! {
fn valid_hostname()(s in "[a-z.0-9*A-Z]") -> String {
s
}
}
prop_compose! {
fn invalid_hostname()(s in "[^\\a-z.0-9*A-Z]+") -> String {
s
}
}
proptest::proptest! {
#[test]
fn valid_hostname_shall_succeed(hostname in valid_hostname()) {
proptest::prop_assert!(ServiceDefinition::from_parts(hostname, 5000).is_ok());
}
#[test]
fn invalid_hostname_shall_fail(hostname in invalid_hostname()) {
proptest::prop_assert!(ServiceDefinition::from_parts(hostname, 5000).is_err());
}
}
}