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
345
346
347
348
349
350
//! Details of the `response` section of the procedural macro.
use proc_macro2::{Span, TokenStream};
use quote::{quote, quote_spanned, ToTokens};
use syn::{spanned::Spanned, Field, Ident, Lit, Meta, NestedMeta};
use crate::api::strip_serde_attrs;
/// The result of processing the `request` section of the macro.
pub struct Response {
/// The fields of the response.
fields: Vec<ResponseField>,
}
impl Response {
/// Whether or not this response has any data in the HTTP body.
pub fn has_body_fields(&self) -> bool {
self.fields.iter().any(|field| field.is_body())
}
/// Whether or not this response has any fields.
pub fn has_fields(&self) -> bool {
!self.fields.is_empty()
}
/// Whether or not this response has any data in HTTP headers.
pub fn has_header_fields(&self) -> bool {
self.fields.iter().any(|field| field.is_header())
}
/// Whether or not this response has any data in the HTTP body.
pub fn has_body(&self) -> bool {
self.fields.iter().any(|field| !field.is_header())
}
/// Produces code for a request struct initializer.
pub fn init_fields(&self) -> TokenStream {
let fields = self
.fields
.iter()
.map(|response_field| match *response_field {
ResponseField::Body(ref field) => {
let field_name = field
.ident
.clone()
.expect("expected field to have an identifier");
let span = field.span();
quote_spanned! {span=>
#field_name: response_body.#field_name
}
}
ResponseField::Header(ref field, ref header) => {
let field_name = field
.ident
.clone()
.expect("expected field to have an identifier");
let header_name = Ident::new(header.as_ref(), Span::call_site());
let span = field.span();
quote_spanned! {span=>
#field_name: headers.remove(::http::header::#header_name)
.expect("response missing expected header")
.to_str()
.expect("failed to convert HeaderValue to str")
.to_owned()
}
}
ResponseField::NewtypeBody(ref field) => {
let field_name = field
.ident
.clone()
.expect("expected field to have an identifier");
let span = field.span();
quote_spanned! {span=>
#field_name: response_body
}
}
});
quote! {
#(#fields,)*
}
}
/// Produces code to add necessary HTTP headers to an `http::Response`.
pub fn apply_header_fields(&self) -> TokenStream {
let header_calls = self.fields.iter().filter_map(|response_field| {
if let ResponseField::Header(ref field, ref header) = *response_field {
let field_name = field
.ident
.as_ref()
.expect("expected field to have an identifier");
let header_name = Ident::new(header.as_ref(), Span::call_site());
let span = field.span();
Some(quote_spanned! {span=>
.header(::http::header::#header_name, response.#field_name)
})
} else {
None
}
});
quote! {
#(#header_calls)*
}
}
/// Produces code to initialize the struct that will be used to create the response body.
pub fn to_body(&self) -> TokenStream {
if let Some(field) = self.newtype_body_field() {
let field_name = field
.ident
.as_ref()
.expect("expected field to have an identifier");
let span = field.span();
quote_spanned!(span=> response.#field_name)
} else {
let fields = self.fields.iter().filter_map(|response_field| {
if let ResponseField::Body(ref field) = *response_field {
let field_name = field
.ident
.as_ref()
.expect("expected field to have an identifier");
let span = field.span();
Some(quote_spanned! {span=>
#field_name: response.#field_name
})
} else {
None
}
});
quote! {
ResponseBody {
#(#fields),*
}
}
}
}
/// Gets the newtype body field, if this request has one.
pub fn newtype_body_field(&self) -> Option<&Field> {
for response_field in self.fields.iter() {
match *response_field {
ResponseField::NewtypeBody(ref field) => {
return Some(field);
}
_ => continue,
}
}
None
}
}
impl From<Vec<Field>> for Response {
fn from(fields: Vec<Field>) -> Self {
let mut has_newtype_body = false;
let fields = fields.into_iter().map(|mut field| {
let mut field_kind = ResponseFieldKind::Body;
let mut header = None;
field.attrs = field.attrs.into_iter().filter(|attr| {
let meta = attr.interpret_meta()
.expect("ruma_api! could not parse response field attributes");
let meta_list = match meta {
Meta::List(meta_list) => meta_list,
_ => return true,
};
if &meta_list.ident.to_string() != "ruma_api" {
return true;
}
for nested_meta_item in meta_list.nested {
match nested_meta_item {
NestedMeta::Meta(meta_item) => {
match meta_item {
Meta::Word(ident) => {
match &ident.to_string()[..] {
"body" => {
has_newtype_body = true;
field_kind = ResponseFieldKind::NewtypeBody;
}
_ => panic!("ruma_api! single-word attribute on responses must be: body"),
}
}
Meta::NameValue(name_value) => {
match &name_value.ident.to_string()[..] {
"header" => {
match name_value.lit {
Lit::Str(lit_str) => header = Some(lit_str.value()),
_ => panic!("ruma_api! header attribute's value must be a string literal"),
}
field_kind = ResponseFieldKind::Header;
}
_ => panic!("ruma_api! name/value pair attribute on requests must be: header"),
}
}
_ => panic!("ruma_api! attributes on responses must be a single word or a name/value pair"),
}
}
NestedMeta::Literal(_) => panic!(
"ruma_api! attribute meta item on responses must be: header"
),
}
}
false
}).collect();
match field_kind {
ResponseFieldKind::Body => {
if has_newtype_body {
panic!("ruma_api! responses cannot have both normal body fields and a newtype body field");
} else {
ResponseField::Body(field)
}
}
ResponseFieldKind::Header => ResponseField::Header(field, header.expect("missing header name")),
ResponseFieldKind::NewtypeBody => ResponseField::NewtypeBody(field),
}
}).collect();
Self { fields }
}
}
impl ToTokens for Response {
fn to_tokens(&self, tokens: &mut TokenStream) {
let response_struct_header = quote! {
#[derive(Debug, Clone)]
pub struct Response
};
let response_struct_body = if self.fields.is_empty() {
quote!(;)
} else {
let fields = self.fields.iter().map(|response_field| {
let field = response_field.field();
let span = field.span();
let stripped_field = strip_serde_attrs(field);
quote_spanned!(span=> #stripped_field)
});
quote! {
{
#(#fields),*
}
}
};
let response_body_struct = if let Some(newtype_body_field) = self.newtype_body_field() {
let field = newtype_body_field.clone();
let ty = &field.ty;
let span = field.span();
quote_spanned! {span=>
/// Data in the response body.
#[derive(Debug, Deserialize, Serialize)]
struct ResponseBody(#ty);
}
} else if self.has_body_fields() {
let fields = self
.fields
.iter()
.filter_map(|response_field| match *response_field {
ResponseField::Body(ref field) => {
let span = field.span();
Some(quote_spanned!(span=> #field))
}
_ => None,
});
quote! {
/// Data in the response body.
#[derive(Debug, Deserialize, Serialize)]
struct ResponseBody {
#(#fields),*
}
}
} else {
TokenStream::new()
};
let response = quote! {
#response_struct_header
#response_struct_body
#response_body_struct
};
response.to_tokens(tokens);
}
}
/// The types of fields that a response can have.
pub enum ResponseField {
/// JSON data in the body of the response.
Body(Field),
/// Data in an HTTP header.
Header(Field, String),
/// A specific data type in the body of the response.
NewtypeBody(Field),
}
impl ResponseField {
/// Gets the inner `Field` value.
fn field(&self) -> &Field {
match *self {
ResponseField::Body(ref field)
| ResponseField::Header(ref field, _)
| ResponseField::NewtypeBody(ref field) => field,
}
}
/// Whether or not this response field is a body kind.
fn is_body(&self) -> bool {
match *self {
ResponseField::Body(_) => true,
_ => false,
}
}
/// Whether or not this response field is a header kind.
fn is_header(&self) -> bool {
match *self {
ResponseField::Header(..) => true,
_ => false,
}
}
}
/// The types of fields that a response can have, without their values.
enum ResponseFieldKind {
/// See the similarly named variant of `ResponseField`.
Body,
/// See the similarly named variant of `ResponseField`.
Header,
/// See the similarly named variant of `ResponseField`.
NewtypeBody,
}