From 88e5059910bfc54be07b52043c2af97a1fa9790d Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Thu, 7 Mar 2019 15:37:39 -0800 Subject: [PATCH] add doc string to guards --- src/guard.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/guard.rs b/src/guard.rs index 1632b9975..f9565d0fb 100644 --- a/src/guard.rs +++ b/src/guard.rs @@ -1,4 +1,30 @@ //! Route match guards. +//! +//! Guards are one of the way how actix-web router chooses +//! handler service. In essence it just function that accepts +//! reference to a `RequestHead` instance and returns boolean. +//! It is possible to add guards to *scopes*, *resources* +//! and *routes*. Actix provide several guards by default, like various +//! http methods, header, etc. To become a guard, type must implement `Guard` +//! trait. Simple functions coulds guards as well. +//! +//! Guard can not modify request object. But it is possible to +//! to store extra attributes on a request by using `Extensions` container. +//! Extensions container available via `RequestHead::extensions()` method. +//! +//! ```rust +//! use actix_web::{web, http, dev, guard, App, HttpResponse}; +//! +//! fn main() { +//! App::new().service(web::resource("/index.html").route( +//! web::route() +//! .guard(guard::Post()) +//! .guard(|head: &dev::RequestHead| head.method == http::Method::GET) +//! .to(|| HttpResponse::MethodNotAllowed())) +//! ); +//! } +//! ``` + #![allow(non_snake_case)] use actix_http::http::{self, header, HttpTryFrom}; use actix_http::RequestHead; @@ -13,6 +39,18 @@ pub trait Guard { fn check(&self, request: &RequestHead) -> bool; } +#[doc(hidden)] +pub struct FnGuard bool + 'static>(F); + +impl Guard for F +where + F: Fn(&RequestHead) -> bool + 'static, +{ + fn check(&self, head: &RequestHead) -> bool { + (*self)(head) + } +} + /// Return guard that matches if any of supplied guards. /// /// ```rust