api-macro: Fix fields without cfg attributes being emitted

This commit is contained in:
Devin Ragotzy 2021-07-04 07:55:51 -04:00
parent c9c536ed0b
commit 82fca86d70
2 changed files with 37 additions and 4 deletions

View File

@ -21,6 +21,9 @@ impl Request {
let incoming_request_type =
if self.contains_lifetimes() { quote!(IncomingRequest) } else { quote!(Request) };
// FIXME: the rest of the field initializer expansions are gated `cfg(...)`
// except this one. If we get errors about missing fields in IncomingRequest for
// a path field look here.
let (parse_request_path, path_vars) = if self.has_path_fields() {
let path_string = metadata.path.value();
@ -65,14 +68,23 @@ impl Request {
};
let (parse_query, query_vars) = if let Some(field) = self.query_map_field() {
let cfg_attrs =
field.attrs.iter().filter(|a| a.path.is_ident("cfg")).collect::<Vec<_>>();
let field_name = field.ident.as_ref().expect("expected field to have an identifier");
let parse = quote! {
#( #cfg_attrs )*
let #field_name = #ruma_serde::urlencoded::from_str(
&request.uri().query().unwrap_or(""),
)?;
};
(parse, quote! { #field_name, })
(
parse,
quote! {
#( #cfg_attrs )*
#field_name,
},
)
} else if self.has_query_fields() {
let (decls, names) = self.vars(RequestFieldKind::Query, quote!(request_query));
@ -99,6 +111,9 @@ impl Request {
_ => panic!("expected request field to be header variant"),
};
let cfg_attrs =
field.attrs.iter().filter(|a| a.path.is_ident("cfg")).collect::<Vec<_>>();
let field_name = &field.ident;
let header_name_string = header_name.to_string();
@ -121,6 +136,7 @@ impl Request {
};
let decl = quote! {
#( #cfg_attrs )*
let #field_name = match headers.get(#http::header::#header_name) {
Some(header_value) => {
let str_value = header_value.to_str()?;
@ -130,7 +146,13 @@ impl Request {
};
};
(decl, field_name)
(
decl,
quote! {
#( #cfg_attrs )*
#field_name
},
)
})
.unzip();
@ -263,7 +285,13 @@ impl Request {
let #field_name = #src.#field_name;
};
(decl, quote! { #field_name, })
(
decl,
quote! {
#( #cfg_attrs )*
#field_name,
},
)
})
.unzip()
}

View File

@ -62,6 +62,7 @@ impl Response {
..
}) if segments.last().unwrap().ident == "Option" => {
quote! {
#( #cfg_attrs )*
#field_name: {
headers.remove(#http::header::#header_name)
.map(|h| h.to_str().map(|s| s.to_owned()))
@ -70,6 +71,7 @@ impl Response {
}
}
_ => quote! {
#( #cfg_attrs )*
#field_name: {
headers.remove(#http::header::#header_name)
.expect("response missing expected header")
@ -82,13 +84,16 @@ impl Response {
}
ResponseField::NewtypeBody(_) => {
quote! {
#( #cfg_attrs )*
#field_name: response_body.0
}
}
// This field must be instantiated last to avoid `use of move value` error.
// We are guaranteed only one new body field because of a check in `try_from`.
// We are guaranteed only one new body field because of a check in
// `parse_response`.
ResponseField::NewtypeRawBody(_) => {
new_type_raw_body = Some(quote! {
#( #cfg_attrs )*
#field_name: {
::std::convert::AsRef::<[::std::primitive::u8]>::as_ref(
response.body(),