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