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:
parent
aed0988694
commit
f6d419298c
@ -50,30 +50,8 @@ impl Api {
|
|||||||
let unstable_path = util::map_option_literal(&metadata.unstable_path);
|
let unstable_path = util::map_option_literal(&metadata.unstable_path);
|
||||||
let r0_path = util::map_option_literal(&metadata.r0_path);
|
let r0_path = util::map_option_literal(&metadata.r0_path);
|
||||||
let stable_path = util::map_option_literal(&metadata.stable_path);
|
let stable_path = util::map_option_literal(&metadata.stable_path);
|
||||||
let rate_limited: TokenStream = metadata
|
let rate_limited = &self.metadata.rate_limited;
|
||||||
.rate_limited
|
let authentication = &self.metadata.authentication;
|
||||||
.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 added = util::map_option_literal(&metadata.added);
|
let added = util::map_option_literal(&metadata.added);
|
||||||
let deprecated = util::map_option_literal(&metadata.deprecated);
|
let deprecated = util::map_option_literal(&metadata.deprecated);
|
||||||
let removed = util::map_option_literal(&metadata.removed);
|
let removed = util::map_option_literal(&metadata.removed);
|
||||||
@ -99,8 +77,8 @@ impl Api {
|
|||||||
added: #added,
|
added: #added,
|
||||||
deprecated: #deprecated,
|
deprecated: #deprecated,
|
||||||
removed: #removed,
|
removed: #removed,
|
||||||
#rate_limited
|
rate_limited: #rate_limited,
|
||||||
#authentication
|
authentication: #ruma_api::AuthScheme::#authentication,
|
||||||
};
|
};
|
||||||
|
|
||||||
#request
|
#request
|
||||||
|
@ -4,7 +4,7 @@ use quote::ToTokens;
|
|||||||
use syn::{
|
use syn::{
|
||||||
braced,
|
braced,
|
||||||
parse::{Parse, ParseStream},
|
parse::{Parse, ParseStream},
|
||||||
Attribute, Ident, LitBool, LitStr, Token,
|
Ident, LitBool, LitStr, Token,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{auth_scheme::AuthScheme, util, version::MatrixVersionLiteral};
|
use crate::{auth_scheme::AuthScheme, util, version::MatrixVersionLiteral};
|
||||||
@ -24,15 +24,6 @@ mod kw {
|
|||||||
syn::custom_keyword!(removed);
|
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.
|
/// The result of processing the `metadata` section of the macro.
|
||||||
pub struct Metadata {
|
pub struct Metadata {
|
||||||
/// The description field.
|
/// The description field.
|
||||||
@ -54,10 +45,10 @@ pub struct Metadata {
|
|||||||
pub stable_path: Option<EndpointPath>,
|
pub stable_path: Option<EndpointPath>,
|
||||||
|
|
||||||
/// The rate_limited field.
|
/// The rate_limited field.
|
||||||
pub rate_limited: Vec<MetadataField<LitBool>>,
|
pub rate_limited: LitBool,
|
||||||
|
|
||||||
/// The authentication field.
|
/// The authentication field.
|
||||||
pub authentication: Vec<MetadataField<AuthScheme>>,
|
pub authentication: AuthScheme,
|
||||||
|
|
||||||
/// The added field.
|
/// The added field.
|
||||||
pub added: Option<MatrixVersionLiteral>,
|
pub added: Option<MatrixVersionLiteral>,
|
||||||
@ -100,8 +91,8 @@ impl Parse for Metadata {
|
|||||||
let mut unstable_path = None;
|
let mut unstable_path = None;
|
||||||
let mut r0_path = None;
|
let mut r0_path = None;
|
||||||
let mut stable_path = None;
|
let mut stable_path = None;
|
||||||
let mut rate_limited = vec![];
|
let mut rate_limited = None;
|
||||||
let mut authentication = vec![];
|
let mut authentication = None;
|
||||||
let mut added = None;
|
let mut added = None;
|
||||||
let mut deprecated = None;
|
let mut deprecated = None;
|
||||||
let mut removed = None;
|
let mut removed = None;
|
||||||
@ -114,12 +105,8 @@ impl Parse for Metadata {
|
|||||||
FieldValue::UnstablePath(p) => set_field(&mut unstable_path, p)?,
|
FieldValue::UnstablePath(p) => set_field(&mut unstable_path, p)?,
|
||||||
FieldValue::R0Path(p) => set_field(&mut r0_path, p)?,
|
FieldValue::R0Path(p) => set_field(&mut r0_path, p)?,
|
||||||
FieldValue::StablePath(p) => set_field(&mut stable_path, p)?,
|
FieldValue::StablePath(p) => set_field(&mut stable_path, p)?,
|
||||||
FieldValue::RateLimited(value, attrs) => {
|
FieldValue::RateLimited(rl) => set_field(&mut rate_limited, rl)?,
|
||||||
rate_limited.push(MetadataField { attrs, value });
|
FieldValue::Authentication(a) => set_field(&mut authentication, a)?,
|
||||||
}
|
|
||||||
FieldValue::Authentication(value, attrs) => {
|
|
||||||
authentication.push(MetadataField { attrs, value });
|
|
||||||
}
|
|
||||||
FieldValue::Added(v) => set_field(&mut added, v)?,
|
FieldValue::Added(v) => set_field(&mut added, v)?,
|
||||||
FieldValue::Deprecated(v) => set_field(&mut deprecated, v)?,
|
FieldValue::Deprecated(v) => set_field(&mut deprecated, v)?,
|
||||||
FieldValue::Removed(v) => set_field(&mut removed, v)?,
|
FieldValue::Removed(v) => set_field(&mut removed, v)?,
|
||||||
@ -215,16 +202,8 @@ impl Parse for Metadata {
|
|||||||
unstable_path,
|
unstable_path,
|
||||||
r0_path,
|
r0_path,
|
||||||
stable_path,
|
stable_path,
|
||||||
rate_limited: if rate_limited.is_empty() {
|
rate_limited: rate_limited.ok_or_else(|| missing_field("rate_limited"))?,
|
||||||
return Err(missing_field("rate_limited"));
|
authentication: authentication.ok_or_else(|| missing_field("authentication"))?,
|
||||||
} else {
|
|
||||||
rate_limited
|
|
||||||
},
|
|
||||||
authentication: if authentication.is_empty() {
|
|
||||||
return Err(missing_field("authentication"));
|
|
||||||
} else {
|
|
||||||
authentication
|
|
||||||
},
|
|
||||||
added,
|
added,
|
||||||
deprecated,
|
deprecated,
|
||||||
removed,
|
removed,
|
||||||
@ -296,8 +275,8 @@ enum FieldValue {
|
|||||||
UnstablePath(EndpointPath),
|
UnstablePath(EndpointPath),
|
||||||
R0Path(EndpointPath),
|
R0Path(EndpointPath),
|
||||||
StablePath(EndpointPath),
|
StablePath(EndpointPath),
|
||||||
RateLimited(LitBool, Vec<Attribute>),
|
RateLimited(LitBool),
|
||||||
Authentication(AuthScheme, Vec<Attribute>),
|
Authentication(AuthScheme),
|
||||||
Added(MatrixVersionLiteral),
|
Added(MatrixVersionLiteral),
|
||||||
Deprecated(MatrixVersionLiteral),
|
Deprecated(MatrixVersionLiteral),
|
||||||
Removed(MatrixVersionLiteral),
|
Removed(MatrixVersionLiteral),
|
||||||
@ -305,15 +284,6 @@ enum FieldValue {
|
|||||||
|
|
||||||
impl Parse for FieldValue {
|
impl Parse for FieldValue {
|
||||||
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
|
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 field: Field = input.parse()?;
|
||||||
let _: Token![:] = input.parse()?;
|
let _: Token![:] = input.parse()?;
|
||||||
|
|
||||||
@ -324,8 +294,8 @@ impl Parse for FieldValue {
|
|||||||
Field::UnstablePath => Self::UnstablePath(input.parse()?),
|
Field::UnstablePath => Self::UnstablePath(input.parse()?),
|
||||||
Field::R0Path => Self::R0Path(input.parse()?),
|
Field::R0Path => Self::R0Path(input.parse()?),
|
||||||
Field::StablePath => Self::StablePath(input.parse()?),
|
Field::StablePath => Self::StablePath(input.parse()?),
|
||||||
Field::RateLimited => Self::RateLimited(input.parse()?, attrs),
|
Field::RateLimited => Self::RateLimited(input.parse()?),
|
||||||
Field::Authentication => Self::Authentication(input.parse()?, attrs),
|
Field::Authentication => Self::Authentication(input.parse()?),
|
||||||
Field::Added => Self::Added(input.parse()?),
|
Field::Added => Self::Added(input.parse()?),
|
||||||
Field::Deprecated => Self::Deprecated(input.parse()?),
|
Field::Deprecated => Self::Deprecated(input.parse()?),
|
||||||
Field::Removed => Self::Removed(input.parse()?),
|
Field::Removed => Self::Removed(input.parse()?),
|
||||||
|
@ -10,7 +10,7 @@ use syn::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use super::{kw, metadata::Metadata};
|
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.
|
/// The result of processing the `request` section of the macro.
|
||||||
pub(crate) struct Request {
|
pub(crate) struct Request {
|
||||||
@ -81,18 +81,10 @@ impl Request {
|
|||||||
let struct_attributes = &self.attributes;
|
let struct_attributes = &self.attributes;
|
||||||
|
|
||||||
let method = &metadata.method;
|
let method = &metadata.method;
|
||||||
|
let authentication = &metadata.authentication;
|
||||||
let unstable_attr = metadata.unstable_path.as_ref().map(|p| quote! { unstable = #p, });
|
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 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 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 request_ident = Ident::new("Request", self.request_kw.span());
|
||||||
let lifetimes = self.all_lifetimes();
|
let lifetimes = self.all_lifetimes();
|
||||||
@ -112,12 +104,12 @@ impl Request {
|
|||||||
#[incoming_derive(!Deserialize, #ruma_api_macros::_FakeDeriveRumaApi)]
|
#[incoming_derive(!Deserialize, #ruma_api_macros::_FakeDeriveRumaApi)]
|
||||||
#[ruma_api(
|
#[ruma_api(
|
||||||
method = #method,
|
method = #method,
|
||||||
|
authentication = #authentication,
|
||||||
#unstable_attr
|
#unstable_attr
|
||||||
#r0_attr
|
#r0_attr
|
||||||
#stable_attr
|
#stable_attr
|
||||||
error_ty = #error_ty,
|
error_ty = #error_ty,
|
||||||
)]
|
)]
|
||||||
#( #auth_attributes )*
|
|
||||||
#( #struct_attributes )*
|
#( #struct_attributes )*
|
||||||
pub struct #request_ident < #(#lifetimes),* > {
|
pub struct #request_ident < #(#lifetimes),* > {
|
||||||
#fields
|
#fields
|
||||||
|
@ -5,7 +5,7 @@ use std::collections::BTreeSet;
|
|||||||
use proc_macro2::{Ident, Span, TokenStream};
|
use proc_macro2::{Ident, Span, TokenStream};
|
||||||
use proc_macro_crate::{crate_name, FoundCrate};
|
use proc_macro_crate::{crate_name, FoundCrate};
|
||||||
use quote::{format_ident, quote, ToTokens};
|
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 {
|
pub fn import_ruma_api() -> TokenStream {
|
||||||
if let Ok(FoundCrate::Name(name)) = crate_name("ruma-api") {
|
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)
|
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> {
|
pub fn all_cfgs_expr(cfgs: &[Attribute]) -> Option<TokenStream> {
|
||||||
let sub_cfgs: Vec<_> = cfgs.iter().filter_map(extract_cfg).collect();
|
let sub_cfgs: Vec<_> = cfgs.iter().filter_map(extract_cfg).collect();
|
||||||
(!sub_cfgs.is_empty()).then(|| quote! { all( #(#sub_cfgs),* ) })
|
(!sub_cfgs.is_empty()).then(|| quote! { all( #(#sub_cfgs),* ) })
|
||||||
|
@ -12,15 +12,7 @@ pub mod some_endpoint {
|
|||||||
method: POST, // An `http::Method` constant. No imports required.
|
method: POST, // An `http::Method` constant. No imports required.
|
||||||
name: "some_endpoint",
|
name: "some_endpoint",
|
||||||
unstable_path: "/_matrix/some/endpoint/:user",
|
unstable_path: "/_matrix/some/endpoint/:user",
|
||||||
|
|
||||||
#[cfg(all())]
|
|
||||||
rate_limited: true,
|
|
||||||
#[cfg(any())]
|
|
||||||
rate_limited: false,
|
rate_limited: false,
|
||||||
|
|
||||||
#[cfg(all())]
|
|
||||||
authentication: AccessToken,
|
|
||||||
#[cfg(any())]
|
|
||||||
authentication: None,
|
authentication: None,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user