From 00ae067bcef689febb839dbe3832a1b922deafcc Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 27 Nov 2020 20:53:41 +0100 Subject: [PATCH] api-macros: Remove RawMetadata --- ruma-api-macros/src/api.rs | 29 +++-------------------------- ruma-api-macros/src/api/metadata.rs | 27 +++++++++++++++++++-------- 2 files changed, 22 insertions(+), 34 deletions(-) diff --git a/ruma-api-macros/src/api.rs b/ruma-api-macros/src/api.rs index 17fdd03c..98aae9e2 100644 --- a/ruma-api-macros/src/api.rs +++ b/ruma-api-macros/src/api.rs @@ -8,7 +8,7 @@ use syn::{ braced, parse::{Parse, ParseStream}, spanned::Spanned, - Attribute, Field, FieldValue, Token, Type, + Attribute, Field, Token, Type, }; pub(crate) mod attribute; @@ -48,7 +48,7 @@ impl TryFrom for Api { let import_path = util::import_ruma_api(); let res = Self { - metadata: raw_api.metadata.try_into()?, + metadata: raw_api.metadata, request: raw_api.request.try_into()?, response: raw_api.response.try_into()?, error: match raw_api.error { @@ -399,7 +399,6 @@ impl ToTokens for Api { mod kw { use syn::custom_keyword; - custom_keyword!(metadata); custom_keyword!(request); custom_keyword!(response); custom_keyword!(error); @@ -408,7 +407,7 @@ mod kw { /// The entire `ruma_api!` macro structure directly as it appears in the source code.. pub struct RawApi { /// The `metadata` section of the macro. - pub metadata: RawMetadata, + pub metadata: Metadata, /// The `request` section of the macro. pub request: RawRequest, @@ -431,28 +430,6 @@ impl Parse for RawApi { } } -pub struct RawMetadata { - pub metadata_kw: kw::metadata, - pub field_values: Vec, -} - -impl Parse for RawMetadata { - fn parse(input: ParseStream<'_>) -> syn::Result { - let metadata_kw = input.parse::()?; - input.parse::()?; - let field_values; - braced!(field_values in input); - - Ok(Self { - metadata_kw, - field_values: field_values - .parse_terminated::(FieldValue::parse)? - .into_iter() - .collect(), - }) - } -} - pub struct RawRequest { pub attributes: Vec, pub request_kw: kw::request, diff --git a/ruma-api-macros/src/api/metadata.rs b/ruma-api-macros/src/api/metadata.rs index a95e60ed..e998651e 100644 --- a/ruma-api-macros/src/api/metadata.rs +++ b/ruma-api-macros/src/api/metadata.rs @@ -1,10 +1,16 @@ //! Details of the `metadata` section of the procedural macro. -use std::convert::TryFrom; +use syn::{ + braced, + parse::{Parse, ParseStream}, + Expr, ExprLit, ExprPath, FieldValue, Ident, Lit, LitBool, LitStr, Member, Token, +}; -use syn::{Expr, ExprLit, ExprPath, Ident, Lit, LitBool, LitStr, Member}; +use crate::util; -use crate::{api::RawMetadata, util}; +mod kw { + syn::custom_keyword!(metadata); +} /// The result of processing the `metadata` section of the macro. pub struct Metadata { @@ -27,10 +33,16 @@ pub struct Metadata { pub authentication: Ident, } -impl TryFrom for Metadata { - type Error = syn::Error; +impl Parse for Metadata { + fn parse(input: ParseStream<'_>) -> syn::Result { + let metadata_kw = input.parse::()?; + input.parse::()?; + let field_values; + braced!(field_values in input); + + let field_values = + field_values.parse_terminated::(FieldValue::parse)?; - fn try_from(raw: RawMetadata) -> syn::Result { let mut description = None; let mut method = None; let mut name = None; @@ -38,7 +50,7 @@ impl TryFrom for Metadata { let mut rate_limited = None; let mut authentication = None; - for field_value in raw.field_values { + for field_value in field_values { let identifier = match field_value.member.clone() { Member::Named(identifier) => identifier, _ => panic!("expected Member::Named"), @@ -93,7 +105,6 @@ impl TryFrom for Metadata { } } - let metadata_kw = raw.metadata_kw; let missing_field = |name| syn::Error::new_spanned(metadata_kw, format!("missing field `{}`", name));