parent
cf3b11862c
commit
7718735215
@ -1,3 +1,72 @@ |
||||
pub mod checker; |
||||
pub mod config; |
||||
pub mod manager; |
||||
|
||||
use std::str::FromStr; |
||||
|
||||
use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; |
||||
use trust_dns_client::rr as trust_dns_rr; |
||||
|
||||
#[derive(Debug, Clone)] |
||||
/// Validated representation of a domain name
|
||||
pub struct Name { |
||||
inner: trust_dns_rr::Name, |
||||
utf8_str: String, |
||||
} |
||||
|
||||
impl Name { |
||||
fn as_str(&self) -> &str { |
||||
self.utf8_str.as_str() |
||||
} |
||||
} |
||||
|
||||
impl FromStr for Name { |
||||
type Err = <trust_dns_rr::Name as FromStr>::Err; |
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> { |
||||
let mut n = trust_dns_rr::Name::from_str(s)?; |
||||
let utf8_str = n.clone().to_utf8(); |
||||
|
||||
n.set_fqdn(true); |
||||
|
||||
Ok(Name { inner: n, utf8_str }) |
||||
} |
||||
} |
||||
|
||||
impl Serialize for Name { |
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
||||
where |
||||
S: Serializer, |
||||
{ |
||||
serializer.serialize_str(self.as_str()) |
||||
} |
||||
} |
||||
|
||||
struct NameVisitor; |
||||
|
||||
impl<'de> de::Visitor<'de> for NameVisitor { |
||||
type Value = Name; |
||||
|
||||
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { |
||||
write!(formatter, "a valid domain name") |
||||
} |
||||
|
||||
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E> |
||||
where |
||||
E: de::Error, |
||||
{ |
||||
match Name::from_str(s) { |
||||
Ok(n) => Ok(n), |
||||
Err(e) => Err(E::custom(format!("invalid domain name: {}", e))), |
||||
} |
||||
} |
||||
} |
||||
|
||||
impl<'de> Deserialize<'de> for Name { |
||||
fn deserialize<D>(deserializer: D) -> Result<Name, D::Error> |
||||
where |
||||
D: Deserializer<'de>, |
||||
{ |
||||
deserializer.deserialize_str(NameVisitor) |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue