DomainDetails

Struct DomainDetails 

Source
pub struct DomainDetails {
    pub middle_start: Option<usize>,
    pub suffix_start: Option<usize>,
    pub fqdn_period: Option<usize>,
}
Expand description

The details of a domain host.

Fields§

§middle_start: Option<usize>

The start of the domain middle.

§suffix_start: Option<usize>

The start of the domain suffix.

§fqdn_period: Option<usize>

The location of the fully qualified domain name period.

Implementations§

Source§

impl DomainDetails

Source

pub fn parse(domain: &str) -> Result<Self, GetDomainDetailsError>

Checks if domain is a valid domain, then returns its details.

If you’re absolutely certain the value you’re using is a valid domain, you can use Self::parse_unchecked.

§Errors

If the call to url::Host::parse returns an error, that error is returned.

If the call to url::Host::parse doesn’t return url::Host::Domain, returns the error GetDomainDetailsError::NotADomain.

§Examples
use better_url::*;

assert_eq!(DomainDetails::parse(    "example.com"   ).unwrap(), DomainDetails {middle_start: Some(0), suffix_start: Some( 8), fqdn_period: None});
assert_eq!(DomainDetails::parse("www.example.com"   ).unwrap(), DomainDetails {middle_start: Some(4), suffix_start: Some(12), fqdn_period: None});
assert_eq!(DomainDetails::parse(    "example.co.uk" ).unwrap(), DomainDetails {middle_start: Some(0), suffix_start: Some( 8), fqdn_period: None});
assert_eq!(DomainDetails::parse("www.example.co.uk" ).unwrap(), DomainDetails {middle_start: Some(4), suffix_start: Some(12), fqdn_period: None});
assert_eq!(DomainDetails::parse(    "example.com."  ).unwrap(), DomainDetails {middle_start: Some(0), suffix_start: Some( 8), fqdn_period: Some(11)});
assert_eq!(DomainDetails::parse("www.example.com."  ).unwrap(), DomainDetails {middle_start: Some(4), suffix_start: Some(12), fqdn_period: Some(15)});
assert_eq!(DomainDetails::parse(    "example.co.uk.").unwrap(), DomainDetails {middle_start: Some(0), suffix_start: Some( 8), fqdn_period: Some(13)});
assert_eq!(DomainDetails::parse("www.example.co.uk.").unwrap(), DomainDetails {middle_start: Some(4), suffix_start: Some(12), fqdn_period: Some(17)});

DomainDetails::parse("127.0.0.1").unwrap_err();
DomainDetails::parse("[::1]").unwrap_err();
Source

pub fn parse_unchecked(domain: &str) -> Self

Gets the details of a domain without checking it’s actually a domain first.

If you are at all possibly not working with a domain (like an IP host), please use Self::parse instead.

§Examples
use better_url::*;

assert_eq!(DomainDetails::parse_unchecked(    "example.com"   ), DomainDetails {middle_start: Some(0), suffix_start: Some( 8), fqdn_period: None});
assert_eq!(DomainDetails::parse_unchecked("www.example.com"   ), DomainDetails {middle_start: Some(4), suffix_start: Some(12), fqdn_period: None});
assert_eq!(DomainDetails::parse_unchecked(    "example.co.uk" ), DomainDetails {middle_start: Some(0), suffix_start: Some( 8), fqdn_period: None});
assert_eq!(DomainDetails::parse_unchecked("www.example.co.uk" ), DomainDetails {middle_start: Some(4), suffix_start: Some(12), fqdn_period: None});
assert_eq!(DomainDetails::parse_unchecked(    "example.com."  ), DomainDetails {middle_start: Some(0), suffix_start: Some( 8), fqdn_period: Some(11)});
assert_eq!(DomainDetails::parse_unchecked("www.example.com."  ), DomainDetails {middle_start: Some(4), suffix_start: Some(12), fqdn_period: Some(15)});
assert_eq!(DomainDetails::parse_unchecked(    "example.co.uk."), DomainDetails {middle_start: Some(0), suffix_start: Some( 8), fqdn_period: Some(13)});
assert_eq!(DomainDetails::parse_unchecked("www.example.co.uk."), DomainDetails {middle_start: Some(4), suffix_start: Some(12), fqdn_period: Some(17)});
Source

