api: Remove error_ty from Request and Response derive attributes
This commit is contained in:
parent
e7e39a6af1
commit
1989e0350d
@ -74,8 +74,8 @@ impl Api {
|
|||||||
|err_ty| quote! { #err_ty },
|
|err_ty| quote! { #err_ty },
|
||||||
);
|
);
|
||||||
|
|
||||||
let request = self.request.map(|req| req.expand(metadata, &error_ty, &ruma_common));
|
let request = self.request.map(|req| req.expand(metadata, &ruma_common));
|
||||||
let response = self.response.map(|res| res.expand(metadata, &error_ty, &ruma_common));
|
let response = self.response.map(|res| res.expand(metadata, &ruma_common));
|
||||||
|
|
||||||
let metadata_doc = format!("Metadata for the `{}` API endpoint.", name.value());
|
let metadata_doc = format!("Metadata for the `{}` API endpoint.", name.value());
|
||||||
|
|
||||||
@ -93,11 +93,11 @@ impl Api {
|
|||||||
history: #history,
|
history: #history,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[allow(unused)]
|
||||||
|
type EndpointError = #error_ty;
|
||||||
|
|
||||||
#request
|
#request
|
||||||
#response
|
#response
|
||||||
|
|
||||||
#[cfg(not(any(feature = "client", feature = "server")))]
|
|
||||||
type _SilenceUnusedError = #error_ty;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,12 +67,7 @@ impl Request {
|
|||||||
lifetimes
|
lifetimes
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn expand(
|
pub(super) fn expand(&self, metadata: &Metadata, ruma_common: &TokenStream) -> TokenStream {
|
||||||
&self,
|
|
||||||
metadata: &Metadata,
|
|
||||||
error_ty: &TokenStream,
|
|
||||||
ruma_common: &TokenStream,
|
|
||||||
) -> TokenStream {
|
|
||||||
let ruma_macros = quote! { #ruma_common::exports::ruma_macros };
|
let ruma_macros = quote! { #ruma_common::exports::ruma_macros };
|
||||||
|
|
||||||
let docs = format!(
|
let docs = format!(
|
||||||
@ -100,7 +95,7 @@ impl Request {
|
|||||||
)]
|
)]
|
||||||
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
|
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
|
||||||
#[incoming_derive(!Deserialize, #ruma_macros::_FakeDeriveRumaApi)]
|
#[incoming_derive(!Deserialize, #ruma_macros::_FakeDeriveRumaApi)]
|
||||||
#[ruma_api(authentication = #authentication, error_ty = #error_ty)]
|
#[ruma_api(authentication = #authentication)]
|
||||||
#( #struct_attributes )*
|
#( #struct_attributes )*
|
||||||
pub struct #request_ident < #(#lifetimes),* > {
|
pub struct #request_ident < #(#lifetimes),* > {
|
||||||
#fields
|
#fields
|
||||||
|
@ -19,12 +19,7 @@ pub(crate) struct Response {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Response {
|
impl Response {
|
||||||
pub(super) fn expand(
|
pub(super) fn expand(&self, metadata: &Metadata, ruma_common: &TokenStream) -> TokenStream {
|
||||||
&self,
|
|
||||||
metadata: &Metadata,
|
|
||||||
error_ty: &TokenStream,
|
|
||||||
ruma_common: &TokenStream,
|
|
||||||
) -> TokenStream {
|
|
||||||
let ruma_macros = quote! { #ruma_common::exports::ruma_macros };
|
let ruma_macros = quote! { #ruma_common::exports::ruma_macros };
|
||||||
|
|
||||||
let docs =
|
let docs =
|
||||||
@ -44,7 +39,6 @@ impl Response {
|
|||||||
)]
|
)]
|
||||||
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
|
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
|
||||||
#[incoming_derive(!Deserialize, #ruma_macros::_FakeDeriveRumaApi)]
|
#[incoming_derive(!Deserialize, #ruma_macros::_FakeDeriveRumaApi)]
|
||||||
#[ruma_api(error_ty = #error_ty)]
|
|
||||||
#( #struct_attributes )*
|
#( #struct_attributes )*
|
||||||
pub struct #response_ident {
|
pub struct #response_ident {
|
||||||
#fields
|
#fields
|
||||||
|
@ -13,7 +13,6 @@ mod kw {
|
|||||||
syn::custom_keyword!(query_map);
|
syn::custom_keyword!(query_map);
|
||||||
syn::custom_keyword!(header);
|
syn::custom_keyword!(header);
|
||||||
syn::custom_keyword!(authentication);
|
syn::custom_keyword!(authentication);
|
||||||
syn::custom_keyword!(error_ty);
|
|
||||||
syn::custom_keyword!(manual_body_serde);
|
syn::custom_keyword!(manual_body_serde);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,7 +55,6 @@ impl Parse for RequestMeta {
|
|||||||
|
|
||||||
pub enum DeriveRequestMeta {
|
pub enum DeriveRequestMeta {
|
||||||
Authentication(Type),
|
Authentication(Type),
|
||||||
ErrorTy(Type),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Parse for DeriveRequestMeta {
|
impl Parse for DeriveRequestMeta {
|
||||||
@ -66,10 +64,6 @@ impl Parse for DeriveRequestMeta {
|
|||||||
let _: kw::authentication = input.parse()?;
|
let _: kw::authentication = input.parse()?;
|
||||||
let _: Token![=] = input.parse()?;
|
let _: Token![=] = input.parse()?;
|
||||||
input.parse().map(Self::Authentication)
|
input.parse().map(Self::Authentication)
|
||||||
} else if lookahead.peek(kw::error_ty) {
|
|
||||||
let _: kw::error_ty = input.parse()?;
|
|
||||||
let _: Token![=] = input.parse()?;
|
|
||||||
input.parse().map(Self::ErrorTy)
|
|
||||||
} else {
|
} else {
|
||||||
Err(lookahead.error())
|
Err(lookahead.error())
|
||||||
}
|
}
|
||||||
@ -104,7 +98,6 @@ impl Parse for ResponseMeta {
|
|||||||
#[allow(clippy::large_enum_variant)]
|
#[allow(clippy::large_enum_variant)]
|
||||||
pub enum DeriveResponseMeta {
|
pub enum DeriveResponseMeta {
|
||||||
ManualBodySerde,
|
ManualBodySerde,
|
||||||
ErrorTy(Type),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Parse for DeriveResponseMeta {
|
impl Parse for DeriveResponseMeta {
|
||||||
@ -113,10 +106,6 @@ impl Parse for DeriveResponseMeta {
|
|||||||
if lookahead.peek(kw::manual_body_serde) {
|
if lookahead.peek(kw::manual_body_serde) {
|
||||||
let _: kw::manual_body_serde = input.parse()?;
|
let _: kw::manual_body_serde = input.parse()?;
|
||||||
Ok(Self::ManualBodySerde)
|
Ok(Self::ManualBodySerde)
|
||||||
} else if lookahead.peek(kw::error_ty) {
|
|
||||||
let _: kw::error_ty = input.parse()?;
|
|
||||||
let _: Token![=] = input.parse()?;
|
|
||||||
input.parse().map(Self::ErrorTy)
|
|
||||||
} else {
|
} else {
|
||||||
Err(lookahead.error())
|
Err(lookahead.error())
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ use syn::{
|
|||||||
parse::{Parse, ParseStream},
|
parse::{Parse, ParseStream},
|
||||||
parse_quote,
|
parse_quote,
|
||||||
punctuated::Punctuated,
|
punctuated::Punctuated,
|
||||||
DeriveInput, Field, Generics, Ident, Lifetime, Token, Type,
|
DeriveInput, Field, Generics, Ident, Lifetime, Token,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
@ -47,7 +47,6 @@ pub fn expand_derive_request(input: DeriveInput) -> syn::Result<TokenStream> {
|
|||||||
.collect::<syn::Result<_>>()?;
|
.collect::<syn::Result<_>>()?;
|
||||||
|
|
||||||
let mut authentication = None;
|
let mut authentication = None;
|
||||||
let mut error_ty = None;
|
|
||||||
|
|
||||||
for attr in input.attrs {
|
for attr in input.attrs {
|
||||||
if !attr.path.is_ident("ruma_api") {
|
if !attr.path.is_ident("ruma_api") {
|
||||||
@ -59,7 +58,6 @@ pub fn expand_derive_request(input: DeriveInput) -> syn::Result<TokenStream> {
|
|||||||
for meta in metas {
|
for meta in metas {
|
||||||
match meta {
|
match meta {
|
||||||
DeriveRequestMeta::Authentication(t) => authentication = Some(parse_quote!(#t)),
|
DeriveRequestMeta::Authentication(t) => authentication = Some(parse_quote!(#t)),
|
||||||
DeriveRequestMeta::ErrorTy(t) => error_ty = Some(t),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -70,7 +68,6 @@ pub fn expand_derive_request(input: DeriveInput) -> syn::Result<TokenStream> {
|
|||||||
fields,
|
fields,
|
||||||
lifetimes,
|
lifetimes,
|
||||||
authentication: authentication.expect("missing authentication attribute"),
|
authentication: authentication.expect("missing authentication attribute"),
|
||||||
error_ty: error_ty.expect("missing error_ty attribute"),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let ruma_common = import_ruma_common();
|
let ruma_common = import_ruma_common();
|
||||||
@ -98,7 +95,6 @@ struct Request {
|
|||||||
fields: Vec<RequestField>,
|
fields: Vec<RequestField>,
|
||||||
|
|
||||||
authentication: AuthScheme,
|
authentication: AuthScheme,
|
||||||
error_ty: Type,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Request {
|
impl Request {
|
||||||
|
@ -11,8 +11,6 @@ impl Request {
|
|||||||
let serde = quote! { #ruma_common::exports::serde };
|
let serde = quote! { #ruma_common::exports::serde };
|
||||||
let serde_json = quote! { #ruma_common::exports::serde_json };
|
let serde_json = quote! { #ruma_common::exports::serde_json };
|
||||||
|
|
||||||
let error_ty = &self.error_ty;
|
|
||||||
|
|
||||||
let incoming_request_type = if self.has_lifetimes() {
|
let incoming_request_type = if self.has_lifetimes() {
|
||||||
quote! { IncomingRequest }
|
quote! { IncomingRequest }
|
||||||
} else {
|
} else {
|
||||||
@ -189,7 +187,7 @@ impl Request {
|
|||||||
#[automatically_derived]
|
#[automatically_derived]
|
||||||
#[cfg(feature = "server")]
|
#[cfg(feature = "server")]
|
||||||
impl #ruma_common::api::IncomingRequest for #incoming_request_type {
|
impl #ruma_common::api::IncomingRequest for #incoming_request_type {
|
||||||
type EndpointError = #error_ty;
|
type EndpointError = self::EndpointError;
|
||||||
type OutgoingResponse = Response;
|
type OutgoingResponse = Response;
|
||||||
|
|
||||||
const METADATA: #ruma_common::api::Metadata = METADATA;
|
const METADATA: #ruma_common::api::Metadata = METADATA;
|
||||||
|
@ -10,8 +10,6 @@ impl Request {
|
|||||||
let bytes = quote! { #ruma_common::exports::bytes };
|
let bytes = quote! { #ruma_common::exports::bytes };
|
||||||
let http = quote! { #ruma_common::exports::http };
|
let http = quote! { #ruma_common::exports::http };
|
||||||
|
|
||||||
let error_ty = &self.error_ty;
|
|
||||||
|
|
||||||
let path_fields =
|
let path_fields =
|
||||||
self.path_fields().map(|f| f.ident.as_ref().expect("path fields have a name"));
|
self.path_fields().map(|f| f.ident.as_ref().expect("path fields have a name"));
|
||||||
|
|
||||||
@ -155,7 +153,7 @@ impl Request {
|
|||||||
#[automatically_derived]
|
#[automatically_derived]
|
||||||
#[cfg(feature = "client")]
|
#[cfg(feature = "client")]
|
||||||
impl #impl_generics #ruma_common::api::OutgoingRequest for Request #ty_generics #where_clause {
|
impl #impl_generics #ruma_common::api::OutgoingRequest for Request #ty_generics #where_clause {
|
||||||
type EndpointError = #error_ty;
|
type EndpointError = self::EndpointError;
|
||||||
type IncomingResponse = Response;
|
type IncomingResponse = Response;
|
||||||
|
|
||||||
const METADATA: #ruma_common::api::Metadata = METADATA;
|
const METADATA: #ruma_common::api::Metadata = METADATA;
|
||||||
|
@ -23,7 +23,6 @@ pub fn expand_derive_response(input: DeriveInput) -> syn::Result<TokenStream> {
|
|||||||
|
|
||||||
let fields = fields.into_iter().map(ResponseField::try_from).collect::<syn::Result<_>>()?;
|
let fields = fields.into_iter().map(ResponseField::try_from).collect::<syn::Result<_>>()?;
|
||||||
let mut manual_body_serde = false;
|
let mut manual_body_serde = false;
|
||||||
let mut error_ty = None;
|
|
||||||
for attr in input.attrs {
|
for attr in input.attrs {
|
||||||
if !attr.path.is_ident("ruma_api") {
|
if !attr.path.is_ident("ruma_api") {
|
||||||
continue;
|
continue;
|
||||||
@ -34,18 +33,12 @@ pub fn expand_derive_response(input: DeriveInput) -> syn::Result<TokenStream> {
|
|||||||
for meta in metas {
|
for meta in metas {
|
||||||
match meta {
|
match meta {
|
||||||
DeriveResponseMeta::ManualBodySerde => manual_body_serde = true,
|
DeriveResponseMeta::ManualBodySerde => manual_body_serde = true,
|
||||||
DeriveResponseMeta::ErrorTy(t) => error_ty = Some(t),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let response = Response {
|
let response =
|
||||||
ident: input.ident,
|
Response { ident: input.ident, generics: input.generics, fields, manual_body_serde };
|
||||||
generics: input.generics,
|
|
||||||
fields,
|
|
||||||
manual_body_serde,
|
|
||||||
error_ty: error_ty.unwrap(),
|
|
||||||
};
|
|
||||||
|
|
||||||
response.check()?;
|
response.check()?;
|
||||||
Ok(response.expand_all())
|
Ok(response.expand_all())
|
||||||
@ -56,7 +49,6 @@ struct Response {
|
|||||||
generics: Generics,
|
generics: Generics,
|
||||||
fields: Vec<ResponseField>,
|
fields: Vec<ResponseField>,
|
||||||
manual_body_serde: bool,
|
manual_body_serde: bool,
|
||||||
error_ty: Type,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Response {
|
impl Response {
|
||||||
@ -109,7 +101,7 @@ impl Response {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let outgoing_response_impl = self.expand_outgoing(&ruma_common);
|
let outgoing_response_impl = self.expand_outgoing(&ruma_common);
|
||||||
let incoming_response_impl = self.expand_incoming(&self.error_ty, &ruma_common);
|
let incoming_response_impl = self.expand_incoming(&ruma_common);
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
#response_body_struct
|
#response_body_struct
|
||||||
|
@ -1,11 +1,10 @@
|
|||||||
use proc_macro2::TokenStream;
|
use proc_macro2::TokenStream;
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
use syn::Type;
|
|
||||||
|
|
||||||
use super::{Response, ResponseFieldKind};
|
use super::{Response, ResponseFieldKind};
|
||||||
|
|
||||||
impl Response {
|
impl Response {
|
||||||
pub fn expand_incoming(&self, error_ty: &Type, ruma_common: &TokenStream) -> TokenStream {
|
pub fn expand_incoming(&self, ruma_common: &TokenStream) -> TokenStream {
|
||||||
let http = quote! { #ruma_common::exports::http };
|
let http = quote! { #ruma_common::exports::http };
|
||||||
let serde_json = quote! { #ruma_common::exports::serde_json };
|
let serde_json = quote! { #ruma_common::exports::serde_json };
|
||||||
|
|
||||||
@ -104,37 +103,41 @@ impl Response {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let method_body = quote! {
|
||||||
|
if response.status().as_u16() < 400 {
|
||||||
|
#extract_response_headers
|
||||||
|
#typed_response_body_decl
|
||||||
|
|
||||||
|
::std::result::Result::Ok(Self {
|
||||||
|
#response_init_fields
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
match <EndpointError as #ruma_common::api::EndpointError>::try_from_http_response(
|
||||||
|
response
|
||||||
|
) {
|
||||||
|
::std::result::Result::Ok(err) => {
|
||||||
|
Err(#ruma_common::api::error::ServerError::Known(err).into())
|
||||||
|
}
|
||||||
|
::std::result::Result::Err(response_err) => {
|
||||||
|
Err(#ruma_common::api::error::ServerError::Unknown(response_err).into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
#[automatically_derived]
|
#[automatically_derived]
|
||||||
#[cfg(feature = "client")]
|
#[cfg(feature = "client")]
|
||||||
impl #ruma_common::api::IncomingResponse for Response {
|
impl #ruma_common::api::IncomingResponse for Response {
|
||||||
type EndpointError = #error_ty;
|
type EndpointError = EndpointError;
|
||||||
|
|
||||||
fn try_from_http_response<T: ::std::convert::AsRef<[::std::primitive::u8]>>(
|
fn try_from_http_response<T: ::std::convert::AsRef<[::std::primitive::u8]>>(
|
||||||
response: #http::Response<T>,
|
response: #http::Response<T>,
|
||||||
) -> ::std::result::Result<
|
) -> ::std::result::Result<
|
||||||
Self,
|
Self,
|
||||||
#ruma_common::api::error::FromHttpResponseError<#error_ty>,
|
#ruma_common::api::error::FromHttpResponseError<EndpointError>,
|
||||||
> {
|
> {
|
||||||
if response.status().as_u16() < 400 {
|
#method_body
|
||||||
#extract_response_headers
|
|
||||||
#typed_response_body_decl
|
|
||||||
|
|
||||||
::std::result::Result::Ok(Self {
|
|
||||||
#response_init_fields
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
match <#error_ty as #ruma_common::api::EndpointError>::try_from_http_response(
|
|
||||||
response
|
|
||||||
) {
|
|
||||||
::std::result::Result::Ok(err) => {
|
|
||||||
Err(#ruma_common::api::error::ServerError::Known(err).into())
|
|
||||||
}
|
|
||||||
::std::result::Result::Err(response_err) => {
|
|
||||||
Err(#ruma_common::api::error::ServerError::Unknown(response_err).into())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user