Name

Struct 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<(Self, 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>)

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

Source

pub fn new() -> Self

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<Item = &str>

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<Item = &str>

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

Source§

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

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

impl Default for Name<'_>

Source§

fn default() -> Self

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

impl Display for Name<'_>

Source§

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

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

impl From<Name<'_>> for Vec<u8>

Source§

fn from(name: Name<'_>) -> Self

Converts to this type from the input type.
Source§

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

Source§

type Error = NameError

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

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

Performs the conversion.

Auto Trait Implementations§

§

impl<'a> Freeze for Name<'a>

§

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 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.