use std::borrow::Cow;
use std::sync::OnceLock;
use php_std::value;
use serde_json::Value;
mod table {
include!("table.rs");
}
include!(concat!(env!("OUT_DIR"), "/tables.rs"));
#[derive(Debug)]
pub struct DomainList {
source: Cow<'static, [u8]>,
table: Cow<'static, [u32]>,
entries: usize,
numeric: OnceLock<Vec<String>>,
}
impl Default for DomainList {
fn default() -> Self {
Self::from_source(Vec::new())
}
}
impl DomainList {
const fn embedded(source: &'static str, table: &'static [u32], entries: usize) -> Self {
Self {
source: Cow::Borrowed(source.as_bytes()),
table: Cow::Borrowed(table),
entries,
numeric: OnceLock::new(),
}
}
fn from_source(source: Vec<u8>) -> Self {
let (table, entries) = table::build(&source);
Self { source: Cow::Owned(source), table: Cow::Owned(table), entries, numeric: OnceLock::new() }
}
pub fn contains(&self, domain: &[u8]) -> bool {
if table::contains(&self.source, &self.table, domain) {
return true;
}
if value::numeric_bytes_ex(domain).is_none() {
return false;
}
let numeric = self.numeric.get_or_init(|| {
self.source
.split(|&b| b == b'\n')
.filter_map(|entry| std::str::from_utf8(entry).ok())
.filter(|entry| value::numeric_str(entry).is_some())
.map(str::to_owned)
.collect()
});
if numeric.is_empty() {
return false;
}
let needle = Value::String(String::from_utf8_lossy(domain).into_owned());
numeric.iter().any(|entry| value::loose_eq(&needle, &Value::String(entry.clone())))
}
pub fn len(&self) -> usize {
self.entries
}
pub fn is_empty(&self) -> bool {
self.entries == 0
}
}
impl<'a> FromIterator<&'a str> for DomainList {
fn from_iter<I: IntoIterator<Item = &'a str>>(iter: I) -> Self {
let mut source = Vec::new();
for domain in iter {
source.extend_from_slice(domain.as_bytes());
source.push(b'\n');
}
Self::from_source(source)
}
}
#[derive(Debug, Default)]
pub struct Lists {
pub free: DomainList,
pub disposable: DomainList,
}
static GLOBAL: Lists = Lists::embedded();
impl Lists {
pub const FREE: &'static str = include_str!("../data/free-domains.txt");
pub const DISPOSABLE: &'static str = include_str!("../data/disposable-domains.txt");
pub fn parse(source: &str) -> DomainList {
DomainList::from_source(source.as_bytes().to_vec())
}
pub const fn embedded() -> Self {
Self {
free: DomainList::embedded(Self::FREE, &FREE_TABLE, FREE_ENTRIES),
disposable: DomainList::embedded(Self::DISPOSABLE, &DISPOSABLE_TABLE, DISPOSABLE_ENTRIES),
}
}
pub fn global() -> &'static Self {
&GLOBAL
}
pub fn is_disposable(&self, domain: &[u8], registerable: &[u8]) -> bool {
self.disposable.contains(domain) || (!registerable.is_empty() && self.disposable.contains(registerable))
}
pub fn is_on_free_list(&self, domain: &[u8]) -> bool {
self.free.contains(domain)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse() {
let list = Lists::parse("gmail.com\nit's.com\n1000\n");
assert_eq!(list.len(), 3);
assert!(list.contains(b"gmail.com") && list.contains(b"it's.com"));
assert!(list.contains(b"1e3") && list.contains(b" 1000") && !list.contains(b"1e4"));
assert!(!list.contains(b"GMAIL.COM"));
}
#[test]
fn embedded_lists_parse_whole() {
let lists = Lists::embedded();
for (list, source) in [(&lists.free, Lists::FREE), (&lists.disposable, Lists::DISPOSABLE)] {
let lines = source.lines().count();
assert!(source.ends_with('\n') && !source.lines().any(str::is_empty));
assert_eq!(list.len(), lines, "every line is one distinct domain");
assert!(lines > 30_000, "the list has {lines} domains");
}
assert!(lists.is_on_free_list(b"gmail.com") && lists.is_disposable(b"x.mailinator.com", b"mailinator.com"));
}
#[test]
fn embedded_table_finds_every_entry() {
let lists = Lists::global();
for (list, source) in [(&lists.free, Lists::FREE), (&lists.disposable, Lists::DISPOSABLE)] {
for domain in source.lines() {
assert!(list.contains(domain.as_bytes()), "{domain} is on its list");
assert!(
!list.contains(format!("{domain}x").as_bytes())
|| source.lines().any(|l| l == format!("{domain}x"))
);
assert!(
!list.contains(format!("x{domain}").as_bytes())
|| source.lines().any(|l| l == format!("x{domain}"))
);
}
assert!(!list.contains(b"") && !list.contains(b"\n") && !list.contains(b"not-a-listed-domain.invalid"));
}
}
#[test]
fn embedded_table_matches_a_parse() {
let lists = Lists::global();
for (list, source) in [(&lists.free, Lists::FREE), (&lists.disposable, Lists::DISPOSABLE)] {
let parsed = Lists::parse(source);
assert_eq!(list.table.as_ref(), parsed.table.as_ref());
assert_eq!(list.len(), parsed.len());
}
}
}