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
/// - `"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]

View File

@ -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!",
));
}

View File

@ -81,7 +81,7 @@ async fn get_param_test(_: Path<String>) -> 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()
}