Struct dominion::body::name::Name

source ·
pub struct Name<'a> { /* private fields */ }
Expand description

A domain name represented as an inverted list of labels.

Implementations§

source§

impl<'a> Name<'a>

source

pub fn parse(buff: &'a [u8], pos: usize) -> Result<(Name<'a>, usize), ParseError>

Parse from the specified buff, starting at position pos.

Errors

It will error if the buffer does not contain a valid domain name. If the domain name has been compressed the buffer should include all previous bytes from the DNS packet to be considered valid. Jump pointers should only point backwards inside the buf.

source

pub fn serialize(&self, packet: &mut Vec<u8, Global>)

Serialize the Name and append it tho the end of the provided packet

source

pub fn new() -> Name<'a>

Create a new, empty, domain name.

let name = Name::new();
assert_eq!(name.to_string(), "".to_string())
source

pub fn tld(&self) -> Option<&str>

Obtain the top level domain (TLD) of the provided domain name.

let mut name = Name::try_from("example.com").unwrap();
assert_eq!(name.tld(), Some("com"))
source

pub fn push_label(&mut self, label: Cow<'a, str>) -> Result<(), NameError>

Push a new label to the end of the domain name, as a subdomain of the current one.

Error

Will error if the label is not a valid DNS label, or if the resulting Domain name is too big.

let mut name = Name::new();
name.push_label("com".into()).unwrap();
name.push_label("example".into()).unwrap();
assert_eq!(name.to_string(), "example.com.".to_string())
source

pub fn label_count(&self) -> usize

Get the number of labels in the domain name.

let mut name = Name::try_from("example.com").unwrap();
assert_eq!(2, name.label_count())
source

pub fn is_subdomain(&self, sub: &Name<'_>) -> bool

Check if sub is a subdomain of the current domain name.

let mut name = Name::try_from("example.com").unwrap();
let mut sub = Name::try_from("subdomain.example.com").unwrap();

assert!(name.is_subdomain(&sub))
source

pub fn iter_human(&self) -> impl DoubleEndedIterator

Return an iterator over the labels in human order.

let mut name = Name::try_from("subdomain.example.com").unwrap();
let mut human = name.iter_human();

assert_eq!(human.next(), Some("subdomain"));
assert_eq!(human.next(), Some("example"));
assert_eq!(human.next(), Some("com"));
source

pub fn iter_hierarchy(&self) -> impl DoubleEndedIterator

Return an iterator over the labels in hierarchical order.

let mut name = Name::try_from("subdomain.example.com").unwrap();
let mut hierarchy = name.iter_hierarchy();

assert_eq!(hierarchy.next(), Some("com"));
assert_eq!(hierarchy.next(), Some("example"));
assert_eq!(hierarchy.next(), Some("subdomain"));

Trait Implementations§

source§

impl<'a> Clone for Name<'a>

source§

fn clone(&self) -> Name<'a>

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 Debug for Name<'_>

source§

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

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

impl Default for Name<'_>

source§

fn default() -> Name<'_>

Returns the “default value” for a type. Read more
source§

impl Display for Name<'_>

source§

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

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

impl<'a> TryFrom<&'a str> for Name<'a>

§

type Error = NameError

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

fn try_from(
    value: &'a str
) -> Result<Name<'a>, <Name<'a> as TryFrom<&'a str>>::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for Name<'a>

§

impl<'a> Send for Name<'a>

§

impl<'a> Sync for Name<'a>

§

impl<'a> Unpin for Name<'a>

§

impl<'a> UnwindSafe for Name<'a>

Blanket Implementations§

source§

impl<T> Any for Twhere
    T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere
    U: From<T>,

const: unstable · 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 Twhere
    T: Clone,

§

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 Twhere
    T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere
    U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere
    U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.