From 4e72c374b93d7e4556b52a9a6141bc47133804d3 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Thu, 12 Aug 2021 11:59:00 +0200 Subject: [PATCH] api-macros: Small refactor --- crates/ruma-api-macros/src/request.rs | 20 ++++++------------- crates/ruma-api-macros/src/response.rs | 8 ++++++++ .../ruma-api-macros/src/response/outgoing.rs | 18 +++++++---------- 3 files changed, 21 insertions(+), 25 deletions(-) diff --git a/crates/ruma-api-macros/src/request.rs b/crates/ruma-api-macros/src/request.rs index 99425cc5..d9599e15 100644 --- a/crates/ruma-api-macros/src/request.rs +++ b/crates/ruma-api-macros/src/request.rs @@ -187,12 +187,8 @@ impl Request { // for lifetimes, the outer check and the macro failing if it encounters // an illegal combination of field attributes, is enough to guarantee // `body_lifetimes` correctness. - let (derive_deserialize, generics) = if self.lifetimes.body.is_empty() { - (quote! { #serde::Deserialize }, TokenStream::new()) - } else { - let lifetimes = &self.lifetimes.body; - (TokenStream::new(), quote! { < #(#lifetimes),* > }) - }; + let lifetimes = &self.lifetimes.body; + let derive_deserialize = lifetimes.is_empty().then(|| quote! { #serde::Deserialize }); quote! { /// Data in the request body. @@ -203,7 +199,7 @@ impl Request { #serde::Serialize, #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 (derive_deserialize, generics) = if self.lifetimes.query.is_empty() { - (quote! { #serde::Deserialize }, TokenStream::new()) - } else { - let lifetimes = &self.lifetimes.query; - (TokenStream::new(), quote! { < #(#lifetimes),* > }) - }; + let lifetimes = &self.lifetimes.query; + let derive_deserialize = lifetimes.is_empty().then(|| quote! { #serde::Deserialize }); quote! { /// Data in the request's query string. @@ -234,7 +226,7 @@ impl Request { #serde::Serialize, #derive_deserialize )] - struct RequestQuery #generics #def + struct RequestQuery< #(#lifetimes),* > #def } }); diff --git a/crates/ruma-api-macros/src/response.rs b/crates/ruma-api-macros/src/response.rs index 9542bcb5..cb40886e 100644 --- a/crates/ruma-api-macros/src/response.rs +++ b/crates/ruma-api-macros/src/response.rs @@ -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. fn as_newtype_body_field(&self) -> Option<&Field> { match self { diff --git a/crates/ruma-api-macros/src/response/outgoing.rs b/crates/ruma-api-macros/src/response/outgoing.rs index f3adc6c5..8a526dc6 100644 --- a/crates/ruma-api-macros/src/response/outgoing.rs +++ b/crates/ruma-api-macros/src/response/outgoing.rs @@ -1,7 +1,7 @@ use proc_macro2::TokenStream; use quote::quote; -use super::{Response, ResponseField}; +use super::Response; impl Response { pub fn expand_outgoing(&self, ruma_api: &TokenStream) -> TokenStream { @@ -9,8 +9,8 @@ impl Response { let http = quote! { #ruma_api::exports::http }; let ruma_serde = quote! { #ruma_api::exports::ruma_serde }; - let serialize_response_headers = self.fields.iter().map(|response_field| { - if let ResponseField::Header(field, header_name) = response_field { + let serialize_response_headers = self.fields.iter().filter_map(|response_field| { + response_field.as_header_field().map(|(field, header_name)| { let field_name = 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() { @@ -48,8 +46,8 @@ impl Response { #ruma_serde::json_to_buf(&self.#field_name)? } } else { - let fields = self.fields.iter().map(|response_field| { - if let ResponseField::Body(field) = response_field { + let fields = self.fields.iter().filter_map(|response_field| { + response_field.as_body_field().map(|field| { let field_name = field.ident.as_ref().expect("expected field to have an identifier"); let cfg_attrs = field.attrs.iter().filter(|a| a.path.is_ident("cfg")); @@ -58,9 +56,7 @@ impl Response { #( #cfg_attrs )* #field_name: self.#field_name, } - } else { - TokenStream::new() - } + }) }); quote! {