use existing Method guard instead of creating a new Custom gueard

This commit is contained in:
edgerunnergit 2023-02-04 09:25:39 +05:30
parent 7115c29a55
commit 28d928a660
4 changed files with 8 additions and 27 deletions

View File

@ -29,7 +29,7 @@ macro_rules! method_type {
$(stringify!($upper) => Ok(Self::$variant),)+
_ => {
if method.chars().all(|c| c.is_ascii_uppercase()) {
Ok(Self::Custom)
Ok(Self::Method)
} else {
Err(format!("HTTP method must be uppercase: `{}`", method))
}
@ -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 => {

View File

@ -91,7 +91,7 @@ async fn get_param_test(_: web::Path<String>) -> impl Responder {
method = "GET",
method = "POST",
method = "HEAD",
method = "CREATE"
method = "HELLO"
)]
async fn route_test() -> impl Responder {
HttpResponse::Ok()

View File

@ -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

View File

@ -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