pub struct Hostname<'url> { /* private fields */ }
Expand description
Represents a parsed hostname with its components (subdomain, domain, and suffix).
The Hostname
struct provides access to the different parts of a domain name,
including support for internationalized domain names (IDNs), custom top-level domains (TLDs),
and subdomains. It uses the Public Suffix List (via the psl
crate) to properly identify
domain suffixes, with additional support for custom TLDs.
§Examples
use faup_rs::{Url, Host};
// Parse a simple domain
let url = Url::parse("https://example.com").unwrap();
if let Host::Hostname(hostname) = url.host() {
assert_eq!(hostname.full_name(), "example.com");
assert_eq!(hostname.suffix(), Some("com"));
assert_eq!(hostname.domain(), Some("example.com"));
assert_eq!(hostname.subdomain(), None);
}
// Parse a domain with subdomains
let url = Url::parse("https://sub.example.co.uk").unwrap();
if let Host::Hostname(hostname) = url.host() {
assert_eq!(hostname.full_name(), "sub.example.co.uk");
assert_eq!(hostname.suffix(), Some("co.uk"));
assert_eq!(hostname.domain(), Some("example.co.uk"));
assert_eq!(hostname.subdomain(), Some("sub"));
}
// Parse a domain with UTF-8 characters
let url = Url::parse("https://例子.测试").unwrap();
if let Host::Hostname(hostname) = url.host() {
assert_eq!(hostname.full_name(), "例子.测试");
assert_eq!(hostname.suffix(), Some("测试"));
assert_eq!(hostname.domain(), Some("例子.测试"));
assert_eq!(hostname.subdomain(), None);
}
// Parse a domain with custom TLD
let url = Url::parse("http://example.b32.i2p").unwrap();
if let Host::Hostname(hostname) = url.host() {
assert_eq!(hostname.suffix(), Some("b32.i2p"));
}
Implementations§
Source§impl<'url> Hostname<'url>
impl<'url> Hostname<'url>
Sourcepub fn suffix(&self) -> Option<&str>
pub fn suffix(&self) -> Option<&str>
Returns the suffix (top-level domain) of the hostname, if recognized.
The suffix is determined using the Public Suffix List, with additional support
for custom TLDs defined in the CUSTOM_TLDS
constant.
§Returns
Option<&str>
- The suffix (TLD), orNone
if not recognized.
§Examples
use faup_rs::{Url, Host};
// Standard TLD
let url = Url::parse("https://example.com").unwrap();
if let Host::Hostname(hostname) = url.host() {
assert_eq!(hostname.suffix(), Some("com"));
}
// Multi-level TLD
let url = Url::parse("https://example.co.uk").unwrap();
if let Host::Hostname(hostname) = url.host() {
assert_eq!(hostname.suffix(), Some("co.uk"));
}
// Custom TLD
let url = Url::parse("http://example.b32.i2p").unwrap();
if let Host::Hostname(hostname) = url.host() {
assert_eq!(hostname.suffix(), Some("b32.i2p"));
}
Sourcepub fn domain(&self) -> Option<&str>
pub fn domain(&self) -> Option<&str>
Returns the domain part of the hostname, if recognized.
The domain is the registrable part of the hostname, excluding any subdomains and including the suffix.
§Returns
Option<&str>
- The domain, orNone
if not recognized.
§Examples
use faup_rs::{Url, Host};
// Simple domain
let url = Url::parse("https://example.com").unwrap();
if let Host::Hostname(hostname) = url.host() {
assert_eq!(hostname.domain(), Some("example.com"));
}
// Domain with multi-level TLD
let url = Url::parse("https://example.co.uk").unwrap();
if let Host::Hostname(hostname) = url.host() {
assert_eq!(hostname.domain(), Some("example.co.uk"));
}
Sourcepub fn subdomain(&self) -> Option<&str>
pub fn subdomain(&self) -> Option<&str>
Returns the subdomain part of the hostname, if present.
The subdomain is everything before the domain. For example, in “sub.example.com”, “sub” is the subdomain.
§Returns
Option<&str>
- The subdomain, orNone
if not present.
§Examples
use faup_rs::{Url, Host};
// Single-level subdomain
let url = Url::parse("https://sub.example.com").unwrap();
if let Host::Hostname(hostname) = url.host() {
assert_eq!(hostname.subdomain(), Some("sub"));
}
// Multi-level subdomain
let url = Url::parse("https://a.b.example.com").unwrap();
if let Host::Hostname(hostname) = url.host() {
assert_eq!(hostname.subdomain(), Some("a.b"));
}
// No subdomain
let url = Url::parse("https://example.com").unwrap();
if let Host::Hostname(hostname) = url.host() {
assert_eq!(hostname.subdomain(), None);
}