Implement methods guards using the .or syntax

This commit is contained in:
Matt Gathu 2020-09-16 06:59:48 +02:00
parent 3527132c4d
commit 64a03398d0
No known key found for this signature in database
GPG Key ID: 7DAD4895E291CE56
3 changed files with 10 additions and 11 deletions

View File

@ -1,9 +1,6 @@
# Changes
## 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

View File

@ -261,11 +261,17 @@ impl ToTokens for Route {
resource_type,
} = self;
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! {
.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 {
quote! {
@ -282,7 +288,7 @@ impl ToTokens for Route {
#ast
let __resource = actix_web::Resource::new(#path)
.name(#resource_name)
#guard_gen
#method_guards
#(.guard(actix_web::guard::fn_guard(#guards)))*
#(.wrap(#wrappers))*
.#resource_type(#name);

View File

@ -104,10 +104,6 @@ pub fn Any<F: Guard + 'static>(guard: F) -> AnyGuard {
pub struct AnyGuard(Vec<Box<dyn Guard>>);
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
pub fn or<F: Guard + 'static>(mut self, guard: F) -> Self {
self.0.push(Box::new(guard));