api-macros: Small refactor

This commit is contained in:
Jonas Platte 2021-08-12 11:59:00 +02:00
parent e396092ac0
commit 4e72c374b9
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
3 changed files with 21 additions and 25 deletions

View File

@ -187,12 +187,8 @@ impl Request {
// for lifetimes, the outer check and the macro failing if it encounters // for lifetimes, the outer check and the macro failing if it encounters
// an illegal combination of field attributes, is enough to guarantee // an illegal combination of field attributes, is enough to guarantee
// `body_lifetimes` correctness. // `body_lifetimes` correctness.
let (derive_deserialize, generics) = if self.lifetimes.body.is_empty() { let lifetimes = &self.lifetimes.body;
(quote! { #serde::Deserialize }, TokenStream::new()) let derive_deserialize = lifetimes.is_empty().then(|| quote! { #serde::Deserialize });
} else {
let lifetimes = &self.lifetimes.body;
(TokenStream::new(), quote! { < #(#lifetimes),* > })
};
quote! { quote! {
/// Data in the request body. /// Data in the request body.
@ -203,7 +199,7 @@ impl Request {
#serde::Serialize, #serde::Serialize,
#derive_deserialize #derive_deserialize
)] )]
struct RequestBody #generics #def struct RequestBody< #(#lifetimes),* > #def
} }
}); });
@ -218,12 +214,8 @@ impl Request {
}; };
let request_query_struct = request_query_def.map(|def| { let request_query_struct = request_query_def.map(|def| {
let (derive_deserialize, generics) = if self.lifetimes.query.is_empty() { let lifetimes = &self.lifetimes.query;
(quote! { #serde::Deserialize }, TokenStream::new()) let derive_deserialize = lifetimes.is_empty().then(|| quote! { #serde::Deserialize });
} else {
let lifetimes = &self.lifetimes.query;
(TokenStream::new(), quote! { < #(#lifetimes),* > })
};
quote! { quote! {
/// Data in the request's query string. /// Data in the request's query string.
@ -234,7 +226,7 @@ impl Request {
#serde::Serialize, #serde::Serialize,
#derive_deserialize #derive_deserialize
)] )]
struct RequestQuery #generics #def struct RequestQuery< #(#lifetimes),* > #def
} }
}); });

View File

@ -194,6 +194,14 @@ impl ResponseField {
} }
} }
/// Return the contained field and HTTP header ident if this repsonse field is a header kind.
fn as_header_field(&self) -> Option<(&Field, &Ident)> {
match self {
ResponseField::Header(field, ident) => Some((field, ident)),
_ => None,
}
}
/// Return the contained field if this response field is a newtype body kind. /// Return the contained field if this response field is a newtype body kind.
fn as_newtype_body_field(&self) -> Option<&Field> { fn as_newtype_body_field(&self) -> Option<&Field> {
match self { match self {

View File

@ -1,7 +1,7 @@
use proc_macro2::TokenStream; use proc_macro2::TokenStream;
use quote::quote; use quote::quote;
use super::{Response, ResponseField}; use super::Response;
impl Response { impl Response {
pub fn expand_outgoing(&self, ruma_api: &TokenStream) -> TokenStream { pub fn expand_outgoing(&self, ruma_api: &TokenStream) -> TokenStream {
@ -9,8 +9,8 @@ impl Response {
let http = quote! { #ruma_api::exports::http }; let http = quote! { #ruma_api::exports::http };
let ruma_serde = quote! { #ruma_api::exports::ruma_serde }; let ruma_serde = quote! { #ruma_api::exports::ruma_serde };
let serialize_response_headers = self.fields.iter().map(|response_field| { let serialize_response_headers = self.fields.iter().filter_map(|response_field| {
if let ResponseField::Header(field, header_name) = response_field { response_field.as_header_field().map(|(field, header_name)| {
let field_name = let field_name =
field.ident.as_ref().expect("expected field to have an identifier"); field.ident.as_ref().expect("expected field to have an identifier");
@ -34,9 +34,7 @@ impl Response {
); );
}, },
} }
} else { })
TokenStream::new()
}
}); });
let body = if let Some(field) = self.newtype_raw_body_field() { let body = if let Some(field) = self.newtype_raw_body_field() {
@ -48,8 +46,8 @@ impl Response {
#ruma_serde::json_to_buf(&self.#field_name)? #ruma_serde::json_to_buf(&self.#field_name)?
} }
} else { } else {
let fields = self.fields.iter().map(|response_field| { let fields = self.fields.iter().filter_map(|response_field| {
if let ResponseField::Body(field) = response_field { response_field.as_body_field().map(|field| {
let field_name = let field_name =
field.ident.as_ref().expect("expected field to have an identifier"); field.ident.as_ref().expect("expected field to have an identifier");
let cfg_attrs = field.attrs.iter().filter(|a| a.path.is_ident("cfg")); let cfg_attrs = field.attrs.iter().filter(|a| a.path.is_ident("cfg"));
@ -58,9 +56,7 @@ impl Response {
#( #cfg_attrs )* #( #cfg_attrs )*
#field_name: self.#field_name, #field_name: self.#field_name,
} }
} else { })
TokenStream::new()
}
}); });
quote! { quote! {