Revert "api: Allow cfg attributes on rate_limited and authentication metadata fields"

cfg-dependent metadata is no longer needed.
This reverts commit 1c0dab5a479737bc2ae83ff710321c191dd502f7.
This commit is contained in:
Jonas Platte 2022-02-13 12:13:50 +01:00
parent aed0988694
commit f6d419298c
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
5 changed files with 21 additions and 93 deletions

View File

@ -50,30 +50,8 @@ impl Api {
let unstable_path = util::map_option_literal(&metadata.unstable_path);
let r0_path = util::map_option_literal(&metadata.r0_path);
let stable_path = util::map_option_literal(&metadata.stable_path);
let rate_limited: TokenStream = metadata
.rate_limited
.iter()
.map(|r| {
let attrs = &r.attrs;
let value = &r.value;
quote! {
#( #attrs )*
rate_limited: #value,
}
})
.collect();
let authentication: TokenStream = metadata
.authentication
.iter()
.map(|r| {
let attrs = &r.attrs;
let value = &r.value;
quote! {
#( #attrs )*
authentication: #ruma_api::AuthScheme::#value,
}
})
.collect();
let rate_limited = &self.metadata.rate_limited;
let authentication = &self.metadata.authentication;
let added = util::map_option_literal(&metadata.added);
let deprecated = util::map_option_literal(&metadata.deprecated);
let removed = util::map_option_literal(&metadata.removed);
@ -99,8 +77,8 @@ impl Api {
added: #added,
deprecated: #deprecated,
removed: #removed,
#rate_limited
#authentication
rate_limited: #rate_limited,
authentication: #ruma_api::AuthScheme::#authentication,
};
#request

View File

@ -4,7 +4,7 @@ use quote::ToTokens;
use syn::{
braced,
parse::{Parse, ParseStream},
Attribute, Ident, LitBool, LitStr, Token,
Ident, LitBool, LitStr, Token,
};
use crate::{auth_scheme::AuthScheme, util, version::MatrixVersionLiteral};
@ -24,15 +24,6 @@ mod kw {
syn::custom_keyword!(removed);
}
/// A field of Metadata that contains attribute macros
pub struct MetadataField<T> {
/// attributes over the field
pub attrs: Vec<Attribute>,
/// the field itself
pub value: T,
}
/// The result of processing the `metadata` section of the macro.
pub struct Metadata {
/// The description field.
@ -54,10 +45,10 @@ pub struct Metadata {
pub stable_path: Option<EndpointPath>,
/// The rate_limited field.
pub rate_limited: Vec<MetadataField<LitBool>>,
pub rate_limited: LitBool,
/// The authentication field.
pub authentication: Vec<MetadataField<AuthScheme>>,
pub authentication: AuthScheme,
/// The added field.
pub added: Option<MatrixVersionLiteral>,
@ -100,8 +91,8 @@ impl Parse for Metadata {
let mut unstable_path = None;
let mut r0_path = None;
let mut stable_path = None;
let mut rate_limited = vec![];
let mut authentication = vec![];
let mut rate_limited = None;
let mut authentication = None;
let mut added = None;
let mut deprecated = None;
let mut removed = None;
@ -114,12 +105,8 @@ impl Parse for Metadata {
FieldValue::UnstablePath(p) => set_field(&mut unstable_path, p)?,
FieldValue::R0Path(p) => set_field(&mut r0_path, p)?,
FieldValue::StablePath(p) => set_field(&mut stable_path, p)?,
FieldValue::RateLimited(value, attrs) => {
rate_limited.push(MetadataField { attrs, value });
}
FieldValue::Authentication(value, attrs) => {
authentication.push(MetadataField { attrs, value });
}
FieldValue::RateLimited(rl) => set_field(&mut rate_limited, rl)?,
FieldValue::Authentication(a) => set_field(&mut authentication, a)?,
FieldValue::Added(v) => set_field(&mut added, v)?,
FieldValue::Deprecated(v) => set_field(&mut deprecated, v)?,
FieldValue::Removed(v) => set_field(&mut removed, v)?,
@ -215,16 +202,8 @@ impl Parse for Metadata {
unstable_path,
r0_path,
stable_path,
rate_limited: if rate_limited.is_empty() {
return Err(missing_field("rate_limited"));
} else {
rate_limited
},
authentication: if authentication.is_empty() {
return Err(missing_field("authentication"));
} else {
authentication
},
rate_limited: rate_limited.ok_or_else(|| missing_field("rate_limited"))?,
authentication: authentication.ok_or_else(|| missing_field("authentication"))?,
added,
deprecated,
removed,
@ -296,8 +275,8 @@ enum FieldValue {
UnstablePath(EndpointPath),
R0Path(EndpointPath),
StablePath(EndpointPath),
RateLimited(LitBool, Vec<Attribute>),
Authentication(AuthScheme, Vec<Attribute>),
RateLimited(LitBool),
Authentication(AuthScheme),
Added(MatrixVersionLiteral),
Deprecated(MatrixVersionLiteral),
Removed(MatrixVersionLiteral),
@ -305,15 +284,6 @@ enum FieldValue {
impl Parse for FieldValue {
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
let attrs: Vec<Attribute> = input.call(Attribute::parse_outer)?;
for attr in attrs.iter() {
if !util::is_cfg_attribute(attr) {
return Err(syn::Error::new_spanned(
&attr,
"only `cfg` attributes may appear here",
));
}
}
let field: Field = input.parse()?;
let _: Token![:] = input.parse()?;
@ -324,8 +294,8 @@ impl Parse for FieldValue {
Field::UnstablePath => Self::UnstablePath(input.parse()?),
Field::R0Path => Self::R0Path(input.parse()?),
Field::StablePath => Self::StablePath(input.parse()?),
Field::RateLimited => Self::RateLimited(input.parse()?, attrs),
Field::Authentication => Self::Authentication(input.parse()?, attrs),
Field::RateLimited => Self::RateLimited(input.parse()?),
Field::Authentication => Self::Authentication(input.parse()?),
Field::Added => Self::Added(input.parse()?),
Field::Deprecated => Self::Deprecated(input.parse()?),
Field::Removed => Self::Removed(input.parse()?),

View File

@ -10,7 +10,7 @@ use syn::{
};
use super::{kw, metadata::Metadata};
use crate::util::{all_cfgs, all_cfgs_expr, extract_cfg};
use crate::util::{all_cfgs, extract_cfg};
/// The result of processing the `request` section of the macro.
pub(crate) struct Request {
@ -81,18 +81,10 @@ impl Request {
let struct_attributes = &self.attributes;
let method = &metadata.method;
let authentication = &metadata.authentication;
let unstable_attr = metadata.unstable_path.as_ref().map(|p| quote! { unstable = #p, });
let r0_attr = metadata.r0_path.as_ref().map(|p| quote! { r0 = #p, });
let stable_attr = metadata.stable_path.as_ref().map(|p| quote! { stable = #p, });
let auth_attributes = metadata.authentication.iter().map(|field| {
let cfg_expr = all_cfgs_expr(&field.attrs);
let value = &field.value;
match cfg_expr {
Some(expr) => quote! { #[cfg_attr(#expr, ruma_api(authentication = #value))] },
None => quote! { #[ruma_api(authentication = #value)] },
}
});
let request_ident = Ident::new("Request", self.request_kw.span());
let lifetimes = self.all_lifetimes();
@ -112,12 +104,12 @@ impl Request {
#[incoming_derive(!Deserialize, #ruma_api_macros::_FakeDeriveRumaApi)]
#[ruma_api(
method = #method,
authentication = #authentication,
#unstable_attr
#r0_attr
#stable_attr
error_ty = #error_ty,
)]
#( #auth_attributes )*
#( #struct_attributes )*
pub struct #request_ident < #(#lifetimes),* > {
#fields

View File

@ -5,7 +5,7 @@ use std::collections::BTreeSet;
use proc_macro2::{Ident, Span, TokenStream};
use proc_macro_crate::{crate_name, FoundCrate};
use quote::{format_ident, quote, ToTokens};
use syn::{parse_quote, visit::Visit, AttrStyle, Attribute, Lifetime, NestedMeta, Type};
use syn::{parse_quote, visit::Visit, Attribute, Lifetime, NestedMeta, Type};
pub fn import_ruma_api() -> TokenStream {
if let Ok(FoundCrate::Name(name)) = crate_name("ruma-api") {
@ -47,10 +47,6 @@ pub fn collect_lifetime_idents(lifetimes: &mut BTreeSet<Lifetime>, ty: &Type) {
Visitor(lifetimes).visit_type(ty)
}
pub fn is_cfg_attribute(attr: &Attribute) -> bool {
matches!(attr.style, AttrStyle::Outer) && attr.path.is_ident("cfg")
}
pub fn all_cfgs_expr(cfgs: &[Attribute]) -> Option<TokenStream> {
let sub_cfgs: Vec<_> = cfgs.iter().filter_map(extract_cfg).collect();
(!sub_cfgs.is_empty()).then(|| quote! { all( #(#sub_cfgs),* ) })

View File

@ -12,15 +12,7 @@ pub mod some_endpoint {
method: POST, // An `http::Method` constant. No imports required.
name: "some_endpoint",
unstable_path: "/_matrix/some/endpoint/:user",
#[cfg(all())]
rate_limited: true,
#[cfg(any())]
rate_limited: false,
#[cfg(all())]
authentication: AccessToken,
#[cfg(any())]
authentication: None,
}