use crate::{
Error, Result,
bytes::Cursor,
message::reader::{Labels, read_domain_name},
names::{InlineName, Name},
};
#[derive(Debug, Clone)]
pub struct NameRef<'a> {
cursor: Cursor<'a>,
}
impl<'a> NameRef<'a> {
#[allow(dead_code)]
#[inline]
pub(crate) fn new(cursor: Cursor<'a>) -> NameRef<'a> {
NameRef { cursor }
}
#[inline]
pub fn labels(&self) -> Labels<'a> {
Labels::new(self.cursor.clone())
}
pub fn eq(&self, other: &Self) -> Result<bool> {
let mut my_labels = self.labels();
let mut other_labels = other.labels();
loop {
let mo = my_labels.next();
let oo = other_labels.next();
match (mo, oo) {
(Some(mr), Some(or)) => {
let ml = mr?;
let ol = or?;
if ml.pos == ol.pos {
break Ok(true);
} else if !ml.bytes.eq_ignore_ascii_case(ol.bytes) {
break Ok(false);
} }
(None, None) => break Ok(true),
_ => break Ok(false),
}
}
}
#[inline]
pub fn ne(&self, other: &Self) -> Result<bool> {
Ok(!self.eq(other)?)
}
}
impl TryFrom<NameRef<'_>> for Name {
type Error = Error;
#[inline]
fn try_from(mut value: NameRef<'_>) -> Result<Self> {
read_domain_name(&mut value.cursor)
}
}
impl TryFrom<&NameRef<'_>> for Name {
type Error = Error;
#[inline]
fn try_from(value: &NameRef<'_>) -> Result<Self> {
read_domain_name(&mut value.cursor.clone())
}
}
impl TryFrom<NameRef<'_>> for InlineName {
type Error = Error;
#[inline]
fn try_from(mut value: NameRef<'_>) -> Result<Self> {
read_domain_name(&mut value.cursor)
}
}
impl TryFrom<&NameRef<'_>> for InlineName {
type Error = Error;
#[inline]
fn try_from(value: &NameRef<'_>) -> Result<Self> {
read_domain_name(&mut value.cursor.clone())
}
}