pub enum Host<'url> {
Hostname(Hostname<'url>),
Ip(IpAddr),
}Expand description
Represents the host component of a URL, which can be either a hostname or an IP address.
Variants§
Implementations§
Source§impl<'host> Host<'host>
impl<'host> Host<'host>
Sourcepub fn parse(host: &'host str) -> Result<Self, Error>
pub fn parse(host: &'host str) -> Result<Self, Error>
Parses a string into a Host enum.
This function expects the input string to be a URL host, which can be either an IPv4 address, an IPv6 address, or a hostname.
§Arguments
host- A string slice that holds the host to parse (e.g.,"example.com","127.0.0.1","::1").
§Returns
Result<Host, Error>- AHostenum if parsing is successful, or an [Error] if parsing fails.
§Examples
use faup_rs::Host;
// Parse an IPv4 address
let host = Host::parse("127.0.0.1").unwrap();
assert!(matches!(host, Host::Ip(std::net::IpAddr::V4(_))));
// Parse an IPv6 address
let host = Host::parse("::1").unwrap();
assert!(matches!(host, Host::Ip(std::net::IpAddr::V6(_))));
// Parse a hostname
let host = Host::parse("example.com").unwrap();
assert!(matches!(host, Host::Hostname(_)));
// Parse a hostname with a subdomain
let host = Host::parse("sub.example.com").unwrap();
assert!(matches!(host, Host::Hostname(_)));
// Parse a hostname with a custom TLD
let host = Host::parse("example.b32.i2p").unwrap();
assert!(matches!(host, Host::Hostname(_)));
// Attempt to parse an invalid host
let result = Host::parse("invalid..host");
assert!(matches!(result, Err(faup_rs::Error::InvalidHost)));Sourcepub fn as_hostname(&self) -> Option<&Hostname<'_>>
pub fn as_hostname(&self) -> Option<&Hostname<'_>>
Returns the hostname component if this is a Host::Hostname variant.
§Returns
Option<&Hostname>- The hostname, orNoneif this is an IP address.
Trait Implementations§
Auto Trait Implementations§
impl<'url> Freeze for Host<'url>
impl<'url> RefUnwindSafe for Host<'url>
impl<'url> Send for Host<'url>
impl<'url> Sync for Host<'url>
impl<'url> Unpin for Host<'url>
impl<'url> UnwindSafe for Host<'url>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more