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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
use lazy_static::lazy_static;
use serde::{Deserialize, Serialize};
pub type Url = String;
pub type JsonValue = Box<::serde_json::Value>;
pub type TimestampString = String;
macro_rules! define_string_wrapper {
($($(#[$meta:meta])* $vis:vis $name:ident;)*) => { $(
$(#[$meta])*
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
$vis struct $name(String);
impl $name {
pub fn new(id: String) -> Self {
$name(id)
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl AsRef<str> for $name {
fn as_ref(&self) -> &str {
self.as_str()
}
}
)* };
}
define_string_wrapper! {
pub DriveId;
pub ItemId;
pub Tag;
}
#[doc(hidden)]
pub trait ResourceField {
fn api_field_name(&self) -> &'static str;
}
macro_rules! define_resource_object {
($(
$(#[$meta:meta])*
$vis:vis struct $struct_name:ident #$field_enum_name:ident {
$(
$(#[$field_meta:meta])*
$([unselectable]
pub $unsel_field_name:ident
$(@$field_rename:literal)?
)?
$(pub $sel_field_name:ident)?
: Option<$field_ty:ty>,
)*
}
)*) => {
$(
$(#[$meta])*
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
$vis struct $struct_name {
$(
#[allow(missing_docs)]
$(#[$field_meta])*
$($(#[serde(rename = $field_rename)])? pub $unsel_field_name)?
$(pub $sel_field_name)?
: Option<$field_ty>,
)*
#[serde(default)]
_private: (),
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
$vis enum $field_enum_name {
$(
$(
#[allow(missing_docs, non_camel_case_types)]
$sel_field_name,
)?
)*
}
impl ResourceField for $field_enum_name {
#[inline]
fn api_field_name(&self) -> &'static str {
lazy_static! {
static ref FIELD_NAME_TABLE: Vec<String> = {
vec![
$($(snake_to_camel_case(stringify!($sel_field_name)),)?)*
]
};
}
&FIELD_NAME_TABLE[*self as usize]
}
}
)*
};
}
define_resource_object! {
#[derive(Debug)]
pub struct Drive #DriveField {
pub id: Option<DriveId>,
pub created_by: Option<JsonValue>,
pub created_date_time: Option<TimestampString>,
pub description: Option<String>,
pub drive_type: Option<JsonValue>,
pub items: Option<Vec<DriveItem>>,
pub last_modified_by: Option<JsonValue>,
pub last_modified_date_time: Option<TimestampString>,
pub name: Option<String>,
pub owner: Option<JsonValue>,
pub quota: Option<JsonValue>,
pub root: Option<DriveItem>,
pub sharepoint_ids: Option<JsonValue>,
pub special: Option<Vec<DriveItem>>,
pub system: Option<JsonValue>,
pub web_url: Option<Url>,
}
#[derive(Debug)]
pub struct DriveItem #DriveItemField {
pub audio: Option<JsonValue>,
pub content: Option<JsonValue>,
pub c_tag: Option<Tag>,
pub deleted: Option<JsonValue>,
pub description: Option<String>,
pub file: Option<JsonValue>,
pub file_system_info: Option<JsonValue>,
pub folder: Option<JsonValue>,
pub image: Option<JsonValue>,
pub location: Option<JsonValue>,
pub package: Option<JsonValue>,
pub photo: Option<JsonValue>,
pub publication: Option<JsonValue>,
pub remote_item: Option<JsonValue>,
pub root: Option<JsonValue>,
pub search_result: Option<JsonValue>,
pub shared: Option<JsonValue>,
pub sharepoint_ids: Option<JsonValue>,
pub size: Option<i64>,
pub special_folder: Option<JsonValue>,
pub video: Option<JsonValue>,
pub web_dav_url: Option<Url>,
pub children: Option<Vec<DriveItem>>,
pub created_by_user: Option<JsonValue>,
pub last_modified_by_user: Option<JsonValue>,
pub permissions: Option<JsonValue>,
pub thumbnails: Option<JsonValue>,
pub versions: Option<JsonValue>,
pub id: Option<ItemId>,
pub created_by: Option<JsonValue>,
pub created_date_time: Option<TimestampString>,
pub e_tag: Option<Tag>,
pub last_modified_by: Option<JsonValue>,
pub last_modified_date_time: Option<TimestampString>,
pub name: Option<String>,
pub parent_reference: Option<JsonValue>,
pub web_url: Option<Url>,
[unselectable]
pub download_url @"@microsoft.graph.downloadUrl": Option<Url>,
}
}
#[inline]
fn snake_to_camel_case(s: &str) -> String {
let mut buf = String::new();
let mut is_u = false;
for c in s.chars() {
if c == '_' {
is_u = true;
} else if is_u {
is_u = false;
buf.push(c.to_ascii_uppercase());
} else {
buf.push(c);
}
}
buf
}
#[allow(missing_docs)]
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ErrorObject {
pub code: Option<String>,
pub message: Option<String>,
pub inner_error: Option<Box<ErrorObject>>,
#[serde(flatten)]
pub extra_data: serde_json::Map<String, serde_json::Value>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_snake_to_camel_case() {
let data = [
("abc", "abc"),
("hello_world", "helloWorld"),
("wh_tst_ef_ck", "whTstEfCk"),
];
for (i, o) in &data {
assert_eq!(snake_to_camel_case(i), *o);
}
}
#[test]
fn test_api_field_name() {
assert_eq!(DriveField::id.api_field_name(), "id");
assert_eq!(DriveField::drive_type.api_field_name(), "driveType");
assert_eq!(DriveField::owner.api_field_name(), "owner");
assert_eq!(DriveField::web_url.api_field_name(), "webUrl");
assert_eq!(DriveItemField::id.api_field_name(), "id");
assert_eq!(
DriveItemField::file_system_info.api_field_name(),
"fileSystemInfo"
);
assert_eq!(DriveItemField::size.api_field_name(), "size");
assert_eq!(DriveItemField::web_dav_url.api_field_name(), "webDavUrl");
assert_eq!(DriveItemField::web_url.api_field_name(), "webUrl");
}
}