macros: Simplify Response derive attribute parsing

This commit is contained in:
Jonas Platte 2022-10-22 00:32:53 +02:00
parent dff84efb0c
commit 876e8e38aa
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
2 changed files with 9 additions and 28 deletions

View File

@ -12,7 +12,6 @@ mod kw {
syn::custom_keyword!(query);
syn::custom_keyword!(query_map);
syn::custom_keyword!(header);
syn::custom_keyword!(manual_body_serde);
}
pub enum RequestMeta {
@ -76,20 +75,3 @@ impl Parse for ResponseMeta {
}
}
}
#[allow(clippy::large_enum_variant)]
pub enum DeriveResponseMeta {
ManualBodySerde,
}
impl Parse for DeriveResponseMeta {
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
let lookahead = input.lookahead1();
if lookahead.peek(kw::manual_body_serde) {
let _: kw::manual_body_serde = input.parse()?;
Ok(Self::ManualBodySerde)
} else {
Err(lookahead.error())
}
}
}

View File

@ -4,17 +4,20 @@ use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use syn::{
parse::{Parse, ParseStream},
punctuated::Punctuated,
visit::Visit,
DeriveInput, Field, Generics, Ident, Lifetime, Token, Type,
DeriveInput, Field, Generics, Ident, Lifetime, Type,
};
use super::attribute::{DeriveResponseMeta, ResponseMeta};
use super::attribute::ResponseMeta;
use crate::util::import_ruma_common;
mod incoming;
mod outgoing;
mod kw {
syn::custom_keyword!(manual_body_serde);
}
pub fn expand_derive_response(input: DeriveInput) -> syn::Result<TokenStream> {
let fields = match input.data {
syn::Data::Struct(s) => s.fields,
@ -28,13 +31,9 @@ pub fn expand_derive_response(input: DeriveInput) -> syn::Result<TokenStream> {
continue;
}
let metas =
attr.parse_args_with(Punctuated::<DeriveResponseMeta, Token![,]>::parse_terminated)?;
for meta in metas {
match meta {
DeriveResponseMeta::ManualBodySerde => manual_body_serde = true,
}
}
let _ = attr.parse_args::<kw::manual_body_serde>()?;
manual_body_serde = true;
}
let response =