From 92beac9893c0d1efa0dc525ab8b4b68acefac509 Mon Sep 17 00:00:00 2001 From: Franklin Date: Wed, 31 Aug 2022 13:41:59 -0400 Subject: [PATCH] Fixed macro --- Cargo.lock | 2 +- Cargo.toml | 2 +- Readme.md | 2 +- src/utils/macros.rs | 14 +++++++------- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 298a951..b33184d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -183,7 +183,7 @@ dependencies = [ [[package]] name = "actix-web-utils" -version = "0.2.5" +version = "0.2.6" dependencies = [ "actix-web", "log", diff --git a/Cargo.toml b/Cargo.toml index 5677039..0a0bc04 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-web-utils" -version = "0.2.5" +version = "0.2.6" edition = "2021" authors = ["Franklin E. Blanco"] description = "Just some useful addons for actix web." diff --git a/Readme.md b/Readme.md index d46f31d..ff5bff1 100644 --- a/Readme.md +++ b/Readme.md @@ -74,7 +74,7 @@ When you could be doing this: fn service_layer_function() -> TypedHttpResponse { //T can be whatever you want // ... Some Business logic let value_returned_from_match = unwrap_or_return_handled_error!( - function_that_returns_a_result() + function_that_returns_a_result(), T ); // ... More Business logic } diff --git a/src/utils/macros.rs b/src/utils/macros.rs index 748bc64..ef6eaf3 100644 --- a/src/utils/macros.rs +++ b/src/utils/macros.rs @@ -1,5 +1,5 @@ /// This is to minimize the amount of matches made in the code -/// Give it a Result and it'll +/// Give it a Result and the type of the success and it'll /// Basically unwrap the result if its there and if it isn't it'll return a handled error inside a TypedHttpResponse. /// Default status code is InternalServerError, if you want something different pass it as the first argument as a u16. /// If you want to also return the success result, then pass a valid status code u16 as a second argument @@ -7,22 +7,22 @@ #[allow(unused_macros)] #[macro_export] macro_rules! unwrap_or_return_handled_error { - ( $e:expr ) => { + ( $e:expr, $type_of_resp:ty ) => { match $e { Ok(value) => value, - Err(error) => return actix_web_utils::traits::macro_traits::ReturnableErrorShape::convert_to_returnable(&error, 500) + Err(error) => return actix_web_utils::traits::macro_traits::ReturnableErrorShape::convert_to_returnable::<$type_of_resp>::(&error, 500) } }; - ( $error_status_code:literal, $e:expr ) => { + ( $error_status_code:literal, $e:expr, $type_of_resp:ty ) => { match $e { Ok(value) => value, - Err(error) => return actix_web_utils::traits::macro_traits::ReturnableErrorShape::convert_to_returnable(&error, error_status_code) + Err(error) => return actix_web_utils::traits::macro_traits::ReturnableErrorShape::convert_to_returnable::<$type_of_resp>::(&error, error_status_code) } }; - ( $error_status_code:literal, $success_status_code:literal, $e:expr) => { + ( $error_status_code:literal, $success_status_code:literal, $e:expr, $type_of_resp:ty) => { match $e { Ok(value) => return actix_web_utils::typed_response::TypedHttpResponse::return_standard_response($success_status_code, value), - Err(error) => return actix_web_utils::traits::macro_traits::ReturnableErrorShape::convert_to_returnable(&error, error_status_code) + Err(error) => return actix_web_utils::traits::macro_traits::ReturnableErrorShape::convert_to_returnable::<$type_of_resp>::(&error, error_status_code) } } } \ No newline at end of file