#domain-name #domain #tld #psl #no-std #tldextract

no-std publicsuffix

Extract root domain and suffix from a domain name

36 stable releases

2.2.3 Oct 8, 2022
2.1.1 Sep 9, 2021
2.1.0 Mar 30, 2021
1.5.6 Feb 26, 2021
0.0.2 Dec 31, 2016

#60 in Web programming

Download history 93452/week @ 2023-12-11 80245/week @ 2023-12-18 43905/week @ 2023-12-25 70197/week @ 2024-01-01 87396/week @ 2024-01-08 87906/week @ 2024-01-15 90552/week @ 2024-01-22 97224/week @ 2024-01-29 86050/week @ 2024-02-05 103370/week @ 2024-02-12 124904/week @ 2024-02-19 128208/week @ 2024-02-26 116529/week @ 2024-03-04 114147/week @ 2024-03-11 109604/week @ 2024-03-18 100008/week @ 2024-03-25

445,032 downloads per month
Used in 779 crates (11 directly)

MIT/Apache

88KB
400 lines

PublicSuffix

A native Rust library for Mozilla's Public Suffix List

CI Latest Version Crates.io downloads Docs Minimum supported Rust version Maintenance License

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.

Setting Up

Add this crate to your Cargo.toml:

[dependencies]
publicsuffix = "2"

Examples

use publicsuffix::{Psl, List};

// the official list can be found at
// https://publicsuffix.org/list/public_suffix_list.dat
let list: List = "<-- your public suffix list here -->".parse()?;

let suffix = list.suffix(b"www.example.com")?;
assert_eq!(suffix, "com");
assert_eq!(suffix.typ(), Some(Type::Icann));

let domain = list.domain(b"www.example.com")?;
assert_eq!(domain, "example.com");
assert_eq!(domain.suffix(), "com");

let domain = list.domain("www.食狮.中国".as_bytes())?;
assert_eq!(domain, "食狮.中国");
assert_eq!(domain.suffix(), "中国");

let domain = list.domain(b"www.xn--85x722f.xn--55qx5d.cn")?;
assert_eq!(domain, "xn--85x722f.xn--55qx5d.cn");
assert_eq!(domain.suffix(), "xn--55qx5d.cn");

let domain = list.domain(b"a.b.example.uk.com")?;
assert_eq!(domain, "example.uk.com");
assert_eq!(domain.suffix(), "uk.com");

let domain = list.domain(b"_tcp.example.com.")?;
assert_eq!(domain, "example.com.");
assert_eq!(domain.suffix(), "com.");

Dependencies

~12–660KB
~16K SLoC