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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//! Handler

use crate::{async_trait, Future};

mod after;
mod and_then;
mod around;
mod before;
mod boxed;
mod catch_error;
mod catch_unwind;
mod either;
mod fn_ext;
mod map;
mod map_err;
mod or_else;
mod responder;
mod transform;

pub use after::After;
pub use and_then::AndThen;
pub use around::{Around, Next};
pub use before::Before;
pub use boxed::BoxHandler;
pub use catch_error::CatchError;
pub use catch_unwind::CatchUnwind;
pub use either::Either;
pub use fn_ext::{FnExt, ResponderExt};
pub use map::Map;
pub use map_err::MapErr;
pub use or_else::OrElse;
pub use responder::Responder;
pub use transform::Transform;

/// Composable request handlers.
#[async_trait]
pub trait Handler<Args>: dyn_clone::DynClone + Send + Sync + 'static {
    type Output;

    #[must_use]
    async fn call(&self, args: Args) -> Self::Output;
}

impl<I, T> HandlerExt<I> for T where T: Handler<I> + ?Sized {}

#[async_trait]
impl<F, I, Fut, O> Handler<I> for F
where
    I: Send + 'static,
    F: Fn(I) -> Fut + ?Sized + Clone + Send + Sync + 'static,
    Fut: Future<Output = O> + Send,
{
    type Output = Fut::Output;

    async fn call(&self, args: I) -> Self::Output {
        (self)(args).await
    }
}

/// An extension trait for `Handler`s that provides a variety of convenient
/// combinator functions.
pub trait HandlerExt<I>: Handler<I> {
    fn boxed(self) -> BoxHandler<I, Self::Output>
    where
        Self: Sized,
    {
        Box::new(self)
    }

    fn before<F>(self, f: F) -> Before<Self, F>
    where
        Self: Sized,
    {
        Before::new(self, f)
    }

    fn after<F>(self, f: F) -> After<Self, F>
    where
        Self: Sized,
    {
        After::new(self, f)
    }

    fn around<F>(self, f: F) -> Around<Self, F>
    where
        Self: Sized,
    {
        Around::new(self, f)
    }

    fn map<F>(self, f: F) -> Map<Self, F>
    where
        Self: Sized,
    {
        Map::new(self, f)
    }

    fn and_then<F>(self, f: F) -> AndThen<Self, F>
    where
        Self: Sized,
    {
        AndThen::new(self, f)
    }

    fn map_err<F>(self, f: F) -> MapErr<Self, F>
    where
        Self: Sized,
    {
        MapErr::new(self, f)
    }

    fn or_else<F>(self, f: F) -> OrElse<Self, F>
    where
        Self: Sized,
    {
        OrElse::new(self, f)
    }

    fn catch_error<F, R, E>(self, f: F) -> CatchError<Self, F, R, E>
    where
        Self: Sized,
    {
        CatchError::new(self, f)
    }

    fn catch_unwind<F>(self, f: F) -> CatchUnwind<Self, F>
    where
        Self: Sized,
    {
        CatchUnwind::new(self, f)
    }

    fn with<T>(self, t: T) -> T::Output
    where
        T: Transform<Self>,
        Self: Sized,
    {
        t.transform(self)
    }

    fn with_fn<F>(self, f: F) -> Self
    where
        F: Fn(Self) -> Self,
        Self: Sized,
    {
        f(self)
    }
}