From 2f17de6013469171195df6ab43489ef5b4c273fc Mon Sep 17 00:00:00 2001 From: edgerunnergit Date: Sat, 4 Feb 2023 09:25:39 +0530 Subject: [PATCH] use existing Method guard instead of creating a new Custom gueard --- actix-web-codegen/src/route.rs | 12 ++++++------ actix-web-codegen/tests/test_macro.rs | 13 ++++++++++++- actix-web/CHANGES.md | 4 ---- actix-web/src/guard/mod.rs | 15 --------------- 4 files changed, 18 insertions(+), 26 deletions(-) diff --git a/actix-web-codegen/src/route.rs b/actix-web-codegen/src/route.rs index 5425bc531..4c0c8e366 100644 --- a/actix-web-codegen/src/route.rs +++ b/actix-web-codegen/src/route.rs @@ -63,7 +63,7 @@ method_type! { Options, OPTIONS, options, Trace, TRACE, trace, Patch, PATCH, patch, - Custom, CUSTOM, custom, + Method, METHOD, method, } impl ToTokens for MethodType { @@ -76,7 +76,7 @@ impl ToTokens for MethodType { impl ToTokens for MethodTypeExt { fn to_tokens(&self, stream: &mut TokenStream2) { match self.method { - MethodType::Custom => { + MethodType::Method => { let ident = Ident::new( self.custom_method.as_ref().unwrap().value().as_str(), Span::call_site(), @@ -186,7 +186,7 @@ impl Args { } else if let syn::Lit::Str(ref lit) = nv.lit { let method = MethodType::try_from(lit)?; if !methods.insert({ - if method == MethodType::Custom { + if method == MethodType::Method { MethodTypeExt { method, custom_method: Some(lit.clone()), @@ -355,7 +355,7 @@ impl ToTokens for Route { match custom_method { Some(lit) => { mult_method_guards.push(quote! { - .or(::actix_web::guard::#method_type(#lit.clone())) + .or(::actix_web::guard::#method_type(::actix_web::http::Method::from_bytes(#lit.as_bytes()).unwrap())) }); } None => { @@ -369,7 +369,7 @@ impl ToTokens for Route { Some(lit) => { quote! { .guard( - ::actix_web::guard::Any(::actix_web::guard::#first_method(#lit.clone())) + ::actix_web::guard::Any(::actix_web::guard::#first_method(::actix_web::http::Method::from_bytes(#lit.as_bytes()).unwrap())) #(#mult_method_guards)* ) } @@ -387,7 +387,7 @@ impl ToTokens for Route { match &first.custom_method { Some(lit) => { quote! { - .guard(::actix_web::guard::#first_method(#lit.clone())) + .guard(::actix_web::guard::#first_method(::actix_web::http::Method::from_bytes(#lit.as_bytes()).unwrap())) } } None => { diff --git a/actix-web-codegen/tests/test_macro.rs b/actix-web-codegen/tests/test_macro.rs index e61a7d6bc..fd47e5e09 100644 --- a/actix-web-codegen/tests/test_macro.rs +++ b/actix-web-codegen/tests/test_macro.rs @@ -91,12 +91,23 @@ async fn get_param_test(_: web::Path) -> impl Responder { method = "GET", method = "POST", method = "HEAD", - method = "CREATE" + method = "HELLO" )] async fn route_test() -> impl Responder { HttpResponse::Ok() } +// use actix_web::http::Method; +// use std::str::FromStr; +// let abc = Method::from_str("ABC").unwrap(); +// #[route( +// "/multi/custom", +// method = abc, +// )] +// async fn route_custom_test() -> impl Responder { +// HttpResponse::Ok() +// } + #[routes] #[get("/routes/test")] #[get("/routes/test2")] diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index 95265e2ae..1e6a953f5 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -1,10 +1,6 @@ # Changelog ## Unreleased - 2022-xx-xx -### Added -- Add `guard::Custom()` for handling `#[route]` macro with custom Methods. [#2969] - -[#2969]: https://github.com/actix/actix-web/pull/2969 ## 4.3.0 - 2023-01-21 ### Added diff --git a/actix-web/src/guard/mod.rs b/actix-web/src/guard/mod.rs index c2896f20c..164032bdc 100644 --- a/actix-web/src/guard/mod.rs +++ b/actix-web/src/guard/mod.rs @@ -342,21 +342,6 @@ method_guard!(Connect, CONNECT); method_guard!(Patch, PATCH); method_guard!(Trace, TRACE); -/// -/// # Examples -#[doc = "The route in this example will respond to all uppercase ASCII requests."] -/// ``` -/// use actix_web::{guard, web, HttpResponse}; -/// -/// web::route() -#[doc = " .guard(guard::Custom(\"HELLO\"))"] -/// .to(|| HttpResponse::Ok()); -/// ``` -#[allow(non_snake_case)] -pub fn Custom(custom_method: &str) -> impl Guard { - MethodGuard(HttpMethod::from_bytes(custom_method.as_bytes()).unwrap()) -} - /// Creates a guard that matches if request contains given header name and value. /// /// # Examples