api-macros: Small refactor
This commit is contained in:
parent
e396092ac0
commit
4e72c374b9
@ -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
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -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! {
|
||||
|
Loading…
x
Reference in New Issue
Block a user