Enum Host

Source
pub enum Host<'host> {
    IPv4Address(Ipv4Addr),
    IPv6Address(Ipv6Addr),
    RegisteredName(RegisteredName<'host>),
}
Expand description

The host component of the authority as defined in [RFC3986, Section 3.2.2].

The RFC mentions support for future IP address literals. Of course, as of this moment there exist none, so hosts of the form "[v*...]" where '*' is a hexadecimal digit and '...' is the actual IP literal are not considered valid.

Also, the host is case-insensitive meaning that "example.com" and "ExAmPlE.CoM" refer to the same host. Furthermore, percent-encoding plays no role in equality checking for characters in the unreserved character set meaning that "example.com" and "ex%61mple.com" are identical. Both of these attributes are reflected in the equality and hash functions.

However, be aware that just because percent-encoding plays no role in equality checking does not mean that the host is normalized. If the host needs to be normalized, use the Host::normalize function.

Variants§

§

IPv4Address(Ipv4Addr)

An IPv4 address. Based on the std’s implementation, leading zeros for octets are allowed for up to three digits. So for example, "000.000.000.000" is still considered a valid IPv4 address, but "000.000.000.0000" is not. Thus, it would be considered a registered name.

§

IPv6Address(Ipv6Addr)

An IPv6 address. This will always be encased in brackets ('[' and ']').

§

RegisteredName(RegisteredName<'host>)

Any other host that does not follow the syntax of an IP address. This includes even hosts of the form "999.999.999.999". One might expect this to produce an invalid IPv4 error, but the RFC states that it is a “first-match-wins” algorithm, and that host does not match the IPv4 literal syntax.

This may be changed in the future, since arguments can be made from either side.

Implementations§

Source§

impl Host<'_>

Source

pub fn as_borrowed(&self) -> Host<'_>

Returns a new host which is identical but has a lifetime tied to this host.

Source

pub fn into_owned(self) -> Host<'static>

Converts the Host into an owned copy.

If you construct the host from a source with a non-static lifetime, you may run into lifetime problems due to the way the struct is designed. Calling this function will ensure that the returned value has a static lifetime.

This is different from just cloning. Cloning the host will just copy the references, and thus the lifetime will remain the same.

Source

pub fn is_ipv4_address(&self) -> bool

Returns whether the host is an IPv4 address.

§Examples
use std::convert::TryFrom;

use uriparse::Host;

let host = Host::try_from("192.168.1.1").unwrap();
assert!(host.is_ipv4_address());
Source

pub fn is_ipv6_address(&self) -> bool

Returns whether the host is an IPv6 address.

§Examples
use std::convert::TryFrom;

use uriparse::Host;

let host = Host::try_from("[::1]").unwrap();
assert!(host.is_ipv6_address());
Source

pub fn is_normalized(&self) -> bool

Returns whether the host is normalized.

IPv4 and IPv6 hosts will always be normalized. Registered names are considered normalized if all characters are lowercase, no bytes that are in the unreserved character set are percent-encoded, and all alphabetical characters in percent-encodings are uppercase.

This function runs in constant-time.

§Examples
use std::convert::TryFrom;

use uriparse::Host;

let host = Host::try_from("192.168.1.1").unwrap();
assert!(host.is_normalized());

let mut host = Host::try_from("EXAMPLE.COM").unwrap();
assert!(!host.is_normalized());
host.normalize();
assert!(host.is_normalized());
Source

pub fn is_registered_name(&self) -> bool

Returns whether the host is a registered name.

§Examples
use std::convert::TryFrom;

use uriparse::Host;

let host = Host::try_from("example.com").unwrap();
assert!(host.is_registered_name());
Source

pub fn normalize(&mut self)

Normalizes the host such that all characters are lowercase, no bytes that are in the unreserved character set are percent-encoded, and all alphabetical characters in percent-encodings are uppercase.

If the host is already normalized, the function will return immediately. Otherwise, if the host is not owned, this function will perform an allocation to clone it. The normalization itself though, is done in-place with no extra memory allocations required.

IPv4 and IPv6 hosts are always considered normalized.

§Examples
use std::convert::TryFrom;

use uriparse::Host;

let mut host = Host::try_from("192.168.1.1").unwrap();
host.normalize();
assert_eq!(host.to_string(), "192.168.1.1");

let mut host = Host::try_from("%ff%41").unwrap();
assert_eq!(host.to_string(), "%ff%41");
host.normalize();
assert_eq!(host.to_string(), "%FFA");

Trait Implementations§

Source§

impl<'host> Clone for Host<'host>

Source§

fn clone(&self) -> Host<'host>

Returns a copy 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<'host> Debug for Host<'host>

Source§

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

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

impl Display for Host<'_>

Source§

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

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

impl<'host> From<Host<'host>> for String

Source§

fn from(value: Host<'host>) -> String

Converts to this type from the input type.
Source§

impl From<IpAddr> for Host<'static>

Source§

fn from(value: IpAddr) -> Self

Converts to this type from the input type.
Source§

impl From<Ipv4Addr> for Host<'static>

Source§

fn from(value: Ipv4Addr) -> Self

Converts to this type from the input type.
Source§

impl From<Ipv6Addr> for Host<'static>

Source§

fn from(value: Ipv6Addr) -> Self

Converts to this type from the input type.
Source§

impl<'host> Hash for Host<'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<'host> PartialEq for Host<'host>

Source§

fn eq(&self, other: &Host<'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<'host> TryFrom<&'host [u8]> for Host<'host>

Source§

type Error = HostError

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

fn try_from(value: &'host [u8]) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'host> TryFrom<&'host str> for Host<'host>

Source§

type Error = HostError

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

fn try_from(value: &'host str) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'host> Eq for Host<'host>

Source§

impl<'host> StructuralPartialEq for Host<'host>

Auto Trait Implementations§

§

impl<'host> Freeze for Host<'host>

§

impl<'host> RefUnwindSafe for Host<'host>

§

impl<'host> Send for Host<'host>

§

impl<'host> Sync for Host<'host>

§

impl<'host> Unpin for Host<'host>

§

impl<'host> UnwindSafe for Host<'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.