Skip to main content

Host

Enum Host 

Source
pub enum Host {
    IpAddr(IpAddr),
    DomainName(DomainName),
    Hostname(Hostname),
}
Available on crate feature 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:

  1. IpAddr: IPv4 (e.g., “192.168.1.1”) or IPv6 (e.g., “::1”)
  2. DomainName: RFC 1035 domain names (labels can start with digits)
  3. 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

Source

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());
Source

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());
Source

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());
Source

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());
Source

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());
Source

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());
Source

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());
Source

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());
Source

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());
Source

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());
Source

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:

  1. Try to parse as IpAddr
  2. Try to parse as DomainName
  3. 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.
Source§

fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>

Generate an arbitrary value of Self from the given unstructured data. Read more
Source§

fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>

Generate an arbitrary value of Self from the entirety of the given unstructured data. Read more
Source§

fn size_hint(depth: usize) -> (usize, Option<usize>)

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
Source§

fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
Source§

impl Clone for Host

Source§

fn clone(&self) -> Host

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Host

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Host

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Host

Source§

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

Formats the value using the given formatter. Read more
Source§

impl FromStr for Host

Source§

type Err = HostError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Host

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Host

Source§

fn eq(&self, other: &Host) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Host

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for Host

Source§

impl StructuralPartialEq for Host

Auto Trait Implementations§

§

impl Freeze for Host

§

impl RefUnwindSafe for Host

§

impl Send for Host

§

impl Sync for Host

§

impl Unpin for Host

§

impl UnwindSafe for Host

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,