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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//! type eraser middleware.

use core::marker::PhantomData;

use crate::service::Service;

#[doc(hidden)]
mod marker {
    pub struct EraseReqBody;
    pub struct EraseResBody;

    pub struct EraseErr;
}

use marker::*;

/// Type eraser middleware is for erasing "unwanted" complex types produced by service tree
/// and expose well known concrete types `xitca-web` can handle.
///
/// # Example
/// ```rust
/// # use xitca_web::{
/// #   handler::handler_service,
/// #   middleware::{eraser::TypeEraser, limit::Limit, Group},
/// #   service::ServiceExt,
/// #   App, WebContext
/// #   };
/// // a handler function expect xitca_web::body::RequestBody as body type.
/// async fn handler(_: &WebContext<'_>) -> &'static str {
///     "hello,world!"
/// }
///
/// // a limit middleware that limit request body to max size of 1MB.
/// // this middleware would produce a new type of request body that
/// // handler function don't know of.
/// let limit = Limit::new().set_request_body_max_size(1024 * 1024);
///
/// // an eraser middleware that transform any request body to xitca_web::body::RequestBody.
/// let eraser = TypeEraser::request_body();
///
/// App::new()
///     .at("/", handler_service(handler))
///     // introduce eraser middleware between handler and limit middleware
///     // to resolve the type difference between them.
///     // without it this piece of code would simply refuse to compile.
///     .enclosed(eraser.clone())
///     .enclosed(limit.clone());
///
/// // group middleware is suggested way of handling of use case like this.
/// let group = Group::new()
///     .enclosed(eraser)
///     .enclosed(limit);
///
/// // it's also suggested to group multiple type mutation middlewares together and apply
/// // eraser on them once if possible. the reason being TypeErase has a cost and by
/// // grouping you only pay the cost once.
///
/// App::new()
///     .at("/", handler_service(handler))
///     .enclosed(group);
/// ```
pub struct TypeEraser<M>(PhantomData<M>);

impl<M> Clone for TypeEraser<M> {
    fn clone(&self) -> Self {
        Self(PhantomData)
    }
}

impl TypeEraser<EraseReqBody> {
    /// Erase generic request body type. making downstream middlewares observe [`RequestBody`].
    ///
    /// [`RequestBody`]: crate::body::RequestBody
    pub const fn request_body() -> Self {
        TypeEraser(PhantomData)
    }
}

impl TypeEraser<EraseResBody> {
    /// Erase generic response body type. making downstream middlewares observe [`ResponseBody`].
    ///
    /// [`ResponseBody`]: crate::body::ResponseBody
    pub const fn response_body() -> Self {
        TypeEraser(PhantomData)
    }
}

impl TypeEraser<EraseErr> {
    /// Erase generic E type from Service<Error = E>. making downstream middlewares observe [`Error`].
    ///
    /// [`Error`]: crate::error::Error
    pub const fn error() -> Self {
        TypeEraser(PhantomData)
    }
}

impl<M, S, E> Service<Result<S, E>> for TypeEraser<M> {
    type Response = service::EraserService<M, S>;
    type Error = E;

    async fn call(&self, res: Result<S, E>) -> Result<Self::Response, Self::Error> {
        res.map(|service| service::EraserService {
            service,
            _erase: PhantomData,
        })
    }
}

mod service {
    use core::cell::RefCell;

    use crate::{
        body::{BodyStream, BoxBody},
        body::{RequestBody, ResponseBody},
        bytes::Bytes,
        error::Error,
        http::WebResponse,
        service::ready::ReadyService,
        WebContext,
    };

    use super::*;

    pub struct EraserService<M, S> {
        pub(super) service: S,
        pub(super) _erase: PhantomData<M>,
    }

    impl<'r, S, C, ReqB, ResB, Err> Service<WebContext<'r, C, ReqB>> for EraserService<EraseReqBody, S>
    where
        S: for<'rs> Service<WebContext<'rs, C>, Response = WebResponse<ResB>, Error = Err>,
        ReqB: BodyStream<Chunk = Bytes> + Default + 'static,
        ResB: BodyStream<Chunk = Bytes> + 'static,
    {
        type Response = WebResponse;
        type Error = Err;

        async fn call(&self, mut ctx: WebContext<'r, C, ReqB>) -> Result<Self::Response, Self::Error> {
            let body = ctx.take_body_mut();
            let mut body = RefCell::new(RequestBody::Unknown(BoxBody::new(body)));
            let WebContext { req, ctx, .. } = ctx;
            let res = self.service.call(WebContext::new(req, &mut body, ctx)).await?;
            Ok(res.map(ResponseBody::box_stream))
        }
    }