pub fn subdomain_period(&self) -> Option<usize>

The location of the period between subdomain and domain middle.

§Examples
use better_url::*;

assert_eq!(DomainDetails::parse(    "example.com"   ).unwrap().subdomain_period(), None   );
assert_eq!(DomainDetails::parse("www.example.com"   ).unwrap().subdomain_period(), Some(3));
assert_eq!(DomainDetails::parse(    "example.co.uk" ).unwrap().subdomain_period(), None   );
assert_eq!(DomainDetails::parse("www.example.co.uk" ).unwrap().subdomain_period(), Some(3));
assert_eq!(DomainDetails::parse(    "example.com."  ).unwrap().subdomain_period(), None   );
assert_eq!(DomainDetails::parse("www.example.com."  ).unwrap().subdomain_period(), Some(3));
assert_eq!(DomainDetails::parse(    "example.co.uk.").unwrap().subdomain_period(), None   );
assert_eq!(DomainDetails::parse("www.example.co.uk.").unwrap().subdomain_period(), Some(3));
Source

pub fn domain_suffix_period(&self) -> Option<usize>

The location of the period between domain middle and domain suffix.

§Examples
use better_url::*;

assert_eq!(DomainDetails::parse(    "example.com"   ).unwrap().domain_suffix_period(), Some( 7));
assert_eq!(DomainDetails::parse("www.example.com"   ).unwrap().domain_suffix_period(), Some(11));
assert_eq!(DomainDetails::parse(    "example.co.uk" ).unwrap().domain_suffix_period(), Some( 7));
assert_eq!(DomainDetails::parse("www.example.co.uk" ).unwrap().domain_suffix_period(), Some(11));
assert_eq!(DomainDetails::parse(    "example.com."  ).unwrap().domain_suffix_period(), Some( 7));
assert_eq!(DomainDetails::parse("www.example.com."  ).unwrap().domain_suffix_period(), Some(11));
assert_eq!(DomainDetails::parse(    "example.co.uk.").unwrap().domain_suffix_period(), Some( 7));
assert_eq!(DomainDetails::parse("www.example.co.uk.").unwrap().domain_suffix_period(), Some(11));
Source

pub fn domain_bounds(&self) -> (Bound<usize>, Bound<usize>)

The bounds of domain.

Notably does not include Self::fqdn_period

§Examples
use better_url::*;

let x =     "example.com"   ; assert_eq!(&x[DomainDetails::parse(x).unwrap().domain_bounds()],     "example.com"  );
let x = "www.example.com"   ; assert_eq!(&x[DomainDetails::parse(x).unwrap().domain_bounds()], "www.example.com"  );
let x =     "example.co.uk" ; assert_eq!(&x[DomainDetails::parse(x).unwrap().domain_bounds()],     "example.co.uk");
let x = "www.example.co.uk" ; assert_eq!(&x[DomainDetails::parse(x).unwrap().domain_bounds()], "www.example.co.uk");
let x =     "example.com."  ; assert_eq!(&x[DomainDetails::parse(x).unwrap().domain_bounds()],     "example.com"  );
let x = "www.example.com."  ; assert_eq!(&x[DomainDetails::parse(x).unwrap().domain_bounds()], "www.example.com"  );
let x =     "example.co.uk."; assert_eq!(&x[DomainDetails::parse(x).unwrap().domain_bounds()],     "example.co.uk");
let x = "www.example.co.uk."; assert_eq!(&x[DomainDetails::parse(x).unwrap().domain_bounds()], "www.example.co.uk");
Source

pub fn subdomain_bounds(&self) -> Option<(Bound<usize>, Bound<usize>)>

The bounds of subdomain.

§Examples
use better_url::*;

