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
//! The macros here are for generating request builders and specifying typed relationships to API
//! paths and types.
//!
//! # Example
//!
//! View documentation on each for macro for more information on how each macro works.
//!
//! ```rust,ignore
//! // Common imports used by macros.
//! imports!();
//!
//! // Define request builder types.
//! new_builder!(
//! User,
//! Users,
//! );
//!
//! // Request builders we intend to extend must be imported separately.
//! use crate::client::GetQueryBuilder;
//!
//! // Generate `From<GetQueryBuilder>` implementations for our request builders.
//! from!(
//! @GetQuery
//! -> User,
//! -> Users,
//! );
//!
//! // Implement methods on GetQueryBuilder for accessing our new request builder types.
//! impl_builder!(
//! @GetQuery
//! -> users ["users"] -> Users,
//! => user ["users"] -> User = id,
//! );
//!
//! // Mark request builders as executable and specify the return type.
//! exec!(
//! User -> crate::models::User,
//! Users -> Vec<crate::models::User>,
//! );
//! ```
/// Necessary imports for when all macros are used within a file.
/// Specifies new request builders. Documentation is passed through.
///
/// # Example
///
/// ```
/// new_builder!(
/// User,
/// /// Documentation is passed through!
/// Users,
/// );
///
/// // Expands to ...
///
/// pub struct UserBuilder { ... }
/// unsafe impl Send for UserBuilder {}
/// /// Documentation is passed through!
/// pub struct UsersBuilder { ... }
/// unsafe impl Send for UsersBuilder {}
/// ```
unsafe
})*
);
}
/// Marks a request builder as executable by implementing the `Executor` trait and specifying a
/// return type. Documentation is passed through.
///
/// # Example
///
/// ```
/// exec!(
/// // builder return type (implements serde::Deserialize)
/// // || ||
/// // \/ \/
/// User -> models::User, // Returns a struct.
/// Users -> Vec<models::User>, // Returns an array of structs.
/// /// Documentation is passed through!
/// NoReturn -> (), // Returns no body.
/// );
///
/// // Expands to ...
///
/// impl Executor for UserBuilder {
/// type T = models::User;
///
/// async fn execute(self) -> Result<ApiResponse<models::User>, Box<dyn Error>> { ... }
/// }
/// impl Executor for UsersBuilder {
/// type T = models::User;
///
/// async fn execute(self) -> Result<ApiResponse<Vec<models::User>>, Box<dyn Error>> { ... }
/// }
/// /// Documentation is passed through!
/// impl Executor for NoReturnBuilder {
/// type T = ();
///
/// async fn execute(self) -> Result<ApiResponse<()>, Box<dyn Error>> { ... }
/// }
/// ```
;
Ok
}
}
)*}
);
}
/// Generates `From<T>` implementations for converting typed request builders into other typed
/// request builders. The implementations here are used in the `impl_builder!` macro. Documentation
/// is passed through for target builders.
///
/// # Example
///
/// ```
/// from!(
/// // source builder
/// // ||
/// // \/
/// @GetQuery
/// -> User, // <= target builder
/// /// Documentation is passed through!
/// -> Users,
/// );
///
/// // Expands to ...
///
/// impl From<GetQueryBuilder> for UserBuilder {
/// fn from(f: GetQueryBuilder) -> Self { ... }
/// }
/// /// Documentation is passed through!
/// impl From<GetQueryBuilder> for UsersBuilder {
/// fn from(f: GetQueryBuilder) -> Self { ... }
/// }
/// ```
}
}
})+)+
};
}
/// Generates code for extending request builders into other request builders. All builders must be
/// created by the `new_builder!` macro and have conversions specified by the `from!` macro.
///
/// # Example
///
/// ```
/// impl_builder!(
/// // source builder
/// // ||
/// // \/
/// @GetQuery
/// // There are two different types of impls we can generate:
/// // 1. `->` which generates an impl requiring no route variable.
/// // 2. `=>` which generates an impl requiring a route variable.
/// // The route variable will be appended to the provided route path.
/// //
/// // method name new builder
/// // || route path ||
/// // || || || route variable name
/// // \/ \/ \/ ||
/// -> users ["users"] -> Users, // ||
/// /// Docs are passed through too! \/
/// => user ["users"] -> User = id,
/// );
///
/// // Expands to ...
///
/// impl GetQueryBuilder {
/// pub fn users(mut self) -> UsersBuilder { ... }
/// /// Docs are passed through too!
/// pub fn user<T: ToString>(mut self, id: T) -> UserBuilder { ... }
/// }
/// ```
)?
// Case 2
$?
)*
}
})+
);
}
;
join_path!;
b
}
)?
})*
}
);
}