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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
use crate::{
errors::*,
ToParams,
};
use std::str::FromStr;
#[derive(Debug, Clone, PartialEq)]
pub struct AppId(String);
#[derive(Debug, Clone, PartialEq)]
pub struct AppKey(String);
#[derive(Debug, Clone, PartialEq)]
pub struct UserKey(String);
#[derive(Debug, Clone, PartialEq)]
pub struct OAuthToken(String);
impl AsRef<str> for AppId {
fn as_ref(&self) -> &str {
self.0.as_str()
}
}
impl AsRef<str> for AppKey {
fn as_ref(&self) -> &str {
self.0.as_str()
}
}
impl AsRef<str> for UserKey {
fn as_ref(&self) -> &str {
self.0.as_str()
}
}
impl AsRef<str> for OAuthToken {
fn as_ref(&self) -> &str {
self.0.as_str()
}
}
impl FromStr for AppId {
type Err = Error;
fn from_str(s: &str) -> Result<AppId> {
Ok(AppId(s.into()))
}
}
impl FromStr for AppKey {
type Err = Error;
fn from_str(s: &str) -> Result<AppKey> {
Ok(AppKey(s.into()))
}
}
impl FromStr for UserKey {
type Err = Error;
fn from_str(s: &str) -> Result<UserKey> {
Ok(UserKey(s.into()))
}
}
impl FromStr for OAuthToken {
type Err = Error;
fn from_str(s: &str) -> Result<OAuthToken> {
Ok(OAuthToken(s.into()))
}
}
impl<'a> From<&'a str> for AppId where Self: FromStr
{
fn from(s: &'a str) -> AppId {
s.parse().unwrap()
}
}
impl<'a> From<&'a str> for AppKey where Self: FromStr
{
fn from(s: &'a str) -> AppKey {
s.parse().unwrap()
}
}
impl<'a> From<&'a str> for UserKey where Self: FromStr
{
fn from(s: &'a str) -> UserKey {
s.parse().unwrap()
}
}
impl<'a> From<&'a str> for OAuthToken where Self: FromStr
{
fn from(s: &'a str) -> OAuthToken {
s.parse().unwrap()
}
}
impl From<String> for AppId {
fn from(s: String) -> AppId {
AppId(s)
}
}
impl From<String> for AppKey {
fn from(s: String) -> AppKey {
AppKey(s)
}
}
impl From<String> for UserKey {
fn from(s: String) -> UserKey {
UserKey(s)
}
}
impl From<String> for OAuthToken {
fn from(s: String) -> OAuthToken {
OAuthToken(s)
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Application {
AppId(AppId, Option<AppKey>),
UserKey(UserKey),
OAuthToken(OAuthToken),
}
impl From<AppId> for Application {
fn from(a: AppId) -> Self {
Application::AppId(a, None)
}
}
impl From<(AppId, AppKey)> for Application {
fn from(a: (AppId, AppKey)) -> Self {
Application::AppId(a.0, Some(a.1))
}
}
impl From<UserKey> for Application {
fn from(u: UserKey) -> Self {
Application::UserKey(u)
}
}
impl From<OAuthToken> for Application {
fn from(o: OAuthToken) -> Self {
Application::OAuthToken(o)
}
}
impl Application {
pub fn from_app_id<T: Into<AppId>>(app_id: T) -> Self {
Application::AppId(app_id.into(), None)
}
pub fn from_app_id_and_key<T: Into<AppId>, U: Into<AppKey>>(app_id: T, app_key: U) -> Self {
Application::AppId(app_id.into(), Some(app_key.into()))
}
pub fn from_user_key<T: Into<UserKey>>(user_key: T) -> Self {
Application::UserKey(user_key.into())
}
pub fn from_oauth_token<T: Into<OAuthToken>>(token: T) -> Self {
Application::OAuthToken(token.into())
}
}
use std::borrow::Cow;
impl<'k, 'v, 'this, E> ToParams<'k, 'v, 'this, E> for Application
where 'this: 'k + 'v,
E: Extend<(Cow<'k, str>, &'v str)>
{
fn to_params_with_mangling<F: FnMut(Cow<'k, str>) -> Cow<'k, str>>(&'this self,
extendable: &mut E,
key_mangling: &mut F) {
use self::Application::*;
let params = match self {
AppId(app_id, app_key_opt) => [Some(("app_id", app_id.as_ref())),
app_key_opt.as_ref().map(|app_key| ("app_key", app_key.as_ref()))],
UserKey(user_key) => [Some(("user_key", user_key.as_ref())), None],
OAuthToken(token) => [Some(("access_token", token.as_ref())), None],
};
extendable.extend(params.iter()
.filter_map(|¶m| param.map(|(k, v)| (key_mangling(k.into()), v))));
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn convert_application_from_app_id() {
let app_id = AppId::from("my_app_id");
let app = Application::from(app_id.clone());
assert_eq!(Application::AppId(app_id, None), app);
}
#[test]
fn convert_application_from_app_id_app_key() {
let app_id_key = (AppId::from("my_app_id"), AppKey::from("my_app_key"));
let app = Application::from(app_id_key.clone());
assert_eq!(Application::AppId(app_id_key.0, Some(app_id_key.1)), app);
}
#[test]
fn convert_application_from_user_key() {
let user_key = UserKey::from("my_user_key");
let app = Application::from(user_key.clone());
assert_eq!(Application::UserKey(user_key), app);
}
#[test]
fn convert_application_from_oauth_token() {
let token = OAuthToken::from("my_oauth_token");
let app = Application::from(token.clone());
assert_eq!(Application::OAuthToken(token), app);
}
#[test]
fn transforms_app_id_into_params() {
let app_id = "my_app_id";
let app = Application::from_app_id(app_id);
let mut result = Vec::new();
app.to_params(&mut result);
let expected: Vec<(Cow<str>, &str)> = vec![("app_id".into(), app_id)];
assert_eq!(expected, result);
}
#[test]
fn transforms_app_id_and_key_into_params() {
let app_id = "my_app_id";
let key = "my_key";
let app = Application::from_app_id_and_key(app_id, key);
let mut result = Vec::new();
app.to_params(&mut result);
let expected: Vec<(Cow<str>, &str)> = vec![("app_id".into(), app_id), ("app_key".into(), key)];
assert_eq!(expected, result);
}
#[test]
fn transforms_user_key_into_params() {
let user_key = "my_user_key";
let app = Application::from_user_key(user_key);
let mut result = Vec::new();
app.to_params(&mut result);
let expected: Vec<(Cow<str>, &str)> = vec![("user_key".into(), user_key)];
assert_eq!(expected, result);
}
#[test]
fn transforms_oauth_token_into_params() {
let oauth_token = "my_token";
let app = Application::from_oauth_token(oauth_token);
let mut result = Vec::new();
app.to_params(&mut result);
let expected: Vec<(Cow<str>, &str)> = vec![("access_token".into(), oauth_token)];
assert_eq!(expected, result);
}
}