From 55ddb4877c6ddada92d71ced49d18d4d1c24ede9 Mon Sep 17 00:00:00 2001 From: Matt Gathu Date: Sun, 13 Sep 2020 10:29:37 +0200 Subject: [PATCH] Update route attribute macro syntax --- actix-web-codegen/src/lib.rs | 2 +- actix-web-codegen/src/route.rs | 50 +++++++++++++-------------- actix-web-codegen/tests/test_macro.rs | 2 +- 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/actix-web-codegen/src/lib.rs b/actix-web-codegen/src/lib.rs index 9b71f2752..0e3b7a467 100644 --- a/actix-web-codegen/src/lib.rs +++ b/actix-web-codegen/src/lib.rs @@ -147,7 +147,7 @@ pub fn patch(args: TokenStream, input: TokenStream) -> TokenStream { /// /// ## Attributes /// - `"path"` - Raw literal string with path for which to register handler. Mandatory. -/// - `methods="HTTP_METHOD_1,HTTP_METHOD_2"` - Registers HTTP methods to provide guards for. +/// - `method="HTTP_METHOD"` - Registers HTTP method to provide guard for. /// - `guard="function_name"` - Registers function as guard using `actix_web::guard::fn_guard` /// - `wrap="Middleware"` - Registers a resource middleware. #[proc_macro_attribute] diff --git a/actix-web-codegen/src/route.rs b/actix-web-codegen/src/route.rs index ef5c1ee3d..ae3227934 100644 --- a/actix-web-codegen/src/route.rs +++ b/actix-web-codegen/src/route.rs @@ -100,40 +100,38 @@ impl Args { "Attribute wrap expects type", )); } - } else if nv.path.is_ident("methods") { + } else if nv.path.is_ident("method") { if let syn::Lit::Str(ref lit) = nv.lit { - for meth in lit.value().split(',') { - match meth.to_uppercase().as_str() { - "CONNECT" => methods.push(GuardType::Connect), - "DELETE" => methods.push(GuardType::Delete), - "GET" => methods.push(GuardType::Get), - "HEAD" => methods.push(GuardType::Head), - "OPTIONS" => methods.push(GuardType::Options), - "PATCH" => methods.push(GuardType::Patch), - "POST" => methods.push(GuardType::Post), - "PUT" => methods.push(GuardType::Put), - "TRACE" => methods.push(GuardType::Trace), - _ => { - return Err(syn::Error::new_spanned( - nv.lit, - &format!( - "Unexpected HTTP Method: `{}`", - meth - ), - )) - } - }; - } + match lit.value().to_uppercase().as_str() { + "CONNECT" => methods.push(GuardType::Connect), + "DELETE" => methods.push(GuardType::Delete), + "GET" => methods.push(GuardType::Get), + "HEAD" => methods.push(GuardType::Head), + "OPTIONS" => methods.push(GuardType::Options), + "PATCH" => methods.push(GuardType::Patch), + "POST" => methods.push(GuardType::Post), + "PUT" => methods.push(GuardType::Put), + "TRACE" => methods.push(GuardType::Trace), + _ => { + return Err(syn::Error::new_spanned( + &nv.lit, + &format!( + "Unexpected HTTP Method: `{}`", + lit.value() + ), + )) + } + }; } else { return Err(syn::Error::new_spanned( nv.lit, - "Attribute methods expects literal string!", + "Attribute method expects literal string!", )); } } else { return Err(syn::Error::new_spanned( nv.path, - "Unknown attribute key is specified. Allowed: guard and wrap", + "Unknown attribute key is specified. Allowed: guard, method and wrap", )); } } @@ -204,7 +202,7 @@ impl Route { if guard == GuardType::Multi && args.methods.is_empty() { return Err(syn::Error::new( Span::call_site(), - "The #[route(..)] macro requires the `methods` attribute!", + "The #[route(..)] macro requires at least one `method` attribute!", )); } diff --git a/actix-web-codegen/tests/test_macro.rs b/actix-web-codegen/tests/test_macro.rs index 476c082d5..dd2bccd7f 100644 --- a/actix-web-codegen/tests/test_macro.rs +++ b/actix-web-codegen/tests/test_macro.rs @@ -81,7 +81,7 @@ async fn get_param_test(_: Path) -> impl Responder { HttpResponse::Ok() } -#[route("/multi", methods = "GET,HEAD,POST")] +#[route("/multi", method = "GET", method = "POST", method = "HEAD")] async fn route_test() -> impl Responder { HttpResponse::Ok() }