91 lines
2 KiB
Rust
91 lines
2 KiB
Rust
|
use std::fmt;
|
||
|
|
||
|
#[derive(Clone, Eq, PartialEq)]
|
||
|
pub struct Name {
|
||
|
name: String
|
||
|
}
|
||
|
|
||
|
impl Name {
|
||
|
pub fn new(name: String) -> Self {
|
||
|
Name {
|
||
|
name,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn ends_with(&self, other: &Name) -> bool {
|
||
|
self.name == other.name || self.name.ends_with(&(String::from(".") + &other.name))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl fmt::Display for Name {
|
||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||
|
write!(f, "{}", self.name)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Ord for Name {
|
||
|
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||
|
let mut labels = self.name.split('.').rev();
|
||
|
let mut other_labels = other.name.split('.').rev();
|
||
|
|
||
|
loop {
|
||
|
match (labels.next(), other_labels.next()) {
|
||
|
(Some(label), Some(other_label)) => match label.cmp(other_label) {
|
||
|
std::cmp::Ordering::Equal => (),
|
||
|
res => return res,
|
||
|
},
|
||
|
(None, Some(_)) => return std::cmp::Ordering::Less,
|
||
|
(Some(_), None) => return std::cmp::Ordering::Greater,
|
||
|
(None, None) => return std::cmp::Ordering::Equal,
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl PartialOrd for Name {
|
||
|
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||
|
Some(self.cmp(other))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[derive(Eq, PartialEq)]
|
||
|
pub enum Rtype {
|
||
|
A,
|
||
|
Aaaa,
|
||
|
Cname,
|
||
|
Mx,
|
||
|
Ns,
|
||
|
Ptr,
|
||
|
Soa,
|
||
|
Srv,
|
||
|
Txt
|
||
|
}
|
||
|
|
||
|
impl Rtype {
|
||
|
pub fn value(&self) -> u16 {
|
||
|
match self {
|
||
|
Rtype::A => 1,
|
||
|
Rtype::Aaaa => 28,
|
||
|
Rtype::Cname => 5,
|
||
|
Rtype::Mx => 15,
|
||
|
Rtype::Ns => 2,
|
||
|
Rtype::Ptr => 12,
|
||
|
Rtype::Soa => 6,
|
||
|
Rtype::Srv => 33,
|
||
|
Rtype::Txt => 16,
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Ord for Rtype {
|
||
|
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||
|
self.value().cmp(&other.value())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl PartialOrd for Rtype {
|
||
|
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||
|
Some(self.cmp(other))
|
||
|
}
|
||
|
}
|