    impl<S, Req, ResB> Service<Req> for EraserService<EraseResBody, S>
    where
        S: Service<Req, Response = WebResponse<ResB>>,
        ResB: BodyStream<Chunk = Bytes> + 'static,
    {
        type Response = WebResponse;
        type Error = S::Error;

        #[inline]
        async fn call(&self, req: Req) -> Result<Self::Response, Self::Error> {
            let res = self.service.call(req).await?;
            Ok(res.map(ResponseBody::box_stream))
        }
    }

    impl<'r, C, B, S> Service<WebContext<'r, C, B>> for EraserService<EraseErr, S>
    where
        S: Service<WebContext<'r, C, B>>,
        S::Error: Into<Error<C>>,
    {
        type Response = S::Response;
        type Error = Error<C>;

        #[inline]
        async fn call(&self, ctx: WebContext<'r, C, B>) -> Result<Self::Response, Self::Error> {
            self.service.call(ctx).await.map_err(Into::into)
        }
    }

    impl<M, S> ReadyService for EraserService<M, S>
    where
        S: ReadyService,
    {
        type Ready = S::Ready;

        #[inline]
        async fn ready(&self) -> Self::Ready {
            self.service.ready().await
        }
    }
}

#[cfg(test)]
mod test {
    use xitca_http::body::Once;
    use xitca_unsafe_collection::futures::NowOrPanic;

    use crate::{
        bytes::Bytes,
        error::Error,
        handler::handler_service,
        http::{Request, StatusCode, WebResponse},
        middleware::Group,
        service::ServiceExt,
        App, WebContext,
    };

    use super::*;

    async fn handler(_: &WebContext<'_>) -> &'static str {
        "996"
    }

    async fn map_body<S, C, B, Err>(_: &S, _: WebContext<'_, C, B>) -> Result<WebResponse<Once<Bytes>>, Err>
    where
        S: for<'r> Service<WebContext<'r, C, B>, Response = WebResponse, Error = Err>,
    {
        Ok(WebResponse::new(Once::new(Bytes::new())))
    }

    async fn middleware_fn<S, C, B, Err>(s: &S, ctx: WebContext<'_, C, B>) -> Result<WebResponse, Err>
    where
        S: for<'r> Service<WebContext<'r, C, B>, Response = WebResponse, Error = Err>,
    {
        s.call(ctx).await
    }

    #[test]
    fn erase_body() {
        let _ = App::new()
            // map WebResponse to WebResponse<Once<Bytes>> type.
            .at("/", handler_service(handler).enclosed_fn(map_body))
            // erase the body type to make it WebResponse type again.
            .enclosed(TypeEraser::response_body())
            // observe erased body type.
            .enclosed_fn(middleware_fn)
            .finish()
            .call(())
            .now_or_panic()
            .unwrap()
            .call(Request::default())
            .now_or_panic()
            .unwrap();
    }

    #[test]
    fn erase_error() {
        async fn middleware_fn<S, C, B, Err>(s: &S, ctx: WebContext<'_, C, B>) -> Result<WebResponse, StatusCode>
        where
            S: for<'r> Service<WebContext<'r, C, B>, Response = WebResponse, Error = Err>,
        {
            s.call(ctx).await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
        }

        async fn middleware_fn2<S, C, B>(s: &S, ctx: WebContext<'_, C, B>) -> Result<WebResponse, Error<C>>
        where
            S: for<'r> Service<WebContext<'r, C, B>, Response = WebResponse, Error = Error<C>>,
        {
            s.call(ctx).await
        }

        let _ = App::new()
            // map WebResponse to WebResponse<Once<Bytes>> type.
            .at("/", handler_service(handler).enclosed(TypeEraser::error()))
            .enclosed(
                Group::new()
                    .enclosed_fn(middleware_fn)
                    .enclosed(TypeEraser::error())
                    .enclosed_fn(middleware_fn2),
            )
            .finish()
            .call(())
            .now_or_panic()
            .unwrap()
            .call(Request::default())
            .now_or_panic()
            .unwrap();
    }
}