messagebird_async/sms/parameter/
types.rs1use super::*;
2
3use serde::ser::{Serialize, Serializer};
4
5use hyper;
6use std::fmt;
7use std::string::ToString;
8
9pub trait Query {
11 fn uri(&self) -> hyper::Uri;
12 fn method(&self) -> hyper::Method {
13 hyper::Method::GET
14 }
15}
16
17#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
21pub struct Contact(u64);
22
23impl Default for Contact {
24 fn default() -> Self {
25 Contact(0)
26 }
27}
28
29impl fmt::Display for Contact {
30 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31 write!(f, "Contact({})", self.0)
32 }
33}
34
35#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
41pub struct Group;
42
43impl Default for Group {
44 fn default() -> Self {
45 Group
46 }
47}
48
49impl fmt::Display for Group {
50 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
51 write!(f, "no group")
52 }
53}
54
55#[derive(Clone, Debug, Eq, PartialEq)]
60pub enum QueryRecipient {
61 Group(Group),
62 Msisdn(Msisdn),
63}
64
65impl From<Recipient> for QueryRecipient {
66 fn from(_recipient: Recipient) -> Self {
67 unimplemented!("TODO implement convenience conversion")
68 }
69}
70
71impl ToString for QueryRecipient {
72 fn to_string(&self) -> String {
73 match self {
74 QueryRecipient::Group(ref group) => group.to_string(),
75 QueryRecipient::Msisdn(ref msisdn) => msisdn.to_string(),
76 }
77 }
78}
79
80impl FromStr for QueryRecipient {
81 type Err = MessageBirdError;
82 fn from_str(_s: &str) -> Result<Self, Self::Err> {
83 unimplemented!("TODO implement deserialize and all of the Group API")
84 }
85}
86
87impl From<Msisdn> for QueryRecipient {
88 fn from(msisdn: Msisdn) -> Self {
89 QueryRecipient::Msisdn(msisdn)
90 }
91}
92
93impl From<Group> for QueryRecipient {
94 fn from(group: Group) -> Self {
95 QueryRecipient::Group(group)
96 }
97}
98
99impl Serialize for QueryRecipient {
100 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
101 where
102 S: Serializer,
103 {
104 let val_str = self.to_string();
105 serializer.serialize_str(val_str.as_str())
106 }
107}
108
109#[cfg(test)]
114mod tests {
115
116 #[derive(Debug, Serialize, Eq, PartialEq)]
117 struct DummyQuery<T> {
118 pub inner: T,
119 }
120
121 use super::*;
122 #[test]
123 fn recipient() {
124 let recipient: QueryRecipient = Msisdn::new(123475).unwrap().into();
125
126 let recipient = DummyQuery { inner: recipient };
127
128 let recipient_str = serde_url_params::to_string(&recipient).unwrap();
129 println!("recipient is {}", recipient_str);
130 }
131
132 #[test]
133 fn recipient_vec() {
134 let recipients: Vec<QueryRecipient> = vec![
135 Msisdn::new(123475).unwrap().into(),
136 Msisdn::new(777777777).unwrap().into(),
137 ];
138
139 let recipients = DummyQuery { inner: recipients };
140
141 let recipients_str = serde_url_params::to_string(&recipients).unwrap();
142 println!("recipients are \"{}\"", recipients_str);
143 }
144
145 #[test]
146 fn recipient_optional_some() {
147 let recipients: Option<QueryRecipient> = Some(Msisdn::new(123475).unwrap().into());
148
149 let recipients = DummyQuery { inner: recipients };
150
151 let recipients_str = serde_url_params::to_string(&recipients).unwrap();
152 println!("recipient is Some(...) => \"{}\"", recipients_str);
153 }
154
155 #[test]
156 fn recipient_optional_none() {
157 let recipients: Option<QueryRecipient> = None;
158
159 let recipients = DummyQuery { inner: recipients };
160
161 let recipients_str = serde_url_params::to_string(&recipients).unwrap();
162 println!("recipient is None => \"{}\"", recipients_str);
163 }
164}