diff --git a/ruma-api-macros/src/api/attribute.rs b/ruma-api-macros/src/api/attribute.rs index add646a8..df610dd9 100644 --- a/ruma-api-macros/src/api/attribute.rs +++ b/ruma-api-macros/src/api/attribute.rs @@ -1,10 +1,7 @@ //! Details of the `#[ruma_api(...)]` attributes. -use std::vec; - use syn::{ parse::{Parse, ParseStream}, - punctuated::{Pair, Punctuated}, Ident, Token, }; @@ -25,26 +22,7 @@ pub enum Meta { NameValue(MetaNameValue), } -impl Parse for Meta { - fn parse(input: ParseStream) -> syn::Result { - let ident = input.parse()?; - - if input.peek(Token![=]) { - let _ = input.parse::(); - Ok(Meta::NameValue(MetaNameValue { - name: ident, - value: input.parse()?, - })) - } else { - Ok(Meta::Word(ident)) - } - } -} - -/// List of `Meta`s -pub struct MetaList(Vec); - -impl MetaList { +impl Meta { /// Check if the given attribute is a ruma_api attribute. If it is, parse it. /// /// # Panics @@ -70,22 +48,18 @@ impl MetaList { } } -impl Parse for MetaList { +impl Parse for Meta { fn parse(input: ParseStream) -> syn::Result { - Ok(MetaList( - Punctuated::::parse_terminated(input)? - .into_pairs() - .map(Pair::into_value) - .collect(), - )) - } -} - -impl IntoIterator for MetaList { - type Item = Meta; - type IntoIter = vec::IntoIter; - - fn into_iter(self) -> Self::IntoIter { - self.0.into_iter() + let ident = input.parse()?; + + if input.peek(Token![=]) { + let _ = input.parse::(); + Ok(Meta::NameValue(MetaNameValue { + name: ident, + value: input.parse()?, + })) + } else { + Ok(Meta::Word(ident)) + } } } diff --git a/ruma-api-macros/src/api/request.rs b/ruma-api-macros/src/api/request.rs index cfd9bdde..e4862249 100644 --- a/ruma-api-macros/src/api/request.rs +++ b/ruma-api-macros/src/api/request.rs @@ -5,7 +5,7 @@ use quote::{quote, quote_spanned, ToTokens}; use syn::{spanned::Spanned, Field, Ident}; use crate::api::{ - attribute::{Meta, MetaList, MetaNameValue}, + attribute::{Meta, MetaNameValue}, strip_serde_attrs, }; @@ -133,39 +133,37 @@ impl From> for Request { let mut header = None; field.attrs.retain(|attr| { - let meta_list = match MetaList::from_attribute(attr) { - Some(list) => list, + let meta = match Meta::from_attribute(attr) { + Some(m) => m, None => return true, }; - for meta in meta_list { - match meta { - Meta::Word(ident) => { - assert!( - field_kind.is_none(), - "ruma_api! field kind can only be set once per field" - ); + match meta { + Meta::Word(ident) => { + assert!( + field_kind.is_none(), + "ruma_api! field kind can only be set once per field" + ); - field_kind = Some(match &ident.to_string()[..] { - "body" => RequestFieldKind::NewtypeBody, - "path" => RequestFieldKind::Path, - "query" => RequestFieldKind::Query, - _ => panic!("ruma_api! single-word attribute on requests must be: body, path, or query"), - }); - } - Meta::NameValue(MetaNameValue { name, value }) => { - assert!( - name == "header", - "ruma_api! name/value pair attribute on requests must be: header" - ); - assert!( - field_kind.is_none(), - "ruma_api! field kind can only be set once per field" - ); + field_kind = Some(match &ident.to_string()[..] { + "body" => RequestFieldKind::NewtypeBody, + "path" => RequestFieldKind::Path, + "query" => RequestFieldKind::Query, + _ => panic!("ruma_api! single-word attribute on requests must be: body, path, or query"), + }); + } + Meta::NameValue(MetaNameValue { name, value }) => { + assert!( + name == "header", + "ruma_api! name/value pair attribute on requests must be: header" + ); + assert!( + field_kind.is_none(), + "ruma_api! field kind can only be set once per field" + ); - header = Some(value); - field_kind = Some(RequestFieldKind::Header); - } + header = Some(value); + field_kind = Some(RequestFieldKind::Header); } } diff --git a/ruma-api-macros/src/api/response.rs b/ruma-api-macros/src/api/response.rs index 3daf613b..a5e3e632 100644 --- a/ruma-api-macros/src/api/response.rs +++ b/ruma-api-macros/src/api/response.rs @@ -5,7 +5,7 @@ use quote::{quote, quote_spanned, ToTokens}; use syn::{spanned::Spanned, Field, Ident}; use crate::api::{ - attribute::{Meta, MetaList, MetaNameValue}, + attribute::{Meta, MetaNameValue}, strip_serde_attrs, }; @@ -105,38 +105,36 @@ impl From> for Response { let mut header = None; field.attrs.retain(|attr| { - let meta_list = match MetaList::from_attribute(attr) { - Some(list) => list, + let meta = match Meta::from_attribute(attr) { + Some(m) => m, None => return true, }; - for meta in meta_list { - match meta { - Meta::Word(ident) => { - assert!( - ident == "body", - "ruma_api! single-word attribute on responses must be: body" - ); - assert!( - field_kind.is_none(), - "ruma_api! field kind can only be set once per field" - ); + match meta { + Meta::Word(ident) => { + assert!( + ident == "body", + "ruma_api! single-word attribute on responses must be: body" + ); + assert!( + field_kind.is_none(), + "ruma_api! field kind can only be set once per field" + ); - field_kind = Some(ResponseFieldKind::NewtypeBody); - } - Meta::NameValue(MetaNameValue { name, value }) => { - assert!( - name == "header", - "ruma_api! name/value pair attribute on requests must be: header" - ); - assert!( - field_kind.is_none(), - "ruma_api! field kind can only be set once per field" - ); + field_kind = Some(ResponseFieldKind::NewtypeBody); + } + Meta::NameValue(MetaNameValue { name, value }) => { + assert!( + name == "header", + "ruma_api! name/value pair attribute on requests must be: header" + ); + assert!( + field_kind.is_none(), + "ruma_api! field kind can only be set once per field" + ); - header = Some(value); - field_kind = Some(ResponseFieldKind::Header); - } + header = Some(value); + field_kind = Some(ResponseFieldKind::Header); } }