Hostname

Struct Hostname 

Source
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>

Source

pub fn full_name(&self) -> &str

Returns the complete hostname as a string.

§Returns
  • &str - The full hostname.
§Examples
use faup_rs::{Url, Host};

let url = Url::parse("https://sub.example.com").unwrap();
if let Host::Hostname(hostname) = url.host() {
    assert_eq!(hostname.full_name(), "sub.example.com");
}
Source

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), or None 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"));
}
Source

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, or None 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"));
}
Source

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, or None 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);
}

Trait Implementations§

Source§

impl<'url> Debug for Hostname<'url>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'url> Freeze for Hostname<'url>

§

impl<'url> RefUnwindSafe for Hostname<'url>

§

impl<'url> Send for Hostname<'url>

§

impl<'url> Sync for Hostname<'url>

§

impl<'url> Unpin for Hostname<'url>

§

impl<'url> UnwindSafe for Hostname<'url>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.