let x =     "example.com"   ; assert_eq!(   DomainDetails::parse(x).unwrap().subdomain_bounds()          , None );
let x = "www.example.com"   ; assert_eq!(&x[DomainDetails::parse(x).unwrap().subdomain_bounds().unwrap()], "www");
let x =     "example.co.uk" ; assert_eq!(   DomainDetails::parse(x).unwrap().subdomain_bounds()          , None );
let x = "www.example.co.uk" ; assert_eq!(&x[DomainDetails::parse(x).unwrap().subdomain_bounds().unwrap()], "www");
let x =     "example.com."  ; assert_eq!(   DomainDetails::parse(x).unwrap().subdomain_bounds()          , None );
let x = "www.example.com."  ; assert_eq!(&x[DomainDetails::parse(x).unwrap().subdomain_bounds().unwrap()], "www");
let x =     "example.co.uk."; assert_eq!(   DomainDetails::parse(x).unwrap().subdomain_bounds()          , None );
let x = "www.example.co.uk."; assert_eq!(&x[DomainDetails::parse(x).unwrap().subdomain_bounds().unwrap()], "www");
Source

pub fn not_domain_suffix_bounds(&self) -> Option<(Bound<usize>, Bound<usize>)>

The bounds of not domain suffix.

§Examples
use better_url::*;

let x =     "example.com"   ; assert_eq!(&x[DomainDetails::parse(x).unwrap().not_domain_suffix_bounds().unwrap()],     "example");
let x = "www.example.com"   ; assert_eq!(&x[DomainDetails::parse(x).unwrap().not_domain_suffix_bounds().unwrap()], "www.example");
let x =     "example.co.uk" ; assert_eq!(&x[DomainDetails::parse(x).unwrap().not_domain_suffix_bounds().unwrap()],     "example");
let x = "www.example.co.uk" ; assert_eq!(&x[DomainDetails::parse(x).unwrap().not_domain_suffix_bounds().unwrap()], "www.example");
let x =     "example.com."  ; assert_eq!(&x[DomainDetails::parse(x).unwrap().not_domain_suffix_bounds().unwrap()],     "example");
let x = "www.example.com."  ; assert_eq!(&x[DomainDetails::parse(x).unwrap().not_domain_suffix_bounds().unwrap()], "www.example");
let x =     "example.co.uk."; assert_eq!(&x[DomainDetails::parse(x).unwrap().not_domain_suffix_bounds().unwrap()],     "example");
let x = "www.example.co.uk."; assert_eq!(&x[DomainDetails::parse(x).unwrap().not_domain_suffix_bounds().unwrap()], "www.example");
Source

pub fn domain_middle_bounds(&self) -> Option<(Bound<usize>, Bound<usize>)>

The bounds of domain middle.

§Examples
use better_url::*;

let x =     "example.com"   ; assert_eq!(&x[DomainDetails::parse(x).unwrap().domain_middle_bounds().unwrap()], "example");
let x = "www.example.com"   ; assert_eq!(&x[DomainDetails::parse(x).unwrap().domain_middle_bounds().unwrap()], "example");
let x =     "example.co.uk" ; assert_eq!(&x[DomainDetails::parse(x).unwrap().domain_middle_bounds().unwrap()], "example");
let x = "www.example.co.uk" ; assert_eq!(&x[DomainDetails::parse(x).unwrap().domain_middle_bounds().unwrap()], "example");
let x =     "example.com."  ; assert_eq!(&x[DomainDetails::parse(x).unwrap().domain_middle_bounds().unwrap()], "example");
let x = "www.example.com."  ; assert_eq!(&x[DomainDetails::parse(x).unwrap().domain_middle_bounds().unwrap()], "example");
let x =     "example.co.uk."; assert_eq!(&x[DomainDetails::parse(x).unwrap().domain_middle_bounds().unwrap()], "example");
let x = "www.example.co.uk."; assert_eq!(&x[DomainDetails::parse(x).unwrap().domain_middle_bounds().unwrap()], "example");
Source

pub fn reg_domain_bounds(&self) -> Option<(Bound<usize>, Bound<usize>)>

The bounds of reg domain.

Notably does not include Self::fqdn_period

§Examples
use better_url::*;

