api-macros: Remove RawResponse
This commit is contained in:
parent
05249c6cdb
commit
4d9fdeb61d
@ -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<RawApi> 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<RawErrorType>,
|
||||
@ -429,41 +426,6 @@ impl Parse for RawApi {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct RawResponse {
|
||||
pub attributes: Vec<Attribute>,
|
||||
pub response_kw: kw::response,
|
||||
pub fields: Vec<Field>,
|
||||
}
|
||||
|
||||
impl Parse for RawResponse {
|
||||
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
|
||||
let attributes = input.call(Attribute::parse_outer)?;
|
||||
let response_kw = input.parse::<kw::response>()?;
|
||||
input.parse::<Token![:]>()?;
|
||||
let fields;
|
||||
braced!(fields in input);
|
||||
|
||||
Ok(Self {
|
||||
attributes,
|
||||
response_kw,
|
||||
fields: fields
|
||||
.parse_terminated::<Field, Token![,]>(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::<Result<Vec<_>, _>>()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct RawErrorType {
|
||||
pub error_kw: kw::error,
|
||||
pub ty: Type,
|
||||
|
@ -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<RawResponse> for Response {
|
||||
type Error = syn::Error;
|
||||
impl Parse for Response {
|
||||
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
|
||||
let attributes = input.call(Attribute::parse_outer)?;
|
||||
let response_kw = input.parse::<kw::response>()?;
|
||||
input.parse::<Token![:]>()?;
|
||||
let fields;
|
||||
braced!(fields in input);
|
||||
|
||||
let fields = fields
|
||||
.parse_terminated::<Field, Token![,]>(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::<Result<Vec<_>, _>>()?;
|
||||
|
||||
fn try_from(raw: RawResponse) -> syn::Result<Self> {
|
||||
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<RawResponse> 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() })
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user