| Crates.io | addr |
| lib.rs | addr |
| version | 0.15.6 |
| created_at | 2018-07-10 15:07:53.216206+00 |
| updated_at | 2022-08-31 12:12:58.478002+00 |
| description | A library for parsing domain names |
| homepage | |
| repository | https://github.com/addr-rs/addr |
| max_upload_size | |
| id | 73630 |
| size | 307,138 |
This library uses Mozilla's Public Suffix List to reliably parse domain names in Rust. It will reliably check if a domain has valid syntax. It also checks the length restrictions for each label, total number of labels and full length of domain name.
use addr::{parse_domain_name, parse_dns_name};
// You can find out the root domain
// or extension of any given domain name
let domain = parse_domain_name("www.example.com")?;
assert_eq!(domain.root(), Some("example.com"));
assert_eq!(domain.suffix(), "com");
assert_eq!(domain.prefix(), Some("www"));
let domain = parse_domain_name("www.食狮.中国")?;
assert_eq!(domain.root(), Some("食狮.中国"));
assert_eq!(domain.suffix(), "中国");
let domain = parse_domain_name("www.xn--85x722f.xn--55qx5d.cn")?;
assert_eq!(domain.root(), Some("xn--85x722f.xn--55qx5d.cn"));
assert_eq!(domain.suffix(), "xn--55qx5d.cn");
let domain = parse_domain_name("a.b.example.uk.com")?;
assert_eq!(domain.root(), Some("example.uk.com"));
assert_eq!(domain.suffix(), "uk.com");
let name = parse_dns_name("_tcp.example.com.")?;
assert_eq!(name.suffix(), Some("com."));
// In any case if the domain's suffix is in the list
// then this is definately a registrable domain name
assert!(domain.has_known_suffix());
Strict internationalized domain names (IDN) validation (use the idna feature flag)
For those who work with domain names the use cases of this library are plenty. publicsuffix.org/learn lists quite a few. For the sake of brevity, I'm not going to repeat them here. I work for a domain registrar so we make good use of this library. Here are some of the ways this library can be used:
domain.has_known_suffix() you can be absolutely sure this is a valid domain name. A regular expression is simply not robust enough.