api-macros: Remove RawResponse
This commit is contained in:
parent
05249c6cdb
commit
4d9fdeb61d
@ -1,14 +1,12 @@
|
|||||||
//! Details of the `ruma_api` procedural macro.
|
//! Details of the `ruma_api` procedural macro.
|
||||||
|
|
||||||
use std::convert::{TryFrom, TryInto as _};
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
use proc_macro2::TokenStream;
|
use proc_macro2::TokenStream;
|
||||||
use quote::{quote, ToTokens};
|
use quote::{quote, ToTokens};
|
||||||
use syn::{
|
use syn::{
|
||||||
braced,
|
|
||||||
parse::{Parse, ParseStream},
|
parse::{Parse, ParseStream},
|
||||||
spanned::Spanned,
|
Field, Token, Type,
|
||||||
Attribute, Field, Token, Type,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub(crate) mod attribute;
|
pub(crate) mod attribute;
|
||||||
@ -50,7 +48,7 @@ impl TryFrom<RawApi> for Api {
|
|||||||
let res = Self {
|
let res = Self {
|
||||||
metadata: raw_api.metadata,
|
metadata: raw_api.metadata,
|
||||||
request: raw_api.request,
|
request: raw_api.request,
|
||||||
response: raw_api.response.try_into()?,
|
response: raw_api.response,
|
||||||
error: match raw_api.error {
|
error: match raw_api.error {
|
||||||
Some(raw_err) => raw_err.ty.to_token_stream(),
|
Some(raw_err) => raw_err.ty.to_token_stream(),
|
||||||
None => quote! { #import_path::error::Void },
|
None => quote! { #import_path::error::Void },
|
||||||
@ -399,7 +397,6 @@ impl ToTokens for Api {
|
|||||||
mod kw {
|
mod kw {
|
||||||
use syn::custom_keyword;
|
use syn::custom_keyword;
|
||||||
|
|
||||||
custom_keyword!(response);
|
|
||||||
custom_keyword!(error);
|
custom_keyword!(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -412,7 +409,7 @@ pub struct RawApi {
|
|||||||
pub request: Request,
|
pub request: Request,
|
||||||
|
|
||||||
/// The `response` section of the macro.
|
/// The `response` section of the macro.
|
||||||
pub response: RawResponse,
|
pub response: Response,
|
||||||
|
|
||||||
/// The `error` section of the macro.
|
/// The `error` section of the macro.
|
||||||
pub error: Option<RawErrorType>,
|
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 struct RawErrorType {
|
||||||
pub error_kw: kw::error,
|
pub error_kw: kw::error,
|
||||||
pub ty: Type,
|
pub ty: Type,
|
||||||
|
@ -1,19 +1,28 @@
|
|||||||
//! Details of the `response` section of the procedural macro.
|
//! Details of the `response` section of the procedural macro.
|
||||||
|
|
||||||
use std::{convert::TryFrom, mem};
|
use std::mem;
|
||||||
|
|
||||||
use proc_macro2::TokenStream;
|
use proc_macro2::TokenStream;
|
||||||
use quote::{quote, quote_spanned, ToTokens};
|
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::{
|
use crate::{
|
||||||
api::{
|
api::{
|
||||||
attribute::{Meta, MetaNameValue},
|
attribute::{Meta, MetaNameValue},
|
||||||
strip_serde_attrs, RawResponse,
|
strip_serde_attrs,
|
||||||
},
|
},
|
||||||
util,
|
util,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
mod kw {
|
||||||
|
syn::custom_keyword!(response);
|
||||||
|
}
|
||||||
|
|
||||||
/// The result of processing the `response` section of the macro.
|
/// The result of processing the `response` section of the macro.
|
||||||
pub struct Response {
|
pub struct Response {
|
||||||
/// The attributes that will be applied to the struct definition.
|
/// The attributes that will be applied to the struct definition.
|
||||||
@ -202,14 +211,32 @@ impl Response {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<RawResponse> for Response {
|
impl Parse for Response {
|
||||||
type Error = syn::Error;
|
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 mut newtype_body_field = None;
|
||||||
|
|
||||||
let fields = raw
|
let fields = fields
|
||||||
.fields
|
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|mut field| {
|
.map(|mut field| {
|
||||||
let mut field_kind = None;
|
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()) {
|
if newtype_body_field.is_some() && fields.iter().any(|f| f.is_body()) {
|
||||||
// TODO: highlight conflicting fields,
|
// TODO: highlight conflicting fields,
|
||||||
return Err(syn::Error::new_spanned(
|
return Err(syn::Error::new_spanned(
|
||||||
raw.response_kw,
|
response_kw,
|
||||||
"Can't have both a newtype body field and regular body fields",
|
"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