api-macros: Remove RawMetadata

This commit is contained in:
Jonas Platte 2020-11-27 20:53:41 +01:00
parent 187809cff5
commit 00ae067bce
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
2 changed files with 22 additions and 34 deletions

View File

@ -8,7 +8,7 @@ use syn::{
braced,
parse::{Parse, ParseStream},
spanned::Spanned,
Attribute, Field, FieldValue, Token, Type,
Attribute, Field, Token, Type,
};
pub(crate) mod attribute;
@ -48,7 +48,7 @@ impl TryFrom<RawApi> for Api {
let import_path = util::import_ruma_api();
let res = Self {
metadata: raw_api.metadata.try_into()?,
metadata: raw_api.metadata,
request: raw_api.request.try_into()?,
response: raw_api.response.try_into()?,
error: match raw_api.error {
@ -399,7 +399,6 @@ impl ToTokens for Api {
mod kw {
use syn::custom_keyword;
custom_keyword!(metadata);
custom_keyword!(request);
custom_keyword!(response);
custom_keyword!(error);
@ -408,7 +407,7 @@ mod kw {
/// The entire `ruma_api!` macro structure directly as it appears in the source code..
pub struct RawApi {
/// The `metadata` section of the macro.
pub metadata: RawMetadata,
pub metadata: Metadata,
/// The `request` section of the macro.
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 attributes: Vec<Attribute>,
pub request_kw: kw::request,

View File

@ -1,10 +1,16 @@
//! 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.
pub struct Metadata {
@ -27,10 +33,16 @@ pub struct Metadata {
pub authentication: Ident,
}
impl TryFrom<RawMetadata> for Metadata {
type Error = syn::Error;
impl Parse for Metadata {
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 method = None;
let mut name = None;
@ -38,7 +50,7 @@ impl TryFrom<RawMetadata> for Metadata {
let mut rate_limited = 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() {
Member::Named(identifier) => identifier,
_ => panic!("expected Member::Named"),
@ -93,7 +105,6 @@ impl TryFrom<RawMetadata> for Metadata {
}
}
let metadata_kw = raw.metadata_kw;
let missing_field =
|name| syn::Error::new_spanned(metadata_kw, format!("missing field `{}`", name));