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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use crate::misc::{MiscDetails, MiscError};
use crate::mx::{MxDetails, MxError};
use crate::smtp::{SmtpDetails, SmtpError};
use crate::syntax::SyntaxDetails;
use serde::{ser::SerializeMap, Serialize, Serializer};
#[derive(Debug, Clone)]
pub struct CheckEmailInputProxy {
pub host: String,
pub port: u16,
}
#[derive(Debug, Clone)]
pub struct CheckEmailInput {
pub to_emails: Vec<String>,
pub from_email: String,
pub hello_name: String,
pub proxy: Option<CheckEmailInputProxy>,
}
impl CheckEmailInput {
pub fn new(to_emails: Vec<String>) -> CheckEmailInput {
CheckEmailInput {
to_emails,
from_email: "user@example.org".into(),
hello_name: "localhost".into(),
proxy: None,
}
}
pub fn from_email(&mut self, email: String) -> &mut CheckEmailInput {
self.from_email = email;
self
}
pub fn hello_name(&mut self, name: String) -> &mut CheckEmailInput {
self.hello_name = name;
self
}
pub fn proxy(&mut self, proxy_host: String, proxy_port: u16) -> &mut CheckEmailInput {
self.proxy = Some(CheckEmailInputProxy {
host: proxy_host,
port: proxy_port,
});
self
}
}
#[derive(Debug, PartialEq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Reachable {
Safe,
Risky,
Invalid,
Unknown,
}
#[derive(Debug)]
pub struct CheckEmailOutput {
pub input: String,
pub is_reachable: Reachable,
pub misc: Result<MiscDetails, MiscError>,
pub mx: Result<MxDetails, MxError>,
pub smtp: Result<SmtpDetails, SmtpError>,
pub syntax: SyntaxDetails,
}
impl Default for CheckEmailOutput {
fn default() -> Self {
CheckEmailOutput {
input: String::default(),
is_reachable: Reachable::Unknown,
misc: Ok(MiscDetails::default()),
mx: Ok(MxDetails::default()),
smtp: Ok(SmtpDetails::default()),
syntax: SyntaxDetails::default(),
}
}
}
impl Serialize for CheckEmailOutput {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
#[derive(Serialize)]
struct MyError<E> {
error: E,
}
let mut map = serializer.serialize_map(Some(1))?;
map.serialize_entry("input", &self.input)?;
map.serialize_entry("is_reachable", &self.is_reachable)?;
match &self.misc {
Ok(t) => map.serialize_entry("misc", &t)?,
Err(error) => map.serialize_entry("misc", &MyError { error })?,
}
match &self.mx {
Ok(t) => map.serialize_entry("mx", &t)?,
Err(error) => map.serialize_entry("mx", &MyError { error })?,
}
match &self.smtp {
Ok(t) => map.serialize_entry("smtp", &t)?,
Err(error) => map.serialize_entry("smtp", &MyError { error })?,
}
map.serialize_entry("syntax", &self.syntax)?;
map.end()
}
}