1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use {Domain, Suffix, Psl};
use serde::{Serialize, Deserialize, Serializer, Deserializer};
use serde::de::{Error, Unexpected};
#[derive(Psl, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
pub struct List;
impl List {
#[inline]
pub fn new() -> List { List }
}
impl<'a> Serialize for Domain<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
serializer.serialize_bytes(self.as_bytes())
}
}
impl<'a> Deserialize<'a> for Domain<'a> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'a>
{
let input = <&'a str as Deserialize<'a>>::deserialize(deserializer)?;
match List.domain(input) {
Some(domain) => { Ok(domain) }
None => {
let invalid = Unexpected::Str(input);
Err(Error::invalid_value(invalid, &"a domain name"))
}
}
}
}
impl<'a> Serialize for Suffix<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
serializer.serialize_bytes(self.as_bytes())
}
}
impl<'a> Deserialize<'a> for Suffix<'a> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'a>
{
let input = <&'a str as Deserialize<'a>>::deserialize(deserializer)?;
match List.suffix(input) {
Some(suffix) => { Ok(suffix) }
None => {
let invalid = Unexpected::Str(input);
Err(Error::invalid_value(invalid, &"a domain suffix"))
}
}
}
}