This library uses Mozilla's Public Suffix List to reliably determine the suffix of a domain name. This crate provides a dynamic list that can be updated at runtime. If you need a faster, though static list, please use the psl crate instead.
NB: v1 of this crate contained logic to validate domain names and email addresses. Since v2, this functionality was moved to the addr crate. This crate also no longer downloads the list for you.
Add this crate to your Cargo.toml
:
toml
[dependencies]
publicsuffix = "2"
```rust use publicsuffix::{Psl, List};
// the official list can be found at // https://publicsuffix.org/list/publicsuffixlist.dat let list: List = "<-- your public suffix here -->".parse()?;
let suffix = list.suffix(b"www.example.com")?; asserteq!(suffix.asbytes(), b"com"); assert_eq!(suffix.typ(), Some(Type::Icann));
let domain = list.domain(b"www.example.com")?; asserteq!(domain.asbytes(), b"example.com"); asserteq!(domain.suffix().asbytes(), b"com");
let domain = list.domain("www.食狮.中国".asbytes())?; asserteq!(domain.asbytes(), "食狮.中国".asbytes()); asserteq!(domain.suffix().asbytes(), "中国".as_bytes());
let domain = list.domain(b"www.xn--85x722f.xn--55qx5d.cn")?; asserteq!(domain.asbytes(), b"xn--85x722f.xn--55qx5d.cn"); asserteq!(domain.suffix().asbytes(), b"xn--55qx5d.cn");
let domain = list.domain(b"a.b.example.uk.com")?; asserteq!(domain.asbytes(), b"example.uk.com"); asserteq!(domain.suffix().asbytes(), b"uk.com");
let domain = list.domain(b"tcp.example.com.")?; asserteq!(domain.asbytes(), b"example.com."); asserteq!(domain.suffix().as_bytes(), b"com."); ```