api-macros: Remove RawRequest
This commit is contained in:
parent
00ae067bce
commit
05249c6cdb
@ -49,7 +49,7 @@ impl TryFrom<RawApi> for Api {
|
|||||||
|
|
||||||
let res = Self {
|
let res = Self {
|
||||||
metadata: raw_api.metadata,
|
metadata: raw_api.metadata,
|
||||||
request: raw_api.request.try_into()?,
|
request: raw_api.request,
|
||||||
response: raw_api.response.try_into()?,
|
response: raw_api.response.try_into()?,
|
||||||
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(),
|
||||||
@ -399,7 +399,6 @@ impl ToTokens for Api {
|
|||||||
mod kw {
|
mod kw {
|
||||||
use syn::custom_keyword;
|
use syn::custom_keyword;
|
||||||
|
|
||||||
custom_keyword!(request);
|
|
||||||
custom_keyword!(response);
|
custom_keyword!(response);
|
||||||
custom_keyword!(error);
|
custom_keyword!(error);
|
||||||
}
|
}
|
||||||
@ -410,7 +409,7 @@ pub struct RawApi {
|
|||||||
pub metadata: Metadata,
|
pub metadata: Metadata,
|
||||||
|
|
||||||
/// The `request` section of the macro.
|
/// The `request` section of the macro.
|
||||||
pub request: RawRequest,
|
pub request: Request,
|
||||||
|
|
||||||
/// The `response` section of the macro.
|
/// The `response` section of the macro.
|
||||||
pub response: RawResponse,
|
pub response: RawResponse,
|
||||||
@ -430,31 +429,6 @@ impl Parse for RawApi {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct RawRequest {
|
|
||||||
pub attributes: Vec<Attribute>,
|
|
||||||
pub request_kw: kw::request,
|
|
||||||
pub fields: Vec<Field>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Parse for RawRequest {
|
|
||||||
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
|
|
||||||
let attributes = input.call(Attribute::parse_outer)?;
|
|
||||||
let request_kw = input.parse::<kw::request>()?;
|
|
||||||
input.parse::<Token![:]>()?;
|
|
||||||
let fields;
|
|
||||||
braced!(fields in input);
|
|
||||||
|
|
||||||
Ok(Self {
|
|
||||||
attributes,
|
|
||||||
request_kw,
|
|
||||||
fields: fields
|
|
||||||
.parse_terminated::<Field, Token![,]>(Field::parse_named)?
|
|
||||||
.into_iter()
|
|
||||||
.collect(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct RawResponse {
|
pub struct RawResponse {
|
||||||
pub attributes: Vec<Attribute>,
|
pub attributes: Vec<Attribute>,
|
||||||
pub response_kw: kw::response,
|
pub response_kw: kw::response,
|
||||||
|
@ -1,19 +1,28 @@
|
|||||||
//! Details of the `request` section of the procedural macro.
|
//! Details of the `request` section of the procedural macro.
|
||||||
|
|
||||||
use std::{collections::BTreeSet, convert::TryFrom, mem};
|
use std::{collections::BTreeSet, 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, Lifetime};
|
use syn::{
|
||||||
|
braced,
|
||||||
|
parse::{Parse, ParseStream},
|
||||||
|
spanned::Spanned,
|
||||||
|
Attribute, Field, Ident, Lifetime, Token,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
api::{
|
api::{
|
||||||
attribute::{Meta, MetaNameValue},
|
attribute::{Meta, MetaNameValue},
|
||||||
strip_serde_attrs, RawRequest,
|
strip_serde_attrs,
|
||||||
},
|
},
|
||||||
util,
|
util,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
mod kw {
|
||||||
|
syn::custom_keyword!(request);
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct RequestLifetimes {
|
pub struct RequestLifetimes {
|
||||||
body: BTreeSet<Lifetime>,
|
body: BTreeSet<Lifetime>,
|
||||||
@ -286,16 +295,21 @@ impl Request {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<RawRequest> for Request {
|
impl Parse for Request {
|
||||||
type Error = syn::Error;
|
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
|
||||||
|
let attributes = input.call(Attribute::parse_outer)?;
|
||||||
|
let request_kw = input.parse::<kw::request>()?;
|
||||||
|
input.parse::<Token![:]>()?;
|
||||||
|
let fields;
|
||||||
|
braced!(fields in input);
|
||||||
|
|
||||||
|
let fields = fields.parse_terminated::<Field, Token![,]>(Field::parse_named)?;
|
||||||
|
|
||||||
fn try_from(raw: RawRequest) -> syn::Result<Self> {
|
|
||||||
let mut newtype_body_field = None;
|
let mut newtype_body_field = None;
|
||||||
let mut query_map_field = None;
|
let mut query_map_field = None;
|
||||||
let mut lifetimes = RequestLifetimes::default();
|
let mut lifetimes = RequestLifetimes::default();
|
||||||
|
|
||||||
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;
|
||||||
@ -392,7 +406,7 @@ impl TryFrom<RawRequest> for Request {
|
|||||||
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.request_kw,
|
request_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",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
@ -400,7 +414,7 @@ impl TryFrom<RawRequest> for Request {
|
|||||||
if query_map_field.is_some() && fields.iter().any(|f| f.is_query()) {
|
if query_map_field.is_some() && fields.iter().any(|f| f.is_query()) {
|
||||||
return Err(syn::Error::new_spanned(
|
return Err(syn::Error::new_spanned(
|
||||||
// TODO: raw,
|
// TODO: raw,
|
||||||
raw.request_kw,
|
request_kw,
|
||||||
"Can't have both a query map field and regular query fields",
|
"Can't have both a query map field and regular query fields",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
@ -408,17 +422,12 @@ impl TryFrom<RawRequest> for Request {
|
|||||||
// TODO when/if `&[(&str, &str)]` is supported remove this
|
// TODO when/if `&[(&str, &str)]` is supported remove this
|
||||||
if query_map_field.is_some() && !lifetimes.query.is_empty() {
|
if query_map_field.is_some() && !lifetimes.query.is_empty() {
|
||||||
return Err(syn::Error::new_spanned(
|
return Err(syn::Error::new_spanned(
|
||||||
raw.request_kw,
|
request_kw,
|
||||||
"Lifetimes are not allowed for query_map fields",
|
"Lifetimes are not allowed for query_map fields",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self { attributes, fields, lifetimes, ruma_api_import: util::import_ruma_api() })
|
||||||
attributes: raw.attributes,
|
|
||||||
fields,
|
|
||||||
lifetimes,
|
|
||||||
ruma_api_import: util::import_ruma_api(),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user