Partially revert previous changes

since they were preparing for something that's now not going to happen,
namely a use case for multiple meta items in one #[ruma_api(..)] attribute
This commit is contained in:
Jonas Platte 2019-11-16 00:40:24 +01:00
parent 1334b6c9d9
commit 4d8cc3624f
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
3 changed files with 66 additions and 96 deletions

View File

@ -1,10 +1,7 @@
//! Details of the `#[ruma_api(...)]` attributes. //! Details of the `#[ruma_api(...)]` attributes.
use std::vec;
use syn::{ use syn::{
parse::{Parse, ParseStream}, parse::{Parse, ParseStream},
punctuated::{Pair, Punctuated},
Ident, Token, Ident, Token,
}; };
@ -25,26 +22,7 @@ pub enum Meta {
NameValue(MetaNameValue), NameValue(MetaNameValue),
} }
impl Parse for Meta { impl Meta {
fn parse(input: ParseStream) -> syn::Result<Self> {
let ident = input.parse()?;
if input.peek(Token![=]) {
let _ = input.parse::<Token![=]>();
Ok(Meta::NameValue(MetaNameValue {
name: ident,
value: input.parse()?,
}))
} else {
Ok(Meta::Word(ident))
}
}
}
/// List of `Meta`s
pub struct MetaList(Vec<Meta>);
impl MetaList {
/// Check if the given attribute is a ruma_api attribute. If it is, parse it. /// Check if the given attribute is a ruma_api attribute. If it is, parse it.
/// ///
/// # Panics /// # Panics
@ -70,22 +48,18 @@ impl MetaList {
} }
} }
impl Parse for MetaList { impl Parse for Meta {
fn parse(input: ParseStream) -> syn::Result<Self> { fn parse(input: ParseStream) -> syn::Result<Self> {
Ok(MetaList( let ident = input.parse()?;
Punctuated::<Meta, Token![,]>::parse_terminated(input)?
.into_pairs() if input.peek(Token![=]) {
.map(Pair::into_value) let _ = input.parse::<Token![=]>();
.collect(), Ok(Meta::NameValue(MetaNameValue {
)) name: ident,
} value: input.parse()?,
} }))
} else {
impl IntoIterator for MetaList { Ok(Meta::Word(ident))
type Item = Meta; }
type IntoIter = vec::IntoIter<Meta>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
} }
} }

View File

@ -5,7 +5,7 @@ use quote::{quote, quote_spanned, ToTokens};
use syn::{spanned::Spanned, Field, Ident}; use syn::{spanned::Spanned, Field, Ident};
use crate::api::{ use crate::api::{
attribute::{Meta, MetaList, MetaNameValue}, attribute::{Meta, MetaNameValue},
strip_serde_attrs, strip_serde_attrs,
}; };
@ -133,39 +133,37 @@ impl From<Vec<Field>> for Request {
let mut header = None; let mut header = None;
field.attrs.retain(|attr| { field.attrs.retain(|attr| {
let meta_list = match MetaList::from_attribute(attr) { let meta = match Meta::from_attribute(attr) {
Some(list) => list, Some(m) => m,
None => return true, None => return true,
}; };
for meta in meta_list { match meta {
match meta { Meta::Word(ident) => {
Meta::Word(ident) => { assert!(
assert!( field_kind.is_none(),
field_kind.is_none(), "ruma_api! field kind can only be set once per field"
"ruma_api! field kind can only be set once per field" );
);
field_kind = Some(match &ident.to_string()[..] { field_kind = Some(match &ident.to_string()[..] {
"body" => RequestFieldKind::NewtypeBody, "body" => RequestFieldKind::NewtypeBody,
"path" => RequestFieldKind::Path, "path" => RequestFieldKind::Path,
"query" => RequestFieldKind::Query, "query" => RequestFieldKind::Query,
_ => panic!("ruma_api! single-word attribute on requests must be: body, path, or query"), _ => panic!("ruma_api! single-word attribute on requests must be: body, path, or query"),
}); });
} }
Meta::NameValue(MetaNameValue { name, value }) => { Meta::NameValue(MetaNameValue { name, value }) => {
assert!( assert!(
name == "header", name == "header",
"ruma_api! name/value pair attribute on requests must be: header" "ruma_api! name/value pair attribute on requests must be: header"
); );
assert!( assert!(
field_kind.is_none(), field_kind.is_none(),
"ruma_api! field kind can only be set once per field" "ruma_api! field kind can only be set once per field"
); );
header = Some(value); header = Some(value);
field_kind = Some(RequestFieldKind::Header); field_kind = Some(RequestFieldKind::Header);
}
} }
} }

View File

@ -5,7 +5,7 @@ use quote::{quote, quote_spanned, ToTokens};
use syn::{spanned::Spanned, Field, Ident}; use syn::{spanned::Spanned, Field, Ident};
use crate::api::{ use crate::api::{
attribute::{Meta, MetaList, MetaNameValue}, attribute::{Meta, MetaNameValue},
strip_serde_attrs, strip_serde_attrs,
}; };
@ -105,38 +105,36 @@ impl From<Vec<Field>> for Response {
let mut header = None; let mut header = None;
field.attrs.retain(|attr| { field.attrs.retain(|attr| {
let meta_list = match MetaList::from_attribute(attr) { let meta = match Meta::from_attribute(attr) {
Some(list) => list, Some(m) => m,
None => return true, None => return true,
}; };
for meta in meta_list { match meta {
match meta { Meta::Word(ident) => {
Meta::Word(ident) => { assert!(
assert!( ident == "body",
ident == "body", "ruma_api! single-word attribute on responses must be: body"
"ruma_api! single-word attribute on responses must be: body" );
); assert!(
assert!( field_kind.is_none(),
field_kind.is_none(), "ruma_api! field kind can only be set once per field"
"ruma_api! field kind can only be set once per field" );
);
field_kind = Some(ResponseFieldKind::NewtypeBody); field_kind = Some(ResponseFieldKind::NewtypeBody);
} }
Meta::NameValue(MetaNameValue { name, value }) => { Meta::NameValue(MetaNameValue { name, value }) => {
assert!( assert!(
name == "header", name == "header",
"ruma_api! name/value pair attribute on requests must be: header" "ruma_api! name/value pair attribute on requests must be: header"
); );
assert!( assert!(
field_kind.is_none(), field_kind.is_none(),
"ruma_api! field kind can only be set once per field" "ruma_api! field kind can only be set once per field"
); );
header = Some(value); header = Some(value);
field_kind = Some(ResponseFieldKind::Header); field_kind = Some(ResponseFieldKind::Header);
}
} }
} }