pata 0.0.1

A fast, lightweight, and evidence-driven email validation library for Rust
Documentation
use std::sync::LazyLock;

use fst::Set;

const DISPOSABLE_BYTES: &[u8] = include_bytes!("disposable.fst");
const FREE_BYTES: &[u8] = include_bytes!("free.fst");

static DISPOSABLE: LazyLock<Set<&'static [u8]>> = LazyLock::new(|| snapshot(DISPOSABLE_BYTES));
static FREE: LazyLock<Set<&'static [u8]>> = LazyLock::new(|| snapshot(FREE_BYTES));

pub(super) fn is_disposable(domain: &str) -> bool {
    let mut candidate = domain;
    loop {
        if DISPOSABLE.contains(candidate) {
            return true;
        }
        let Some((_, parent)) = candidate.split_once('.') else {
            return false;
        };
        if !parent.contains('.') {
            return false;
        }
        candidate = parent;
    }
}

pub(super) fn is_free(domain: &str) -> bool {
    FREE.contains(domain)
}

#[cfg(test)]
pub(super) fn disposable_count() -> usize {
    DISPOSABLE.len()
}

#[cfg(test)]
pub(super) fn free_count() -> usize {
    FREE.len()
}

fn snapshot(bytes: &'static [u8]) -> Set<&'static [u8]> {
    Set::new(bytes).expect("checked-in classifier snapshot is valid")
}