let x =     "example.com"   ; assert_eq!(&x[DomainDetails::parse(x).unwrap().reg_domain_bounds().unwrap()], "example.com"  );
let x = "www.example.com"   ; assert_eq!(&x[DomainDetails::parse(x).unwrap().reg_domain_bounds().unwrap()], "example.com"  );
let x =     "example.co.uk" ; assert_eq!(&x[DomainDetails::parse(x).unwrap().reg_domain_bounds().unwrap()], "example.co.uk");
let x = "www.example.co.uk" ; assert_eq!(&x[DomainDetails::parse(x).unwrap().reg_domain_bounds().unwrap()], "example.co.uk");
let x =     "example.com."  ; assert_eq!(&x[DomainDetails::parse(x).unwrap().reg_domain_bounds().unwrap()], "example.com"  );
let x = "www.example.com."  ; assert_eq!(&x[DomainDetails::parse(x).unwrap().reg_domain_bounds().unwrap()], "example.com"  );
let x =     "example.co.uk."; assert_eq!(&x[DomainDetails::parse(x).unwrap().reg_domain_bounds().unwrap()], "example.co.uk");
let x = "www.example.co.uk."; assert_eq!(&x[DomainDetails::parse(x).unwrap().reg_domain_bounds().unwrap()], "example.co.uk");
Source

pub fn domain_suffix_bounds(&self) -> Option<(Bound<usize>, Bound<usize>)>

The bounds of domain suffix.

Notably does not include Self::fqdn_period

§Examples
use better_url::*;

let x =     "example.com"   ; assert_eq!(&x[DomainDetails::parse(x).unwrap().domain_suffix_bounds().unwrap()], "com"  );
let x = "www.example.com"   ; assert_eq!(&x[DomainDetails::parse(x).unwrap().domain_suffix_bounds().unwrap()], "com"  );
let x =     "example.co.uk" ; assert_eq!(&x[DomainDetails::parse(x).unwrap().domain_suffix_bounds().unwrap()], "co.uk");
let x = "www.example.co.uk" ; assert_eq!(&x[DomainDetails::parse(x).unwrap().domain_suffix_bounds().unwrap()], "co.uk");
let x =     "example.com."  ; assert_eq!(&x[DomainDetails::parse(x).unwrap().domain_suffix_bounds().unwrap()], "com"  );
let x = "www.example.com."  ; assert_eq!(&x[DomainDetails::parse(x).unwrap().domain_suffix_bounds().unwrap()], "com"  );
let x =     "example.co.uk."; assert_eq!(&x[DomainDetails::parse(x).unwrap().domain_suffix_bounds().unwrap()], "co.uk");
let x = "www.example.co.uk."; assert_eq!(&x[DomainDetails::parse(x).unwrap().domain_suffix_bounds().unwrap()], "co.uk");
Source

pub fn is_fqdn(&self) -> bool

If Self describes a fully qualified domain name, return true.

§Examples
use better_url::*;

assert!(!DomainDetails::parse(    "example.com"   ).unwrap().is_fqdn());
assert!(!DomainDetails::parse("www.example.com"   ).unwrap().is_fqdn());
assert!(!DomainDetails::parse(    "example.co.uk" ).unwrap().is_fqdn());
assert!(!DomainDetails::parse("www.example.co.uk" ).unwrap().is_fqdn());
assert!( DomainDetails::parse(    "example.com."  ).unwrap().is_fqdn());
assert!( DomainDetails::parse("www.example.com."  ).unwrap().is_fqdn());
assert!( DomainDetails::parse(    "example.co.uk.").unwrap().is_fqdn());
assert!( DomainDetails::parse("www.example.co.uk.").unwrap().is_fqdn());

Trait Implementations§

Source§

impl Clone for DomainDetails

Source§

fn clone(&self) -> DomainDetails

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 DomainDetails

Source§

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

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

impl From<DomainDetails> for HostDetails

Source§

fn from(value: DomainDetails) -> Self

Converts to this type from the input type.
Source§

impl FromStr for DomainDetails

Source§

type Err = GetDomainDetailsError

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 PartialEq for DomainDetails

Source§

fn eq(&self, other: &DomainDetails) -> 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 TryFrom<HostDetails> for DomainDetails

Source§

type Error = HostIsNotDomain

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

fn try_from(value: HostDetails) -> Result<DomainDetails, Self::Error>

Performs the conversion.
Source§

impl Copy for DomainDetails

Source§

impl Eq for DomainDetails

Source§

impl StructuralPartialEq for DomainDetails

Auto Trait Implementations§

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, 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> ErasedDestructor for T
where T: 'static,