Update route attribute macro syntax

This commit is contained in:
Matt Gathu 2020-09-13 10:29:37 +02:00
parent 9539456656
commit 55ddb4877c
3 changed files with 26 additions and 28 deletions

View File

@ -147,7 +147,7 @@ pub fn patch(args: TokenStream, input: TokenStream) -> TokenStream {
/// ///
/// ## Attributes /// ## Attributes
/// - `"path"` - Raw literal string with path for which to register handler. Mandatory. /// - `"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` /// - `guard="function_name"` - Registers function as guard using `actix_web::guard::fn_guard`
/// - `wrap="Middleware"` - Registers a resource middleware. /// - `wrap="Middleware"` - Registers a resource middleware.
#[proc_macro_attribute] #[proc_macro_attribute]

View File

@ -100,10 +100,9 @@ impl Args {
"Attribute wrap expects type", "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 { if let syn::Lit::Str(ref lit) = nv.lit {
for meth in lit.value().split(',') { match lit.value().to_uppercase().as_str() {
match meth.to_uppercase().as_str() {
"CONNECT" => methods.push(GuardType::Connect), "CONNECT" => methods.push(GuardType::Connect),
"DELETE" => methods.push(GuardType::Delete), "DELETE" => methods.push(GuardType::Delete),
"GET" => methods.push(GuardType::Get), "GET" => methods.push(GuardType::Get),
@ -115,25 +114,24 @@ impl Args {
"TRACE" => methods.push(GuardType::Trace), "TRACE" => methods.push(GuardType::Trace),
_ => { _ => {
return Err(syn::Error::new_spanned( return Err(syn::Error::new_spanned(
nv.lit, &nv.lit,
&format!( &format!(
"Unexpected HTTP Method: `{}`", "Unexpected HTTP Method: `{}`",
meth lit.value()
), ),
)) ))
} }
}; };
}
} else { } else {
return Err(syn::Error::new_spanned( return Err(syn::Error::new_spanned(
nv.lit, nv.lit,
"Attribute methods expects literal string!", "Attribute method expects literal string!",
)); ));
} }
} else { } else {
return Err(syn::Error::new_spanned( return Err(syn::Error::new_spanned(
nv.path, 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() { if guard == GuardType::Multi && args.methods.is_empty() {
return Err(syn::Error::new( return Err(syn::Error::new(
Span::call_site(), Span::call_site(),
"The #[route(..)] macro requires the `methods` attribute!", "The #[route(..)] macro requires at least one `method` attribute!",
)); ));
} }

View File

@ -81,7 +81,7 @@ async fn get_param_test(_: Path<String>) -> impl Responder {
HttpResponse::Ok() HttpResponse::Ok()
} }
#[route("/multi", methods = "GET,HEAD,POST")] #[route("/multi", method = "GET", method = "POST", method = "HEAD")]
async fn route_test() -> impl Responder { async fn route_test() -> impl Responder {
HttpResponse::Ok() HttpResponse::Ok()
} }