1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//! A guard is called before the request is processed by the router and
//! can modify the request data or stops request processing by returning a
//! response immediately.

use crate::{
    body::Body,
    request::Request,
    responder::{DynResponder, Responder},
};
use futures::{future::BoxFuture, FutureExt};
use futures_util::future::Future;

/// Auto trait implementation over every function that match the definition of a
/// guard.
pub trait Guard {
    type Future: Future<Output = Result<Request<Body>, Self::Responder>> + Send;
    type Responder: Responder + Send;

    fn validate(&'static self, req: Request<Body>) -> Self::Future;
}

impl<Fun, Fut, Resp> Guard for Fun
where
    Resp: Responder + Send,
    Fun: Fn(Request<Body>) -> Fut,
    Fut: 'static + Future<Output = Result<Request<Body>, Resp>> + Send,
{
    type Future = BoxFuture<'static, Result<Request<Body>, Self::Responder>>;
    type Responder = Resp;

    #[inline]
    fn validate(&self, req: Request<Body>) -> Self::Future {
        (*self)(req).boxed()
    }
}

/// Builder to apply guards onto the handler
pub struct Builder<Chain: GuardChain> {
    chain: Chain,
}

impl Default for Builder<GuardChainEnd> {
    fn default() -> Self {
        Self { chain: GuardChainEnd }
    }
}

impl<Chain: GuardChain + 'static> Builder<Chain> {
    pub fn apply<Handler>(self, handler: Handler) -> Builder<GuardChainLink<Handler, Chain>>
    where
        Handler: 'static + Guard + Sync + Send,
    {
        Builder {
            chain: GuardChainLink { handler, rest: self.chain },
        }
    }

    pub(crate) fn build(self) -> Box<dyn GuardChain> {
        Box::new(self.chain)
    }
}

#[doc(hidden)]
pub trait GuardChain: Sync + Send {
    fn validate(&'static self, req: Request<Body>) -> BoxFuture<'static, Result<Request<Body>, Box<dyn DynResponder + Send>>>;

    /// to avoid useless heap allocation if there is only a guard end chain
    fn is_end(&self) -> bool;
}

#[doc(hidden)]
pub struct GuardChainEnd;

impl GuardChain for GuardChainEnd {
    #[inline]
    fn validate(&'static self, req: Request<Body>) -> BoxFuture<'static, Result<Request<Body>, Box<dyn DynResponder + Send>>> {
        async { Ok(req) }.boxed()
    }

    #[inline]
    fn is_end(&self) -> bool {
        true
    }
}

#[doc(hidden)]
pub struct GuardChainLink<Handler: Guard, Rest: GuardChain> {
    handler: Handler,
    rest: Rest,
}

impl<Handler, Rest> GuardChain for GuardChainLink<Handler, Rest>
where
    Handler: Guard + Sync + Send + 'static,
    Rest: GuardChain + 'static,
{
    #[inline]
    fn validate(&'static self, req: Request<Body>) -> BoxFuture<'static, Result<Request<Body>, Box<dyn DynResponder + Send>>> {
        async move {
            match self.handler.validate(req).await {
                Ok(req) => {
                    if self.rest.is_end() {
                        Ok(req)
                    } else {
                        self.rest.validate(req).await
                    }
                }
                Err(resp) => Err(Box::new(Some(resp)) as Box<dyn DynResponder + Send>),
            }
        }
        .boxed()
    }

    #[inline]
    fn is_end(&self) -> bool {
        false
    }
}