mirror of https://github.com/fafhrd91/actix-web
Update route attribute macro syntax
This commit is contained in:
parent
9539456656
commit
55ddb4877c
|
@ -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]
|
||||||
|
|
|
@ -100,40 +100,38 @@ 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),
|
"HEAD" => methods.push(GuardType::Head),
|
||||||
"HEAD" => methods.push(GuardType::Head),
|
"OPTIONS" => methods.push(GuardType::Options),
|
||||||
"OPTIONS" => methods.push(GuardType::Options),
|
"PATCH" => methods.push(GuardType::Patch),
|
||||||
"PATCH" => methods.push(GuardType::Patch),
|
"POST" => methods.push(GuardType::Post),
|
||||||
"POST" => methods.push(GuardType::Post),
|
"PUT" => methods.push(GuardType::Put),
|
||||||
"PUT" => methods.push(GuardType::Put),
|
"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: `{}`",
|
lit.value()
|
||||||
meth
|
),
|
||||||
),
|
))
|
||||||
))
|
}
|
||||||
}
|
};
|
||||||
};
|
|
||||||
}
|
|
||||||
} 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!",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue