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
use crate::error::{Kind, Result};
use crate::matcher;
use core::fmt;
use psl_types::{List, Type};
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct Name<'a> {
full: &'a str,
suffix: psl_types::Suffix<'a>,
}
impl<'a> Name<'a> {
pub(crate) fn parse<T: List<'a> + ?Sized>(list: &T, name: &'a str) -> Result<Name<'a>> {
let stripped = name.strip_suffix('.').unwrap_or(name);
if stripped.contains('.') {
matcher::is_domain_name(stripped)?;
} else {
matcher::is_label(stripped, true)?;
}
Ok(Self {
suffix: list.suffix(name.as_bytes()).ok_or(Kind::InvalidDomain)?,
full: name,
})
}
pub const fn as_str(&self) -> &str {
&self.full
}
pub fn root(&self) -> Option<&str> {
let suffix = self.suffix();
let offset = self
.full
.strip_suffix(suffix)?
.strip_suffix('.')?
.rfind('.')
.map(|x| x + 1)
.unwrap_or(0);
self.full.get(offset..)
}
pub fn suffix(&self) -> &str {
let offset = self.full.len() - self.suffix.as_bytes().len();
&self.full[offset..]
}
pub const fn has_known_suffix(&self) -> bool {
self.suffix.is_known()
}
pub const fn is_icann(&self) -> bool {
matches!(self.suffix.typ(), Some(Type::Icann))
}
pub const fn is_private(&self) -> bool {
matches!(self.suffix.typ(), Some(Type::Private))
}
}
impl fmt::Display for Name<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.full)
}
}