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
167
168
use super::*;
#[derive(Copy, Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
#[serde(rename = "msisdn")]
pub struct Msisdn(u64);
impl Deref for Msisdn {
type Target = u64;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Msisdn {
pub fn new(raw: u64) -> Result<Self, MessageBirdError> {
if raw != 0 {
Ok(Msisdn(raw))
} else {
Err(MessageBirdError::TypeError {
msg: format!("Invalid phone number: {}", raw),
})
}
}
pub fn try_from(raw: u64) -> Result<Self, MessageBirdError> {
Msisdn::new(raw)
}
}
impl FromStr for Msisdn {
type Err = MessageBirdError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
serde_plain::from_str::<Self>(s).map_err(|_e| MessageBirdError::ParseError)
}
}
impl ToString for Msisdn {
fn to_string(&self) -> String {
serde_plain::to_string(self).unwrap()
}
}
#[derive(Copy, Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum Status {
Unknown,
Scheduled,
Sent,
Buffered,
Delivered,
Expired,
DeliveryFailed,
}
impl Status {
pub fn as_str(&self) -> &str {
match self {
Status::Scheduled => "scheduled",
Status::Sent => "sent",
Status::Buffered => "buffered",
Status::Delivered => "delivered",
Status::Expired => "expired",
Status::DeliveryFailed => "delivery_failed",
_ => "invalid",
}
}
}
impl FromStr for Status {
type Err = MessageBirdError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
serde_plain::from_str::<Self>(s).map_err(|_e| MessageBirdError::ParseError)
}
}
impl ToString for Status {
fn to_string(&self) -> String {
serde_plain::to_string(self).unwrap()
}
}
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Clone)]
#[serde(rename_all = "snake_case")]
pub struct Recipient {
#[serde(rename = "recipient")]
msisdn: Msisdn,
#[serde(rename = "status")]
status: Option<Status>,
#[serde(rename = "statusDatetime")]
status_datetime: Option<DateTime>,
}
impl Recipient {
pub fn new(number: u64) -> Self {
Self {
msisdn: Msisdn(number),
status: None,
status_datetime: None,
}
}
}
impl From<u64> for Recipient {
fn from(raw: u64) -> Self {
Recipient::new(raw)
}
}
impl FromStr for Recipient {
type Err = MessageBirdError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = s.replace('"', "");
s.parse::<u64>()
.and_then(|x: u64| Ok(Recipient::from(x)))
.map_err(|e| {
debug!("{:?}", e);
MessageBirdError::ParseError
})
}
}
#[cfg(test)]
mod test {
use super::*;
serde_roundtrip!(recipient_serde, Recipient, Recipient::new(1234512345));
static RAW_1: &str = r#"{
"recipient": 23747,
"status": "delivery_failed",
"statusDatetime" : "2016-05-03T14:26:57+00:00"
}"#;
deser_roundtrip!(recipient_sample1_deser, Recipient, RAW_1);
static RAW_3: &str = r#"{
"recipient": 23747,
"status": null,
"statusDatetime" : null
}"#;
deser_roundtrip!(recipient_sample2_deser, Recipient, RAW_3);
}