From 4d9fdeb61d848518625fa9f2cb20b829f192f5d3 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 27 Nov 2020 21:01:07 +0100 Subject: [PATCH] api-macros: Remove RawResponse --- ruma-api-macros/src/api.rs | 46 +++------------------------- ruma-api-macros/src/api/response.rs | 47 +++++++++++++++++++++++------ 2 files changed, 41 insertions(+), 52 deletions(-) diff --git a/ruma-api-macros/src/api.rs b/ruma-api-macros/src/api.rs index d71039a2..2c759755 100644 --- a/ruma-api-macros/src/api.rs +++ b/ruma-api-macros/src/api.rs @@ -1,14 +1,12 @@ //! Details of the `ruma_api` procedural macro. -use std::convert::{TryFrom, TryInto as _}; +use std::convert::TryFrom; use proc_macro2::TokenStream; use quote::{quote, ToTokens}; use syn::{ - braced, parse::{Parse, ParseStream}, - spanned::Spanned, - Attribute, Field, Token, Type, + Field, Token, Type, }; pub(crate) mod attribute; @@ -50,7 +48,7 @@ impl TryFrom for Api { let res = Self { metadata: raw_api.metadata, request: raw_api.request, - response: raw_api.response.try_into()?, + response: raw_api.response, error: match raw_api.error { Some(raw_err) => raw_err.ty.to_token_stream(), None => quote! { #import_path::error::Void }, @@ -399,7 +397,6 @@ impl ToTokens for Api { mod kw { use syn::custom_keyword; - custom_keyword!(response); custom_keyword!(error); } @@ -412,7 +409,7 @@ pub struct RawApi { pub request: Request, /// The `response` section of the macro. - pub response: RawResponse, + pub response: Response, /// The `error` section of the macro. pub error: Option, @@ -429,41 +426,6 @@ impl Parse for RawApi { } } -pub struct RawResponse { - pub attributes: Vec, - pub response_kw: kw::response, - pub fields: Vec, -} - -impl Parse for RawResponse { - fn parse(input: ParseStream<'_>) -> syn::Result { - let attributes = input.call(Attribute::parse_outer)?; - let response_kw = input.parse::()?; - input.parse::()?; - let fields; - braced!(fields in input); - - Ok(Self { - attributes, - response_kw, - fields: fields - .parse_terminated::(Field::parse_named)? - .into_iter() - .map(|f| { - if util::has_lifetime(&f.ty) { - Err(syn::Error::new( - f.ident.span(), - "Lifetimes on Response fields cannot be supported until GAT are stable", - )) - } else { - Ok(f) - } - }) - .collect::, _>>()?, - }) - } -} - pub struct RawErrorType { pub error_kw: kw::error, pub ty: Type, diff --git a/ruma-api-macros/src/api/response.rs b/ruma-api-macros/src/api/response.rs index 4daa20eb..f2a389ff 100644 --- a/ruma-api-macros/src/api/response.rs +++ b/ruma-api-macros/src/api/response.rs @@ -1,19 +1,28 @@ //! Details of the `response` section of the procedural macro. -use std::{convert::TryFrom, mem}; +use std::mem; use proc_macro2::TokenStream; use quote::{quote, quote_spanned, ToTokens}; -use syn::{spanned::Spanned, Attribute, Field, Ident}; +use syn::{ + braced, + parse::{Parse, ParseStream}, + spanned::Spanned, + Attribute, Field, Ident, Token, +}; use crate::{ api::{ attribute::{Meta, MetaNameValue}, - strip_serde_attrs, RawResponse, + strip_serde_attrs, }, util, }; +mod kw { + syn::custom_keyword!(response); +} + /// The result of processing the `response` section of the macro. pub struct Response { /// The attributes that will be applied to the struct definition. @@ -202,14 +211,32 @@ impl Response { } } -impl TryFrom for Response { - type Error = syn::Error; +impl Parse for Response { + fn parse(input: ParseStream<'_>) -> syn::Result { + let attributes = input.call(Attribute::parse_outer)?; + let response_kw = input.parse::()?; + input.parse::()?; + let fields; + braced!(fields in input); + + let fields = fields + .parse_terminated::(Field::parse_named)? + .into_iter() + .map(|f| { + if util::has_lifetime(&f.ty) { + Err(syn::Error::new( + f.ident.span(), + "Lifetimes on Response fields cannot be supported until GAT are stable", + )) + } else { + Ok(f) + } + }) + .collect::, _>>()?; - fn try_from(raw: RawResponse) -> syn::Result { let mut newtype_body_field = None; - let fields = raw - .fields + let fields = fields .into_iter() .map(|mut field| { let mut field_kind = None; @@ -270,12 +297,12 @@ impl TryFrom for Response { if newtype_body_field.is_some() && fields.iter().any(|f| f.is_body()) { // TODO: highlight conflicting fields, return Err(syn::Error::new_spanned( - raw.response_kw, + response_kw, "Can't have both a newtype body field and regular body fields", )); } - Ok(Self { attributes: raw.attributes, fields, ruma_api_import: util::import_ruma_api() }) + Ok(Self { attributes, fields, ruma_api_import: util::import_ruma_api() }) } }