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.
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<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 {
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<Self> {
Ok(MetaList(
Punctuated::<Meta, Token![,]>::parse_terminated(input)?
.into_pairs()
.map(Pair::into_value)
.collect(),
))
}
}
impl IntoIterator for MetaList {
type Item = Meta;
type IntoIter = vec::IntoIter<Meta>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
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))
}
}
}

View File

@ -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<Vec<Field>> 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);
}
}

View File

@ -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<Vec<Field>> 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);
}
}