grippo 0.1.2

Parse, match and scrub data at scale.
Documentation
// 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;