From 1a646075475493b23b3f32722cd3ab029032adfe Mon Sep 17 00:00:00 2001 From: Matthew Pomes Date: Fri, 22 Nov 2024 10:46:14 -0600 Subject: [PATCH] Fix minor issues --- core/codegen/src/attribute/catch/mod.rs | 3 ++- core/codegen/src/attribute/route/mod.rs | 15 +++++++++++---- core/codegen/src/derive/typed_error.rs | 13 ++++++++----- core/codegen/src/lib.rs | 2 +- core/codegen/tests/catcher.rs | 6 ++++-- core/codegen/tests/typed_error.rs | 11 +++++++++++ core/lib/src/router/collider.rs | 4 +++- core/lib/src/router/router.rs | 5 +++-- 8 files changed, 43 insertions(+), 16 deletions(-) diff --git a/core/codegen/src/attribute/catch/mod.rs b/core/codegen/src/attribute/catch/mod.rs index bd1f081cf1..2f1ba84a22 100644 --- a/core/codegen/src/attribute/catch/mod.rs +++ b/core/codegen/src/attribute/catch/mod.rs @@ -61,7 +61,8 @@ pub fn _catch( ::rocket::trace::error!( downcast_to = stringify!(#ty), error_name = #__error.name(), - "Failed to downcast error. This should never happen, please open an issue with details." + "Failed to downcast error. This should never happen, please \ + open an issue with details." ); return #_Err(#Status::InternalServerError); }, diff --git a/core/codegen/src/attribute/route/mod.rs b/core/codegen/src/attribute/route/mod.rs index ba43576332..169a5e4be5 100644 --- a/core/codegen/src/attribute/route/mod.rs +++ b/core/codegen/src/attribute/route/mod.rs @@ -113,6 +113,12 @@ fn query_decls(route: &Route) -> Option { "{_err}" ); } } ); + ::rocket::trace::info!( + name: "forward", + target: concat!("rocket::codegen::route::", module_path!()), + error_name = #TypedError::name(&__e), + "parameter guard forwarding" + ); return #Outcome::Forward(( #__data, @@ -141,6 +147,7 @@ fn request_guard_decl(guard: &Guard) -> TokenStream { parameter = stringify!(#ident), type_name = stringify!(#ty), status = #TypedError::status(&__e).code, + error_name = #TypedError::name(&__e), "request guard forwarding" ); @@ -156,8 +163,8 @@ fn request_guard_decl(guard: &Guard) -> TokenStream { target: concat!("rocket::codegen::route::", module_path!()), parameter = stringify!(#ident), type_name = stringify!(#ty), + status = #TypedError::status(&__c).code, error_name = #TypedError::name(&__c), - // reason = %#display_hack!(__e), "request guard failed" ); @@ -181,8 +188,7 @@ fn param_guard_decl(guard: &Guard) -> TokenStream { target: concat!("rocket::codegen::route::", module_path!()), parameter = #name, type_name = stringify!(#ty), - name = #TypedError::name(&__error), - // reason = %#display_hack!(__error), + error_name = #TypedError::name(&__error), "path guard forwarding" ); @@ -248,6 +254,7 @@ fn data_guard_decl(guard: &Guard) -> TokenStream { parameter = stringify!(#ident), type_name = stringify!(#ty), status = #TypedError::status(&__e).code, + error_name = #TypedError::name(&__e), "data guard forwarding" ); @@ -263,7 +270,7 @@ fn data_guard_decl(guard: &Guard) -> TokenStream { target: concat!("rocket::codegen::route::", module_path!()), parameter = stringify!(#ident), type_name = stringify!(#ty), - // reason = %#display_hack!(__e), + error_name = #TypedError::name(&__e), "data guard failed" ); diff --git a/core/codegen/src/derive/typed_error.rs b/core/codegen/src/derive/typed_error.rs index 17efb4bd4e..c3c9a68bfd 100644 --- a/core/codegen/src/derive/typed_error.rs +++ b/core/codegen/src/derive/typed_error.rs @@ -128,9 +128,12 @@ pub fn derive_typed_error(input: proc_macro::TokenStream) -> TokenStream { }) ) .validator(ValidatorBuild::new() - .input_validate(|_, i| match i.generics().lifetimes().count() > 1 { - true => Err(i.generics().span().error("only one lifetime is supported")), - false => Ok(()) + .input_validate(|_, i| if i.generics().lifetimes().count() > 1 { + Err(i.generics().span().error("only one lifetime is supported")) + } else if i.generics().const_params().count() > 0 { + Err(i.generics().span().error("const params are not supported")) + } else { + Ok(()) }) ) .inner_mapper(MapperBuild::new() @@ -146,14 +149,14 @@ pub fn derive_typed_error(input: proc_macro::TokenStream) -> TokenStream { match g { syn::GenericParam::Lifetime(_) => quote!{ 'static }, syn::GenericParam::Type(TypeParam { ident, .. }) => quote! { #ident }, - syn::GenericParam::Const(ConstParam { .. }) => todo!(), + syn::GenericParam::Const(ConstParam { .. }) => unreachable!(), } }); let trans = input.generics() .lifetimes() .map(|LifetimeParam { lifetime, .. }| quote!{#_catcher::Inv<#lifetime>}); quote!{ - type Static = #name <#(#args)*>; + type Static = #name <#(#args,)*>; type Transience = (#(#trans,)*); } }) diff --git a/core/codegen/src/lib.rs b/core/codegen/src/lib.rs index c4a5ca8ac9..1282b4cf33 100644 --- a/core/codegen/src/lib.rs +++ b/core/codegen/src/lib.rs @@ -1035,7 +1035,7 @@ pub fn derive_responder(input: TokenStream) -> TokenStream { /// name: &'r str, /// value: &'r str, /// } -/// +/// /// #[derive(TypedError)] /// enum HeaderError { /// InvalidValue, diff --git a/core/codegen/tests/catcher.rs b/core/codegen/tests/catcher.rs index 3887b9a9d9..e6dd4f8b5e 100644 --- a/core/codegen/tests/catcher.rs +++ b/core/codegen/tests/catcher.rs @@ -105,11 +105,13 @@ fn test_basic_params() { #[catch(default, error = "")] fn test_io_error(e: &io::Error) -> String { format!("{e:?}") } #[catch(default, error = "<_e>")] -fn test_parse_int_error(_e: &ParseIntError) -> String { println!("ParseIntError"); format!("ParseIntError") } +fn test_parse_int_error(_e: &ParseIntError) -> String { format!("ParseIntError") } #[catch(default, error = "<_e>")] fn test_parse_bool_error(_e: &ParseBoolError) -> String { format!("ParseBoolError") } #[catch(default, error = "")] -fn test_param_parse_bool_error(e: &FromParamError<'_, ParseBoolError>) -> String { format!("ParseBoolError: {}", e.raw) } +fn test_param_parse_bool_error(e: &FromParamError<'_, ParseBoolError>) -> String { + format!("ParseBoolError: {}", e.raw) +} #[test] diff --git a/core/codegen/tests/typed_error.rs b/core/codegen/tests/typed_error.rs index cbe9ae6cfd..1f47bef56f 100644 --- a/core/codegen/tests/typed_error.rs +++ b/core/codegen/tests/typed_error.rs @@ -67,3 +67,14 @@ fn validate_static() { )); boxed_error(Box::new(val)); } + +#[derive(TypedError)] +pub enum Generic { + First(E), +} + +#[derive(TypedError)] +pub struct GenericWithLifetime<'r, E> { + s: &'r str, + inner: E, +} diff --git a/core/lib/src/router/collider.rs b/core/lib/src/router/collider.rs index c62d3ef6ad..7cdadf9473 100644 --- a/core/lib/src/router/collider.rs +++ b/core/lib/src/router/collider.rs @@ -141,7 +141,9 @@ impl Catcher { /// assert!(!a.collides_with(&b)); /// ``` pub fn collides_with(&self, other: &Self) -> bool { - self.code == other.code && self.base().segments().eq(other.base().segments()) && self.type_id == other.type_id + self.code == other.code + && self.base().segments().eq(other.base().segments()) + && self.type_id == other.type_id } } diff --git a/core/lib/src/router/router.rs b/core/lib/src/router/router.rs index b810c21bd1..d95540c664 100644 --- a/core/lib/src/router/router.rs +++ b/core/lib/src/router/router.rs @@ -593,7 +593,8 @@ mod test { { let mut router = Router::new(); for (status, base, ty) in catchers { - let mut catcher = Catcher::new(status.map(|s| s.code), catcher::dummy_handler).rebase(Origin::parse(base).unwrap()); + let mut catcher = Catcher::new(status.map(|s| s.code), catcher::dummy_handler) + .rebase(Origin::parse(base).unwrap()); catcher.type_id = ty; router.catchers.push(catcher); } @@ -646,7 +647,7 @@ mod test { let request = client.req(Method::Get, Origin::parse(uri).unwrap()); router.catch_any(status, ty(&()), &request) } - + #[test] fn test_catch_vs_catch_any() { let router = make_router_catches([(None, "/", None)]).unwrap();