pub enum Host {
IpAddr(IpAddr),
DomainName(DomainName),
Hostname(Hostname),
}net only.Expand description
A network host that can be an IP address, domain name, or hostname.
This enum provides a unified type for representing network hosts, with automatic parsing that follows a specific priority order.
§Parsing Priority
When parsing from a string, the following order is used:
IpAddr: IPv4 (e.g., “192.168.1.1”) or IPv6 (e.g., “::1”)DomainName: RFC 1035 domain names (labels can start with digits)Hostname: RFC 1123 hostnames (labels must start with letters)
§Examples
use bare_types::net::Host;
// Create from IP address
let ipaddr = "192.168.1.1".parse::<bare_types::net::IpAddr>().unwrap();
let host = Host::from_ipaddr(ipaddr);
assert!(host.is_ipaddr());
// Create from domain name
let domain = "123.example.com".parse::<bare_types::net::DomainName>().unwrap();
let host = Host::from_domainname(domain);
assert!(host.is_domainname());
// Create from hostname
let hostname = "localhost".parse::<bare_types::net::Hostname>().unwrap();
let host = Host::from_hostname(hostname);
assert!(host.is_hostname());
// Parse with automatic detection
let host: Host = "192.168.1.1".parse().unwrap();
assert!(host.is_ipaddr());
let host: Host = "example.com".parse().unwrap();
assert!(host.is_domainname());Variants§
IpAddr(IpAddr)
An IP address (IPv4 or IPv6)
DomainName(DomainName)
A domain name (RFC 1035, labels can start with digits)
Hostname(Hostname)
A hostname (RFC 1123, labels must start with letters)
Implementations§
Source§impl Host
impl Host
Sourcepub const fn from_ipaddr(ipaddr: IpAddr) -> Self
pub const fn from_ipaddr(ipaddr: IpAddr) -> Self
Creates a Host from an IP address.
§Examples
use bare_types::net::Host;
let ipaddr = "192.168.1.1".parse::<bare_types::net::IpAddr>()?;
let host = Host::from_ipaddr(ipaddr);
assert!(host.is_ipaddr());Sourcepub const fn from_domainname(domain: DomainName) -> Self
pub const fn from_domainname(domain: DomainName) -> Self
Creates a Host from a domain name.
§Examples
use bare_types::net::Host;
let domain = "example.com".parse::<bare_types::net::DomainName>()?;
let host = Host::from_domainname(domain);
assert!(host.is_domainname());Sourcepub const fn from_hostname(hostname: Hostname) -> Self
pub const fn from_hostname(hostname: Hostname) -> Self
Creates a Host from a hostname.
§Examples
use bare_types::net::Host;
let hostname = "example.com".parse::<bare_types::net::Hostname>()?;
let host = Host::from_hostname(hostname);
assert!(host.is_hostname());Sourcepub const fn is_ipaddr(&self) -> bool
pub const fn is_ipaddr(&self) -> bool
Returns true if this host is an IP address.
§Examples
use bare_types::net::Host;
let host: Host = "192.168.1.1".parse()?;
assert!(host.is_ipaddr());Sourcepub const fn is_domainname(&self) -> bool
pub const fn is_domainname(&self) -> bool
Returns true if this host is a domain name.
§Examples
use bare_types::net::Host;
let host: Host = "123.example.com".parse()?;
assert!(host.is_domainname());Sourcepub const fn is_hostname(&self) -> bool
pub const fn is_hostname(&self) -> bool
Returns true if this host is a hostname.
§Examples
use bare_types::net::Host;
let hostname = "localhost".parse::<bare_types::net::Hostname>()?;
let host = Host::from_hostname(hostname);
assert!(host.is_hostname());Sourcepub const fn as_ipaddr(&self) -> Option<&IpAddr>
pub const fn as_ipaddr(&self) -> Option<&IpAddr>
Returns a reference to the IP address if this is an IP address.
§Examples
use bare_types::net::Host;
let host: Host = "192.168.1.1".parse()?;
assert!(host.as_ipaddr().is_some());Sourcepub const fn as_domainname(&self) -> Option<&DomainName>
pub const fn as_domainname(&self) -> Option<&DomainName>
Returns a reference to the domain name if this is a domain name.
§Examples
use bare_types::net::Host;
let host: Host = "123.example.com".parse()?;
assert!(host.as_domainname().is_some());Sourcepub const fn as_hostname(&self) -> Option<&Hostname>
pub const fn as_hostname(&self) -> Option<&Hostname>
Returns a reference to the hostname if this is a hostname.
§Examples
use bare_types::net::Host;
let hostname = "localhost".parse::<bare_types::net::Hostname>()?;
let host = Host::from_hostname(hostname);
assert!(host.as_hostname().is_some());Sourcepub fn is_localhost(&self) -> bool
pub fn is_localhost(&self) -> bool
Returns true if this host represents localhost.
For IP addresses, this checks if it’s a loopback address. For domain names and hostnames, this checks if it’s “localhost”.
§Examples
use bare_types::net::Host;
// IPv4 loopback
let host: Host = "127.0.0.1".parse()?;
assert!(host.is_localhost());
// IPv6 loopback
let host: Host = "::1".parse()?;
assert!(host.is_localhost());
// localhost domain name
let host: Host = "localhost".parse()?;
assert!(host.is_localhost());
// Not localhost
let host: Host = "example.com".parse()?;
assert!(!host.is_localhost());Sourcepub fn parse_str(s: &str) -> Result<Self, HostError>
pub fn parse_str(s: &str) -> Result<Self, HostError>
Parses a string into a Host with automatic type detection.
The parsing follows this priority order:
- Try to parse as
IpAddr - Try to parse as
DomainName - Try to parse as
Hostname
§Errors
Returns HostError::InvalidInput if the string cannot be parsed as
an IP address, domain name, or hostname.
§Examples
use bare_types::net::Host;
// IP address is parsed first
let host = Host::parse_str("192.168.1.1")?;
assert!(host.is_ipaddr());
// Domain name (labels can start with digits)
let host = Host::parse_str("123.example.com")?;
assert!(host.is_domainname());
// Domain name is also parsed before hostname for letter-start labels
let host = Host::parse_str("www.example.com")?;
assert!(host.is_domainname());Trait Implementations§
Source§impl<'a> Arbitrary<'a> for Host
Available on crate feature arbitrary only.
impl<'a> Arbitrary<'a> for Host
arbitrary only.Source§fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
Self from the given unstructured data. Read moreSource§fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
Self from the entirety of the given
unstructured data. Read moreSource§fn size_hint(depth: usize) -> (usize, Option<usize>)
fn size_hint(depth: usize) -> (usize, Option<usize>)
Unstructured this type
needs to construct itself. Read moreSource§fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), MaxRecursionReached>
fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>
Unstructured this type
needs to construct itself. Read more