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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
use crate::serde_str::{deserialize_from_str, serialize_to_str};
use derive_more::{Display, Error, From};
use serde::{de::Deserializer, ser::Serializer, Deserialize, Serialize};
use std::{
fmt,
net::{IpAddr, Ipv4Addr, Ipv6Addr},
str::FromStr,
};
#[derive(Clone, Debug, From, Display, Hash, PartialEq, Eq)]
pub enum Host {
Ipv4(Ipv4Addr),
Ipv6(Ipv6Addr),
Name(String),
}
impl Host {
pub const fn is_global(&self) -> bool {
match self {
Self::Ipv4(x) => {
!(x.is_broadcast()
|| x.is_documentation()
|| x.is_link_local()
|| x.is_loopback()
|| x.is_private()
|| x.is_unspecified())
}
Self::Ipv6(x) => {
x.is_multicast() && (x.segments()[0] & 0x000f == 14)
}
Self::Name(_) => false,
}
}
pub const fn is_ipv4(&self) -> bool {
matches!(self, Self::Ipv4(_))
}
pub const fn is_ipv6(&self) -> bool {
matches!(self, Self::Ipv6(_))
}
pub const fn is_name(&self) -> bool {
matches!(self, Self::Name(_))
}
}
impl From<IpAddr> for Host {
fn from(addr: IpAddr) -> Self {
match addr {
IpAddr::V4(x) => Self::Ipv4(x),
IpAddr::V6(x) => Self::Ipv6(x),
}
}
}
#[derive(Copy, Clone, Debug, Error, Hash, PartialEq, Eq)]
pub enum HostParseError {
EmptyLabel,
EndsWithHyphen,
EndsWithPeriod,
InvalidLabel,
LargeLabel,
LargeName,
StartsWithHyphen,
StartsWithPeriod,
}
impl HostParseError {
pub const fn into_static_str(self) -> &'static str {
match self {
Self::EmptyLabel => "Hostname cannot have an empty label",
Self::EndsWithHyphen => "Hostname cannot end with hyphen ('-')",
Self::EndsWithPeriod => "Hostname cannot end with period ('.')",
Self::InvalidLabel => "Hostname label can only be a-zA-Z0-9 or hyphen ('-')",
Self::LargeLabel => "Hostname label larger cannot be larger than 63 characters",
Self::LargeName => "Hostname cannot be larger than 253 characters",
Self::StartsWithHyphen => "Hostname cannot start with hyphen ('-')",
Self::StartsWithPeriod => "Hostname cannot start with period ('.')",
}
}
}
impl fmt::Display for HostParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.into_static_str())
}
}
impl FromStr for Host {
type Err = HostParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if let Ok(x) = s.parse::<Ipv4Addr>() {
return Ok(Self::Ipv4(x));
} else if let Ok(x) = s.parse::<Ipv6Addr>() {
return Ok(Self::Ipv6(x));
}
if s.is_empty() {
return Err(HostParseError::InvalidLabel);
}
let mut label_size_cnt = 0;
let mut last_char = None;
for (i, c) in s.char_indices() {
if i >= 253 {
return Err(HostParseError::LargeName);
}
if i == 0 && c == '.' {
return Err(HostParseError::StartsWithPeriod);
} else if i == 0 && c == '-' {
return Err(HostParseError::StartsWithHyphen);
}
if c.is_alphanumeric() {
label_size_cnt += 1;
if label_size_cnt > 63 {
return Err(HostParseError::LargeLabel);
}
} else if c == '.' {
if label_size_cnt == 0 {
return Err(HostParseError::EmptyLabel);
}
label_size_cnt = 0;
} else if c != '-' {
return Err(HostParseError::InvalidLabel);
}
last_char = Some(c);
}
if last_char == Some('.') {
return Err(HostParseError::EndsWithPeriod);
} else if last_char == Some('-') {
return Err(HostParseError::EndsWithHyphen);
}
Ok(Self::Name(s.to_string()))
}
}
impl PartialEq<str> for Host {
fn eq(&self, other: &str) -> bool {
match self {
Self::Ipv4(x) => x.to_string() == other,
Self::Ipv6(x) => x.to_string() == other,
Self::Name(x) => x == other,
}
}
}
impl<'a> PartialEq<&'a str> for Host {
fn eq(&self, other: &&'a str) -> bool {
match self {
Self::Ipv4(x) => x.to_string() == *other,
Self::Ipv6(x) => x.to_string() == *other,
Self::Name(x) => x == other,
}
}
}
impl Serialize for Host {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serialize_to_str(self, serializer)
}
}
impl<'de> Deserialize<'de> for Host {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserialize_from_str(deserializer)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn display_should_output_ipv4_correctly() {
let host = Host::Ipv4(Ipv4Addr::LOCALHOST);
assert_eq!(host.to_string(), "127.0.0.1");
}
#[test]
fn display_should_output_ipv6_correctly() {
let host = Host::Ipv6(Ipv6Addr::LOCALHOST);
assert_eq!(host.to_string(), "::1");
}
#[test]
fn display_should_output_hostname_verbatim() {
let host = Host::Name("localhost".to_string());
assert_eq!(host.to_string(), "localhost");
}
#[test]
fn from_str_should_fail_if_str_is_empty() {
let err = "".parse::<Host>().unwrap_err();
assert_eq!(err, HostParseError::InvalidLabel);
}
#[test]
fn from_str_should_fail_if_str_is_larger_than_253_characters() {
let long_name = format!(
"{}.{}.{}.{}",
"a".repeat(63),
"a".repeat(63),
"a".repeat(63),
"a".repeat(62)
);
let err = long_name.parse::<Host>().unwrap_err();
assert_eq!(err, HostParseError::LargeName);
}
#[test]
fn from_str_should_fail_if_str_starts_with_period() {
let err = ".localhost".parse::<Host>().unwrap_err();
assert_eq!(err, HostParseError::StartsWithPeriod);
}
#[test]
fn from_str_should_fail_if_str_ends_with_period() {
let err = "localhost.".parse::<Host>().unwrap_err();
assert_eq!(err, HostParseError::EndsWithPeriod);
}
#[test]
fn from_str_should_fail_if_str_starts_with_hyphen() {
let err = "-localhost".parse::<Host>().unwrap_err();
assert_eq!(err, HostParseError::StartsWithHyphen);
}
#[test]
fn from_str_should_fail_if_str_ends_with_hyphen() {
let err = "localhost-".parse::<Host>().unwrap_err();
assert_eq!(err, HostParseError::EndsWithHyphen);
}
#[test]
fn from_str_should_fail_if_str_has_a_label_larger_than_63_characters() {
let long_label = format!("{}.com", "a".repeat(64));
let err = long_label.parse::<Host>().unwrap_err();
assert_eq!(err, HostParseError::LargeLabel);
}
#[test]
fn from_str_should_fail_if_str_has_empty_label() {
let err = "example..com".parse::<Host>().unwrap_err();
assert_eq!(err, HostParseError::EmptyLabel);
}
#[test]
fn from_str_should_fail_if_str_has_invalid_label() {
let err = "www.exa_mple.com".parse::<Host>().unwrap_err();
assert_eq!(err, HostParseError::InvalidLabel);
}
#[test]
fn from_str_should_succeed_if_valid_ipv4_address() {
let host = "127.0.0.1".parse::<Host>().unwrap();
assert_eq!(host, Host::Ipv4(Ipv4Addr::new(127, 0, 0, 1)));
}
#[test]
fn from_str_should_succeed_if_valid_ipv6_address() {
let host = "::1".parse::<Host>().unwrap();
assert_eq!(host, Host::Ipv6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)));
}
#[test]
fn from_str_should_succeed_if_valid_hostname() {
let host = "localhost".parse::<Host>().unwrap();
assert_eq!(host, Host::Name("localhost".to_string()));
let host = "example.com".parse::<Host>().unwrap();
assert_eq!(host, Host::Name("example.com".to_string()));
let host = "w-w-w.example.com".parse::<Host>().unwrap();
assert_eq!(host, Host::Name("w-w-w.example.com".to_string()));
let host = "w3.example.com".parse::<Host>().unwrap();
assert_eq!(host, Host::Name("w3.example.com".to_string()));
let host = "3.example.com".parse::<Host>().unwrap();
assert_eq!(host, Host::Name("3.example.com".to_string()));
}
}