api-macros: Remove RawMetadata
This commit is contained in:
parent
187809cff5
commit
00ae067bce
@ -8,7 +8,7 @@ use syn::{
|
|||||||
braced,
|
braced,
|
||||||
parse::{Parse, ParseStream},
|
parse::{Parse, ParseStream},
|
||||||
spanned::Spanned,
|
spanned::Spanned,
|
||||||
Attribute, Field, FieldValue, Token, Type,
|
Attribute, Field, Token, Type,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub(crate) mod attribute;
|
pub(crate) mod attribute;
|
||||||
@ -48,7 +48,7 @@ impl TryFrom<RawApi> for Api {
|
|||||||
let import_path = util::import_ruma_api();
|
let import_path = util::import_ruma_api();
|
||||||
|
|
||||||
let res = Self {
|
let res = Self {
|
||||||
metadata: raw_api.metadata.try_into()?,
|
metadata: raw_api.metadata,
|
||||||
request: raw_api.request.try_into()?,
|
request: raw_api.request.try_into()?,
|
||||||
response: raw_api.response.try_into()?,
|
response: raw_api.response.try_into()?,
|
||||||
error: match raw_api.error {
|
error: match raw_api.error {
|
||||||
@ -399,7 +399,6 @@ impl ToTokens for Api {
|
|||||||
mod kw {
|
mod kw {
|
||||||
use syn::custom_keyword;
|
use syn::custom_keyword;
|
||||||
|
|
||||||
custom_keyword!(metadata);
|
|
||||||
custom_keyword!(request);
|
custom_keyword!(request);
|
||||||
custom_keyword!(response);
|
custom_keyword!(response);
|
||||||
custom_keyword!(error);
|
custom_keyword!(error);
|
||||||
@ -408,7 +407,7 @@ mod kw {
|
|||||||
/// The entire `ruma_api!` macro structure directly as it appears in the source code..
|
/// The entire `ruma_api!` macro structure directly as it appears in the source code..
|
||||||
pub struct RawApi {
|
pub struct RawApi {
|
||||||
/// The `metadata` section of the macro.
|
/// The `metadata` section of the macro.
|
||||||
pub metadata: RawMetadata,
|
pub metadata: Metadata,
|
||||||
|
|
||||||
/// The `request` section of the macro.
|
/// The `request` section of the macro.
|
||||||
pub request: RawRequest,
|
pub request: RawRequest,
|
||||||
@ -431,28 +430,6 @@ impl Parse for RawApi {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct RawMetadata {
|
|
||||||
pub metadata_kw: kw::metadata,
|
|
||||||
pub field_values: Vec<FieldValue>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Parse for RawMetadata {
|
|
||||||
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
|
|
||||||
let metadata_kw = input.parse::<kw::metadata>()?;
|
|
||||||
input.parse::<Token![:]>()?;
|
|
||||||
let field_values;
|
|
||||||
braced!(field_values in input);
|
|
||||||
|
|
||||||
Ok(Self {
|
|
||||||
metadata_kw,
|
|
||||||
field_values: field_values
|
|
||||||
.parse_terminated::<FieldValue, Token![,]>(FieldValue::parse)?
|
|
||||||
.into_iter()
|
|
||||||
.collect(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct RawRequest {
|
pub struct RawRequest {
|
||||||
pub attributes: Vec<Attribute>,
|
pub attributes: Vec<Attribute>,
|
||||||
pub request_kw: kw::request,
|
pub request_kw: kw::request,
|
||||||
|
@ -1,10 +1,16 @@
|
|||||||
//! Details of the `metadata` section of the procedural macro.
|
//! Details of the `metadata` section of the procedural macro.
|
||||||
|
|
||||||
use std::convert::TryFrom;
|
use syn::{
|
||||||
|
braced,
|
||||||
|
parse::{Parse, ParseStream},
|
||||||
|
Expr, ExprLit, ExprPath, FieldValue, Ident, Lit, LitBool, LitStr, Member, Token,
|
||||||
|
};
|
||||||
|
|
||||||
use syn::{Expr, ExprLit, ExprPath, Ident, Lit, LitBool, LitStr, Member};
|
use crate::util;
|
||||||
|
|
||||||
use crate::{api::RawMetadata, util};
|
mod kw {
|
||||||
|
syn::custom_keyword!(metadata);
|
||||||
|
}
|
||||||
|
|
||||||
/// 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 {
|
||||||
@ -27,10 +33,16 @@ pub struct Metadata {
|
|||||||
pub authentication: Ident,
|
pub authentication: Ident,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<RawMetadata> for Metadata {
|
impl Parse for Metadata {
|
||||||
type Error = syn::Error;
|
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
|
||||||
|
let metadata_kw = input.parse::<kw::metadata>()?;
|
||||||
|
input.parse::<Token![:]>()?;
|
||||||
|
let field_values;
|
||||||
|
braced!(field_values in input);
|
||||||
|
|
||||||
|
let field_values =
|
||||||
|
field_values.parse_terminated::<FieldValue, Token![,]>(FieldValue::parse)?;
|
||||||
|
|
||||||
fn try_from(raw: RawMetadata) -> syn::Result<Self> {
|
|
||||||
let mut description = None;
|
let mut description = None;
|
||||||
let mut method = None;
|
let mut method = None;
|
||||||
let mut name = None;
|
let mut name = None;
|
||||||
@ -38,7 +50,7 @@ impl TryFrom<RawMetadata> for Metadata {
|
|||||||
let mut rate_limited = None;
|
let mut rate_limited = None;
|
||||||
let mut authentication = None;
|
let mut authentication = None;
|
||||||
|
|
||||||
for field_value in raw.field_values {
|
for field_value in field_values {
|
||||||
let identifier = match field_value.member.clone() {
|
let identifier = match field_value.member.clone() {
|
||||||
Member::Named(identifier) => identifier,
|
Member::Named(identifier) => identifier,
|
||||||
_ => panic!("expected Member::Named"),
|
_ => panic!("expected Member::Named"),
|
||||||
@ -93,7 +105,6 @@ impl TryFrom<RawMetadata> for Metadata {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let metadata_kw = raw.metadata_kw;
|
|
||||||
let missing_field =
|
let missing_field =
|
||||||
|name| syn::Error::new_spanned(metadata_kw, format!("missing field `{}`", name));
|
|name| syn::Error::new_spanned(metadata_kw, format!("missing field `{}`", name));
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user