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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use crate::syntax::SyntaxDetails;
use crate::util::ser_with_display::ser_with_display;
use async_std_resolver::{config, lookup::MxLookup, resolver, ResolveError};
use serde::{ser::SerializeMap, Serialize, Serializer};
use std::io::Error;
#[derive(Debug)]
pub struct MxDetails {
pub lookup: Result<MxLookup, ResolveError>,
}
impl Default for MxDetails {
fn default() -> Self {
MxDetails {
lookup: Err(ResolveError::from("Skipped")),
}
}
}
impl From<MxLookup> for MxDetails {
fn from(lookup: MxLookup) -> Self {
MxDetails { lookup: Ok(lookup) }
}
}
impl Serialize for MxDetails {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let records = self
.lookup
.as_ref()
.map(|lookup| {
lookup
.iter()
.map(|host| host.exchange().to_string())
.collect::<Vec<_>>()
})
.unwrap_or_else(|_| vec![]);
let mut map = serializer.serialize_map(Some(2))?;
map.serialize_entry("accepts_mail", &!records.is_empty())?;
map.serialize_entry("records", &records)?;
map.end()
}
}
#[derive(Debug, Serialize)]
#[serde(tag = "type", content = "message")]
pub enum MxError {
#[serde(serialize_with = "ser_with_display")]
IoError(Error),
#[serde(serialize_with = "ser_with_display")]
ResolveError(ResolveError),
}
impl From<ResolveError> for MxError {
fn from(error: ResolveError) -> Self {
MxError::ResolveError(error)
}
}
pub async fn check_mx(syntax: &SyntaxDetails) -> Result<MxDetails, MxError> {
let resolver = resolver(
config::ResolverConfig::default(),
config::ResolverOpts::default(),
)
.await?;
match resolver.mx_lookup(syntax.domain.as_ref()).await {
Ok(lookup) => Ok(MxDetails::from(lookup)),
Err(err) => Ok(MxDetails { lookup: Err(err) }),
}
}