messagebird_async/sms/
message.rs1use super::*;
2
3#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
7#[serde(rename = "direction")]
8pub enum Direction {
9 #[serde(rename = "mt")]
10 SendToMobile,
11 #[serde(rename = "mo")]
12 ReceivedFromMobile,
13 #[serde(rename = "invalid")]
14 Invalid,
15}
16
17impl Direction {
18 pub fn as_str(&self) -> &str {
19 match self {
20 Direction::SendToMobile => "mt",
21 Direction::ReceivedFromMobile => "mo",
22 _ => "invalid",
23 }
24 }
25}
26
27impl FromStr for Direction {
28 type Err = MessageBirdError;
29 fn from_str(s: &str) -> Result<Self, Self::Err> {
30 serde_plain::from_str::<Self>(s).map_err(|_e| MessageBirdError::ParseError)
31 }
32}
33
34impl ToString for Direction {
35 fn to_string(&self) -> String {
36 serde_plain::to_string(self).unwrap()
37 }
38}
39
40#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
47pub struct Gateway(pub u32);
48
49impl Deref for Gateway {
50 type Target = u32;
51 fn deref(&self) -> &Self::Target {
52 &self.0
53 }
54}
55
56impl FromStr for Gateway {
57 type Err = MessageBirdError;
58 fn from_str(s: &str) -> Result<Self, Self::Err> {
59 serde_plain::from_str::<Self>(s).map_err(|_e| MessageBirdError::ParseError)
60 }
61}
62
63impl ToString for Gateway {
64 fn to_string(&self) -> String {
65 serde_plain::to_string(self).unwrap()
66 }
67}
68
69#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
80#[serde(rename_all = "camelCase")]
81pub struct Message {
82 id: Identifier,
83 href: Option<CallbackUrl>,
84 direction: Direction,
85 #[serde(rename = "type")]
86 payload_type: PayloadType,
87 originator: Option<Originator>,
88 #[serde(rename = "body")]
89 payload: Payload,
90 reference: Option<String>,
91 #[serde(flatten)]
92 report_url: Option<CallbackUrl>,
93 validity: Option<Duration>,
94 gateway: Option<Gateway>,
95 #[serde(rename = "typeDetails")]
96 details: TypeDetails,
97 #[serde(rename = "datacoding")]
98 payload_encoding: PayloadEncoding,
99 #[serde(rename = "mclass")]
100 class: MessageClass,
101 scheduled_datetime: Option<DateTime>,
102 created_datetime: Option<DateTime>,
103 recipients: Recipients,
104}
105
106#[cfg(test)]
107mod tests {
108
109 use super::*;
110 static RAW: &str = r#"
111{
112 "id":"e8077d803532c0b5937c639b60216938",
113 "href":"https://rest.messagebird.com/messages/e8077d803532c0b5937c639b60216938",
114 "direction":"mt",
115 "type":"sms",
116 "originator":"YourName",
117 "body":"This is a test message",
118 "reference":null,
119 "validity":null,
120 "gateway":null,
121 "typeDetails":{},
122 "datacoding":"plain",
123 "mclass":1,
124 "scheduledDatetime":null,
125 "createdDatetime":"2016-05-03T14:26:57+00:00",
126 "recipients":{
127 "totalCount":1,
128 "totalSentCount":1,
129 "totalDeliveredCount":0,
130 "totalDeliveryFailedCount":0,
131 "items":[
132 {
133 "recipient":31612345678,
134 "status":"sent",
135 "statusDatetime":"2016-05-03T14:26:57+00:00"
136 }
137 ]
138 }
139}
140"#;
141
142 deser_roundtrip!(message_deser, Message, RAW);
143}