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
// Project: grippo
// File: src/domain.rs
// Purpose: A host name split at its public suffix, as borrowed spans
// Language: Rust
//
// License: Apache-2.0
// Copyright: (c) 2026 HYPERI PTY LIMITED
//! A host name split at its public suffix from the list psl compiles in:
//! the suffix, the registrable domain one label above it, and the
//! subdomain below that, each a span of the name as written.
/// Whether letters are folded to lower case before the lookup. The list is
/// lower case, so `AsGiven` reads `WWW.EXAMPLE.CO.UK`'s suffix as an unlisted
/// `UK`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Case {
/// ASCII letters folded before the lookup. The spans keep the case written.
Fold,
/// The bytes looked up as written.
AsGiven,
}
/// Which part of the list a suffix comes from.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Kind {
/// The ICANN section: `com`, `co.uk`, `in-addr.arpa`.
Icann,
/// The private section: `amazonaws.com`, `akamaized.net`.
Private,
/// Not on the list: the last label stands as the suffix.
Unlisted,
}
/// A host name split at its public suffix. Each span borrows the name.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DomainSplit<'a> {
/// The public suffix: `co.uk` in `a.example.co.uk`.
pub suffix: &'a str,
/// The suffix and one label: `example.co.uk`. `None` when the name is
/// the suffix alone.
pub registrable: Option<&'a str>,
/// What's left of the registrable domain, without its dot: `a`.
pub subdomain: Option<&'a str>,
/// Which part of the list the suffix comes from.
pub kind: Kind,
}
impl DomainSplit<'_> {
/// Whether the suffix is on the list.
#[must_use]
pub const fn known(&self) -> bool {
!matches!(self.kind, Kind::Unlisted)
}
}
/// The longest name a lookup folds, 253 bytes and a trailing dot.
const MAX_NAME: usize = 254;
/// `name` split at its public suffix, or `None` for an empty name, a name
/// longer than 253 bytes, or one that is only dots.
///
/// A trailing dot is left out of every span.
#[must_use]
pub fn split(name: &str, case: Case) -> Option<DomainSplit<'_>> {
let name = name.strip_suffix('.').unwrap_or(name);
if name.is_empty() || name.len() >= MAX_NAME || name.starts_with('.') {
return None;
}
let mut folded = [0_u8; MAX_NAME];
let bytes = match case {
Case::AsGiven => name.as_bytes(),
Case::Fold => {
let out = &mut folded[..name.len()];
out.copy_from_slice(name.as_bytes());
out.make_ascii_lowercase();
out
}
};
let suffix = psl::suffix(bytes)?;
let kind = match suffix.typ() {
Some(psl::Type::Icann) => Kind::Icann,
Some(psl::Type::Private) => Kind::Private,
None => Kind::Unlisted,
};
let tail = |len: usize| name.get(name.len().checked_sub(len)?..);
let suffix_span = tail(suffix.as_bytes().len())?;
let registrable = psl::domain(bytes).and_then(|d| tail(d.as_bytes().len()));
let subdomain = registrable.and_then(|r| {
let rest = name.len().checked_sub(r.len() + 1)?;
name.get(..rest).filter(|s| !s.is_empty())
});
Some(DomainSplit {
suffix: suffix_span,
registrable,
subdomain,
kind,
})
}
#[cfg(test)]
mod tests;