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
extern crate chrono;
extern crate reqwest;
#[macro_use]
extern crate serde_derive;
pub mod email;
pub mod validation;
pub use reqwest::Error as ReqError;
const MAILGUN_DEFAULT_API: &str = "https://api.mailgun.net/v3";
pub type MailgunResult<T> = Result<T, ReqError>;
pub struct Credentials {
api_base: String,
api_key: String,
domain: String,
}
impl Credentials {
pub fn new<A: AsRef<str>, D: AsRef<str>>(api_key: A, domain: D) -> Self {
Self::with_base(MAILGUN_DEFAULT_API, api_key, domain)
}
pub fn with_base<B: AsRef<str>, A: AsRef<str>, D: AsRef<str>>(
api_base: B,
api_key: A,
domain: D,
) -> Self {
let api_base = api_base.as_ref();
let api_key = api_key.as_ref();
let domain = domain.as_ref();
assert!(
api_base.starts_with("http"),
"Domain does not start with http"
);
assert!(
api_base.chars().filter(|c| *c == '.').count() >= 1,
"api_base does not contain any dots"
);
assert!(api_key.len() >= 50, "api_key is to short");
assert!(
domain.chars().filter(|c| *c == '.').count() >= 1,
"Domain does not contain any dots"
);
Credentials {
api_base: api_base.to_string(),
api_key: api_key.to_string(),
domain: domain.to_string(),
}
}
pub fn domain(&self) -> &str {
&self.domain
}
}
pub struct EmailAddress {
name: Option<String>,
address: String,
}
impl EmailAddress {
pub fn address(address: &str) -> Self {
EmailAddress {
name: None,
address: address.to_string(),
}
}
pub fn name_address(name: &str, address: &str) -> Self {
EmailAddress {
name: Some(name.to_string()),
address: address.to_string(),
}
}
pub fn email(&self) -> &str {
&self.address
}
pub fn to_string(&self) -> String {
match self.name {
Some(ref name) => format!("{} <{}>", name, self.address),
None => self.address.clone(),
}
}
}