pub struct Domain<S>(/* private fields */)
where
S: ?Sized;Expand description
A DNS domain name, as . dot-separated labels.
Non-ASCII labels are encoded in punycode per IDNA if this is the host of a special URL,
or percent encoded for non-special URLs.
§Note
In this implementation, a fully-qualified domain name (FQDN) is valid. This means that
the domain name can end with a . dot.
e.g.
example.com.is a valid domain name, because it is a FQDN.- A single
.dot is a valid domain name, because it is root domain.
§Example
use std::{sync::Arc, str::FromStr};
use hostaddr::Domain;
let domain = Domain::<String>::from_str("example.com").unwrap();
assert_eq!(domain.as_inner(), "example.com");
let domain = Domain::<String>::from_str("пример.испытание").unwrap();
assert_eq!(domain.as_inner(), "xn--e1afmkfd.xn--80akhbyknj4f");
let domain = Domain::<Arc<str>>::from_str("测试.中国").unwrap();
assert_eq!(domain.as_inner().as_ref(), "xn--0zwm56d.xn--fiqs8s");
let domain = Domain::<Arc<[u8]>>::try_from("test.com".as_bytes()).unwrap();
assert_eq!(domain.as_inner().as_ref(), b"test.com");Implementations§
Source§impl<S> Domain<S>where
S: ?Sized,
impl<S> Domain<S>where
S: ?Sized,
Sourcepub fn into_inner(self) -> S
pub fn into_inner(self) -> S
Returns the inner S.
Sourcepub fn as_deref(&self) -> Domain<&<S as Deref>::Target>where
S: Deref,
pub fn as_deref(&self) -> Domain<&<S as Deref>::Target>where
S: Deref,
Converts from Domain<S> (or &Domain<S>) to Domain<&S::Target>.
Sourcepub fn to_fqdn(&self) -> Option<Domain<Buffer>>
pub fn to_fqdn(&self) -> Option<Domain<Buffer>>
Converts the domain to a fully qualified domain name (FQDN) by appending a dot (.)
at the end if it is not already a FQDN.
Returns None if the domain is already a FQDN.
§Domain Length Constraints
Valid domain names are at most 253 bytes long (or 254 bytes with trailing dot for FQDN). Since validation ensures these constraints, this method will always succeed for valid domains.
§Example
use hostaddr::{Domain, Buffer};
// Regular domain to FQDN
let domain = Domain::<str>::try_from_ascii_str("example.com").unwrap();
let fqdn = domain.to_fqdn().unwrap();
assert_eq!(fqdn.as_ref().into_inner().as_str(), "example.com.");
// Already a FQDN
let domain = Domain::<str>::try_from_ascii_str("example.com.").unwrap();
assert!(domain.to_fqdn().is_none());
// Works with max-length domains (253 bytes)
let long_label = "a".repeat(63);
let long_domain = format!("{}.{}.{}.{}", long_label, long_label, long_label, &long_label[..61]);
let domain = Domain::<str>::try_from_ascii_str(&long_domain).unwrap();
assert_eq!(domain.as_ref().into_inner().len(), 253);
let fqdn = domain.to_fqdn().unwrap();
assert_eq!(fqdn.as_ref().into_inner().as_str().len(), 254);Sourcepub fn is_fqdn(&self) -> bool
pub fn is_fqdn(&self) -> bool
Returns true if the domain is a fully qualified domain name (FQDN).
A FQDN is a domain name that ends with a dot (.), representing the DNS root.
§Example
use hostaddr::Domain;
// Regular domain (not FQDN)
let domain = Domain::<str>::try_from_ascii_str("example.com").unwrap();
assert!(!domain.is_fqdn());
// FQDN (ends with dot)
let fqdn = Domain::<str>::try_from_ascii_str("example.com.").unwrap();
assert!(fqdn.is_fqdn());
// Root domain is a FQDN
let root = Domain::<str>::try_from_ascii_str(".").unwrap();
assert!(root.is_fqdn());Source§impl<S> Domain<&S>
impl<S> Domain<&S>
Sourcepub const fn copied(self) -> Domain<S>where
S: Copy,
pub const fn copied(self) -> Domain<S>where
S: Copy,
Maps an Domain<&S> to an Domain<S> by copying the contents of the
domain.
§Example
use hostaddr::{Domain, Buffer};
let domain: Domain<Buffer> = Domain::try_from("example.com").unwrap();
assert_eq!("example.com", domain.as_ref().copied().as_inner().as_str());Source§impl Domain<str>
impl Domain<str>
Sourcepub const fn try_from_ascii_str(
input: &str,
) -> Result<&Domain<str>, ParseAsciiDomainError>
pub const fn try_from_ascii_str( input: &str, ) -> Result<&Domain<str>, ParseAsciiDomainError>
Parses a domain name from &str.
Unlike Domain::try_from_str, this method does not perform any percent decoding
or punycode decoding. If the input is not ASCII, it will return an error.
§Example
use hostaddr::Domain;
let domain = Domain::try_from_ascii_str("example.com").unwrap();
assert_eq!(domain.as_ref().into_inner(), "example.com");
// This will return an error because the domain is not ASCII.
assert!(Domain::try_from_ascii_str("测试.中国").is_err());
// Thie will not return an error, even though the human-readable domain is not ASCII.
let domain = Domain::try_from_ascii_str("xn--0zwm56d.xn--fiqs8s").unwrap();
assert_eq!(domain.as_ref().into_inner(), "xn--0zwm56d.xn--fiqs8s");Source§impl Domain<[u8]>
impl Domain<[u8]>
Sourcepub const fn try_from_ascii_bytes(
input: &[u8],
) -> Result<&Domain<[u8]>, ParseAsciiDomainError>
pub const fn try_from_ascii_bytes( input: &[u8], ) -> Result<&Domain<[u8]>, ParseAsciiDomainError>
Parses a domain name from &[u8].
Unlike Domain::try_from_bytes, this method does not perform any percent decoding
or punycode decoding. If the input is not ASCII, it will return an error.
§Example
use hostaddr::Domain;
let domain = Domain::try_from_ascii_bytes(b"example.com").unwrap();
assert_eq!(domain.as_ref().into_inner(), b"example.com");
// This will return an error because the domain is not ASCII.
assert!(Domain::try_from_ascii_bytes("测试.中国".as_bytes()).is_err());
// Thie will not return an error, even though the human-readable domain is not ASCII.
let domain = Domain::try_from_ascii_bytes(b"xn--0zwm56d.xn--fiqs8s").unwrap();
assert_eq!(domain.as_ref().into_inner(), b"xn--0zwm56d.xn--fiqs8s");Source§impl<S> Domain<S>
impl<S> Domain<S>
Sourcepub fn try_from_bytes(
input: S,
) -> Result<Either<Domain<S>, Buffer>, ParseDomainError>
pub fn try_from_bytes( input: S, ) -> Result<Either<Domain<S>, Buffer>, ParseDomainError>
Parses a domain name from &[u8].
If you can make sure the input is ASCII and not percent encoded,
then Domain::try_from_ascii_bytes should be used instead.
§Note
- If the given input is encoded in percent encoding, it will be decoded.
- If the given input is not ASCII, it will be converted to ASCII using punycode.
- Otherwise, the input will be returned as is.
If the 1. & 2. happen, the result will be returned as a Either::Right(Buffer).
If the input is not a valid domain name, this method will return an error.
§Example
use hostaddr::Domain;
let domain = Domain::try_from_bytes(b"example.com").unwrap();
assert_eq!(domain.unwrap_left().into_inner(), b"example.com");
let domain = Domain::try_from_bytes("测试.中国".as_bytes()).unwrap();
assert_eq!(domain.unwrap_right().as_bytes(), b"xn--0zwm56d.xn--fiqs8s");
let domain = Domain::try_from_bytes(b"example%2Ecom").unwrap();
assert_eq!(domain.unwrap_right().as_bytes(), b"example.com");
let domain = Domain::try_from_bytes("测试%2E中国".as_bytes()).unwrap();
assert_eq!(domain.unwrap_right().as_bytes(), b"xn--0zwm56d.xn--fiqs8s");Sourcepub fn try_from_str(
input: S,
) -> Result<Either<Domain<S>, Buffer>, ParseDomainError>
pub fn try_from_str( input: S, ) -> Result<Either<Domain<S>, Buffer>, ParseDomainError>
Parses a domain name from &str.
If you can make sure the input is ASCII and not percent encoded,
then Domain::try_from_ascii_str should be used instead.
§Note
- If the given input is encoded in percent encoding, it will be decoded.
- If the given input is not ASCII, it will be converted to ASCII using punycode.
- Otherwise, the input will be returned as is.
If the 1. & 2. happen, the result will be returned as a Either::Right(Buffer).
If the input is not a valid domain name, this method will return an error.
§Example
use hostaddr::Domain;
let domain = Domain::try_from_str("example.com").unwrap();
assert_eq!(domain.unwrap_left().into_inner(), "example.com");
let domain = Domain::try_from_str("测试.中国").unwrap();
assert_eq!(domain.unwrap_right().as_str(), "xn--0zwm56d.xn--fiqs8s");
let domain = Domain::try_from_str("example%2Ecom").unwrap();
assert_eq!(domain.unwrap_right().as_str(), "example.com");
let domain = Domain::try_from_str("测试%2E中国").unwrap();
assert_eq!(domain.unwrap_right().as_str(), "xn--0zwm56d.xn--fiqs8s");Trait Implementations§
Source§impl<S> CheapClone for Domain<S>where
S: CheapClone,
impl<S> CheapClone for Domain<S>where
S: CheapClone,
Source§fn cheap_clone(&self) -> Self
fn cheap_clone(&self) -> Self
Source§impl Data for Domain<SmolStr>
impl Data for Domain<SmolStr>
Source§fn from_ref(
val: <Domain<SmolStr> as Data>::Ref<'_>,
) -> Result<Domain<SmolStr>, DecodeError>
fn from_ref( val: <Domain<SmolStr> as Data>::Ref<'_>, ) -> Result<Domain<SmolStr>, DecodeError>
Source§fn encoded_len(&self) -> usize
fn encoded_len(&self) -> usize
Source§fn encode(&self, buf: &mut [u8]) -> Result<usize, EncodeError>
fn encode(&self, buf: &mut [u8]) -> Result<usize, EncodeError>
Source§fn encoded_len_with_length_delimited(&self) -> usize
fn encoded_len_with_length_delimited(&self) -> usize
Source§fn encode_to_vec(&self) -> Result<Vec<u8>, EncodeError>
fn encode_to_vec(&self) -> Result<Vec<u8>, EncodeError>
Source§fn encode_to_bytes(&self) -> Result<Bytes, EncodeError>
fn encode_to_bytes(&self) -> Result<Bytes, EncodeError>
Bytes.Source§fn encode_length_delimited(&self, buf: &mut [u8]) -> Result<usize, EncodeError>
fn encode_length_delimited(&self, buf: &mut [u8]) -> Result<usize, EncodeError>
Source§fn encode_length_delimited_to_vec(&self) -> Result<Vec<u8>, EncodeError>
fn encode_length_delimited_to_vec(&self) -> Result<Vec<u8>, EncodeError>
Source§fn encode_length_delimited_to_bytes(&self) -> Result<Bytes, EncodeError>
fn encode_length_delimited_to_bytes(&self) -> Result<Bytes, EncodeError>
Bytes.Source§fn decode(src: &[u8]) -> Result<(usize, Self), DecodeError>where
Self: Sized,
fn decode(src: &[u8]) -> Result<(usize, Self), DecodeError>where
Self: Sized,
Source§fn decode_length_delimited(buf: &[u8]) -> Result<(usize, Self), DecodeError>where
Self: Sized,
fn decode_length_delimited(buf: &[u8]) -> Result<(usize, Self), DecodeError>where
Self: Sized,
Source§impl<'a> DataRef<'a, Domain<SmolStr>> for Domain<Buffer>
impl<'a> DataRef<'a, Domain<SmolStr>> for Domain<Buffer>
Source§fn decode(buf: &'a [u8]) -> Result<(usize, Domain<Buffer>), DecodeError>
fn decode(buf: &'a [u8]) -> Result<(usize, Domain<Buffer>), DecodeError>
Source§fn decode_length_delimited(src: &'a [u8]) -> Result<(usize, Self), DecodeError>where
Self: Sized,
fn decode_length_delimited(src: &'a [u8]) -> Result<(usize, Self), DecodeError>where
Self: Sized,
Source§impl From<Domain<&str>> for Domain<Buffer>
impl From<Domain<&str>> for Domain<Buffer>
Source§fn from(value: Domain<&str>) -> Domain<Buffer>
fn from(value: Domain<&str>) -> Domain<Buffer>
use hostaddr::{Domain, Buffer};
let domain: Domain<Buffer> = Domain::try_from_ascii_str("example.com").unwrap().as_ref().into();
let buffer: Buffer = domain.into();
assert_eq!(buffer.as_str(), "example.com");
let domain: Domain<Buffer> = buffer.into();
assert_eq!(domain.into_inner().as_str(), "example.com");Source§impl<S> Ord for Domain<S>
impl<S> Ord for Domain<S>
Source§impl<S> PartialOrd for Domain<S>where
S: PartialOrd + ?Sized,
impl<S> PartialOrd for Domain<S>where
S: PartialOrd + ?Sized,
impl<S> Copy for Domain<S>
impl<S> Eq for Domain<S>
impl<S> StructuralPartialEq for Domain<S>where
S: ?Sized,
Auto Trait Implementations§
impl<S> Freeze for Domain<S>
impl<S> RefUnwindSafe for Domain<S>where
S: RefUnwindSafe + ?Sized,
impl<S> Send for Domain<S>
impl<S> Sync for Domain<S>
impl<S> Unpin for Domain<S>
impl<S> UnwindSafe for Domain<S>where
S: UnwindSafe + ?Sized,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more