macros: Consistently create Incoming* types from Incoming derive

… as a type alias if there are no lifetime generics on the input type.
This commit is contained in:
Jonas Platte 2022-03-22 10:27:56 +01:00 committed by Jonas Platte
parent 5a791b3c6e
commit 54f9db8ccc
3 changed files with 25 additions and 12 deletions

View File

@ -1,4 +1,4 @@
use ruma_common::api::{ruma_api, IncomingRequest};
use ruma_common::api::{ruma_api, IncomingRequest as _};
ruma_api! {
metadata: {

View File

@ -1,10 +1,7 @@
use bytes::BufMut;
use ruma_common::{
api::{
use ruma_common::api::{
error::{FromHttpResponseError, IntoHttpError, MatrixError},
ruma_api, IncomingResponse, OutgoingResponse,
},
serde::Incoming,
};
ruma_api! {
@ -25,7 +22,6 @@ ruma_api! {
}
}
#[derive(Incoming)]
pub struct Response;
impl IncomingResponse for Response {

View File

@ -5,8 +5,8 @@ use syn::{
parse_quote,
punctuated::Punctuated,
AngleBracketedGenericArguments, Attribute, Data, DeriveInput, GenericArgument, GenericParam,
Generics, Ident, ParenthesizedGenericArguments, Path, PathArguments, Token, Type, TypePath,
TypeReference, TypeSlice,
Generics, Ident, ItemType, ParenthesizedGenericArguments, Path, PathArguments, Token, Type,
TypePath, TypeReference, TypeSlice,
};
use crate::util::import_ruma_common;
@ -38,8 +38,25 @@ pub fn expand_derive_incoming(mut ty_def: DeriveInput) -> syn::Result<TokenStrea
}
}
let ident = format_ident!("Incoming{}", ty_def.ident, span = Span::call_site());
if !found_lifetime {
return Ok(TokenStream::new());
let doc = format!(
"Convenience type alias for [{}], for consistency with other [{}] types.",
&ty_def.ident, ident
);
let mut type_alias: ItemType = parse_quote! { type X = Y; };
type_alias.vis = ty_def.vis.clone();
type_alias.ident = ident;
type_alias.generics = ty_def.generics.clone();
type_alias.ty =
Box::new(TypePath { qself: None, path: ty_def.ident.clone().into() }.into());
return Ok(quote! {
#[doc = #doc]
#type_alias
});
}
let mut derives = vec![quote! { Debug }];
@ -73,7 +90,7 @@ pub fn expand_derive_incoming(mut ty_def: DeriveInput) -> syn::Result<TokenStrea
clean_generics(&mut ty_def.generics);
let doc = format!("'Incoming' variant of [{}].", &ty_def.ident);
ty_def.ident = format_ident!("Incoming{}", ty_def.ident, span = Span::call_site());
ty_def.ident = ident;
Ok(quote! {
#[doc = #doc]