mirror of https://github.com/fafhrd91/actix-web
Implement methods guards using the .or syntax
This commit is contained in:
parent
3527132c4d
commit
64a03398d0
|
@ -1,9 +1,6 @@
|
||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## Unreleased - 2020-xx-xx
|
## Unreleased - 2020-xx-xx
|
||||||
* Add public constructor for `web::guard::AnyGuard`
|
|
||||||
|
|
||||||
[#1674]: https://github.com/actix/actix-web/pull/1674
|
|
||||||
|
|
||||||
|
|
||||||
## 3.0.2 - 2020-09-15
|
## 3.0.2 - 2020-09-15
|
||||||
|
|
|
@ -261,11 +261,17 @@ impl ToTokens for Route {
|
||||||
resource_type,
|
resource_type,
|
||||||
} = self;
|
} = self;
|
||||||
let resource_name = name.to_string();
|
let resource_name = name.to_string();
|
||||||
let methods = methods.iter();
|
let mut methods = methods.iter();
|
||||||
|
|
||||||
|
let method_guards = if *guard == GuardType::Multi {
|
||||||
|
// unwrapping since length is checked to be at least one
|
||||||
|
let first = methods.next().unwrap();
|
||||||
|
|
||||||
let guard_gen = if *guard == GuardType::Multi {
|
|
||||||
quote! {
|
quote! {
|
||||||
.guard(actix_web::guard::AnyGuard::new(vec![#(Box::new(actix_web::guard::#methods())),*]))
|
.guard(
|
||||||
|
actix_web::guard::Any(actix_web::guard::#first())
|
||||||
|
#(.or(actix_web::guard::#methods()))*
|
||||||
|
)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
quote! {
|
quote! {
|
||||||
|
@ -282,7 +288,7 @@ impl ToTokens for Route {
|
||||||
#ast
|
#ast
|
||||||
let __resource = actix_web::Resource::new(#path)
|
let __resource = actix_web::Resource::new(#path)
|
||||||
.name(#resource_name)
|
.name(#resource_name)
|
||||||
#guard_gen
|
#method_guards
|
||||||
#(.guard(actix_web::guard::fn_guard(#guards)))*
|
#(.guard(actix_web::guard::fn_guard(#guards)))*
|
||||||
#(.wrap(#wrappers))*
|
#(.wrap(#wrappers))*
|
||||||
.#resource_type(#name);
|
.#resource_type(#name);
|
||||||
|
|
|
@ -104,10 +104,6 @@ pub fn Any<F: Guard + 'static>(guard: F) -> AnyGuard {
|
||||||
pub struct AnyGuard(Vec<Box<dyn Guard>>);
|
pub struct AnyGuard(Vec<Box<dyn Guard>>);
|
||||||
|
|
||||||
impl AnyGuard {
|
impl AnyGuard {
|
||||||
/// Create AnyGuard from a vector of Guards
|
|
||||||
pub fn new(guards: Vec<Box<dyn Guard>>) -> Self {
|
|
||||||
AnyGuard(guards)
|
|
||||||
}
|
|
||||||
/// Add guard to a list of guards to check
|
/// Add guard to a list of guards to check
|
||||||
pub fn or<F: Guard + 'static>(mut self, guard: F) -> Self {
|
pub fn or<F: Guard + 'static>(mut self, guard: F) -> Self {
|
||||||
self.0.push(Box::new(guard));
|
self.0.push(Box::new(guard));
|
||||||
|
|
Loading…
Reference in New Issue