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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use std::{convert::Infallible, marker::PhantomData};

use hyper::body::Incoming;
use motore::{layer::Layer, service::Service};

use crate::{
    handler::{MiddlewareHandlerFromFn, MiddlewareHandlerMapResponse},
    response::{IntoResponse, Response},
    DynService, HttpContext,
};

pub struct FromFnLayer<F, S, T> {
    f: F,
    state: S,
    _marker: PhantomData<fn(T)>,
}

impl<F, S, T> Clone for FromFnLayer<F, S, T>
where
    F: Clone,
    S: Clone,
{
    fn clone(&self) -> Self {
        Self {
            f: self.f.clone(),
            state: self.state.clone(),
            _marker: PhantomData,
        }
    }
}

pub fn from_fn<F, T>(f: F) -> FromFnLayer<F, (), T> {
    from_fn_with_state(f, ())
}

fn from_fn_with_state<F, S, T>(f: F, state: S) -> FromFnLayer<F, S, T> {
    FromFnLayer {
        f,
        state,
        _marker: PhantomData,
    }
}

impl<I, F, S, T> Layer<I> for FromFnLayer<F, S, T>
where
    F: Clone,
    S: Clone,
{
    type Service = FromFn<I, F, S, T>;

    fn layer(self, inner: I) -> Self::Service {
        FromFn {
            inner,
            f: self.f.clone(),
            state: self.state.clone(),
            _marker: self._marker,
        }
    }
}

pub struct FromFn<I, F, S, T> {
    inner: I,
    f: F,
    state: S,
    _marker: PhantomData<fn(T)>,
}

impl<I, F, S, T> Clone for FromFn<I, F, S, T>
where
    I: Clone,
    F: Clone,
    S: Clone,
{
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
            f: self.f.clone(),
            state: self.state.clone(),
            _marker: PhantomData,
        }
    }
}

impl<I, F, S, T> Service<HttpContext, Incoming> for FromFn<I, F, S, T>
where
    I: Service<HttpContext, Incoming, Response = Response, Error = Infallible>
        + Clone
        + Send
        + Sync
        + 'static,
    F: for<'r> MiddlewareHandlerFromFn<'r, T, S> + Clone + Sync,
    S: Clone + Sync,
{
    type Response = I::Response;
    type Error = I::Error;

    async fn call<'s, 'cx>(
        &'s self,
        cx: &'cx mut HttpContext,
        req: Incoming,
    ) -> Result<Self::Response, Self::Error> {
        let next = Next {
            inner: DynService::new(self.inner.clone()),
        };
        Ok(
            self.f.call(cx, req, &self.state, next).await, // .into_response()
        )
    }
}

pub struct Next {
    inner: DynService,
}

impl Next {
    pub async fn run(self, cx: &mut HttpContext, req: Incoming) -> Result<Response, Infallible> {
        self.inner.call(cx, req).await
    }
}

pub struct MapResponseLayer<F, S, T> {
    f: F,
    state: S,
    _marker: PhantomData<fn(T)>,
}

impl<F, S, T> Clone for MapResponseLayer<F, S, T>
where
    F: Clone,
    S: Clone,
{
    fn clone(&self) -> Self {
        Self {
            f: self.f.clone(),
            state: self.state.clone(),
            _marker: PhantomData,
        }
    }
}

pub fn map_response<F, T>(f: F) -> MapResponseLayer<F, (), T> {
    map_response_with_state(f, ())
}

fn map_response_with_state<F, S, T>(f: F, state: S) -> MapResponseLayer<F, S, T> {
    MapResponseLayer {
        f,
        state,
        _marker: PhantomData,
    }
}

impl<I, F, S, T> Layer<I> for MapResponseLayer<F, S, T>
where
    F: Clone,
    S: Clone,
{
    type Service = MapResponse<I, F, S, T>;

    fn layer(self, inner: I) -> Self::Service {
        MapResponse {
            inner,
            f: self.f.clone(),
            state: self.state.clone(),
            _marker: self._marker,
        }
    }
}

pub struct MapResponse<I, F, S, T> {
    inner: I,
    f: F,
    state: S,
    _marker: PhantomData<fn(T)>,
}

impl<I, F, S, T> Clone for MapResponse<I, F, S, T>
where
    I: Clone,
    F: Clone,
    S: Clone,
{
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
            f: self.f.clone(),
            state: self.state.clone(),
            _marker: PhantomData,
        }
    }
}

impl<I, F, S, T> Service<HttpContext, Incoming> for MapResponse<I, F, S, T>
where
    I: Service<HttpContext, Incoming, Response = Response, Error = Infallible>
        + Clone
        + Send
        + Sync
        + 'static,
    F: for<'r> MiddlewareHandlerMapResponse<'r, T, S> + Clone + Sync,
    S: Clone + Sync,
{
    type Response = I::Response;
    type Error = I::Error;

    async fn call<'s, 'cx>(
        &'s self,
        cx: &'cx mut HttpContext,
        req: Incoming,
    ) -> Result<Self::Response, Self::Error> {
        let response = match self.inner.call(cx, req).await {
            Ok(resp) => resp,
            Err(e) => e.into_response(),
        };

        Ok(self.f.call(cx, &self.state, response).await)
    }
}