messagebird_async/sms/
message.rs

1use super::*;
2
3/// Determines if the direction of the message
4///
5/// Mostly useful for filtering messages with `ListParamters`/`RequestMessageList`
6#[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/// Determines the Gateway ID
41///
42/// Not very useful right now, recommended to not use unless you have explicit issues
43/// i.e. some base stations in south eastern europe happily convert binary SMS to
44/// textual SMS - because why not? In that case alternate routes might help to circumpass
45/// the issues.
46#[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// what is there to query
70// to send, only originator,body and recipients are mandatory
71// the rest is optional, but this would make it pretty annoying
72// to use with almost every member being optional
73
74/// BirdedMessage
75///
76/// A message as queried from the MessageBird API.
77/// Refer to `SendableMessage` for an object which can be
78/// sent.
79#[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}