macros: Simplify internal derive macro input parsing

This commit is contained in:
Jonas Platte 2024-09-06 19:11:51 +02:00
parent 88f56b0e00
commit de6b08a2b5
3 changed files with 10 additions and 18 deletions

View File

@ -3,7 +3,7 @@ use quote::{quote, ToTokens};
use syn::{
parse::{Parse, ParseStream},
punctuated::Punctuated,
DeriveInput, Field, Generics, Ident, ItemStruct, Token, Type,
Field, Generics, Ident, ItemStruct, Token, Type,
};
use super::{
@ -44,13 +44,9 @@ impl Parse for RequestAttr {
}
}
pub fn expand_derive_request(input: DeriveInput) -> syn::Result<TokenStream> {
let fields = match input.data {
syn::Data::Struct(s) => s.fields,
_ => panic!("This derive macro only works on structs"),
};
let fields = fields.into_iter().map(RequestField::try_from).collect::<syn::Result<_>>()?;
pub fn expand_derive_request(input: ItemStruct) -> syn::Result<TokenStream> {
let fields =
input.fields.into_iter().map(RequestField::try_from).collect::<syn::Result<_>>()?;
let mut error_ty = None;

View File

@ -6,7 +6,7 @@ use syn::{
parse::{Parse, ParseStream},
punctuated::Punctuated,
visit::Visit,
DeriveInput, Field, Generics, Ident, ItemStruct, Lifetime, Token, Type,
Field, Generics, Ident, ItemStruct, Lifetime, Token, Type,
};
use super::{
@ -59,13 +59,9 @@ impl Parse for ResponseAttr {
}
}
pub fn expand_derive_response(input: DeriveInput) -> syn::Result<TokenStream> {
let fields = match input.data {
syn::Data::Struct(s) => s.fields,
_ => panic!("This derive macro only works on structs"),
};
let fields = fields.into_iter().map(ResponseField::try_from).collect::<syn::Result<_>>()?;
pub fn expand_derive_response(input: ItemStruct) -> syn::Result<TokenStream> {
let fields =
input.fields.into_iter().map(ResponseField::try_from).collect::<syn::Result<_>>()?;
let mut manual_body_serde = false;
let mut error_ty = None;
let mut status_ident = None;

View File

@ -397,14 +397,14 @@ pub fn response(attr: TokenStream, item: TokenStream) -> TokenStream {
/// Internal helper that the request macro delegates most of its work to.
#[proc_macro_derive(Request, attributes(ruma_api))]
pub fn derive_request(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let input = parse_macro_input!(input);
expand_derive_request(input).unwrap_or_else(syn::Error::into_compile_error).into()
}
/// Internal helper that the response macro delegates most of its work to.
#[proc_macro_derive(Response, attributes(ruma_api))]
pub fn derive_response(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let input = parse_macro_input!(input);
expand_derive_response(input).unwrap_or_else(syn::Error::into_compile_error).into()
}