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
276
277
278
279
280
281
282
283
284
285
286
287
//! type extractor and response generator for json

use core::{
    convert::Infallible,
    fmt,
    marker::PhantomData,
    ops::{Deref, DerefMut},
};

use serde::{de::Deserialize, ser::Serialize};
use xitca_http::util::service::router::{PathGen, RouteGen, RouterMapErr};

use crate::{
    body::BodyStream,
    bytes::{BufMutWriter, Bytes, BytesMut},
    context::WebContext,
    error::{error_from_service, forward_blank_bad_request, Error},
    handler::{FromRequest, Responder},
    http::{const_header_value::JSON, header::CONTENT_TYPE, WebResponse},
    service::Service,
};

use super::{
    body::Limit,
    header::{self, HeaderRef},
};

pub const DEFAULT_LIMIT: usize = 1024 * 1024;

/// Extract type for Json object. const generic param LIMIT is for max size of the object in bytes.
/// Object larger than limit would be treated as error.
///
/// Default limit is [DEFAULT_LIMIT] in bytes.
#[derive(Clone)]
pub struct Json<T, const LIMIT: usize = DEFAULT_LIMIT>(pub T);

impl<T, const LIMIT: usize> fmt::Debug for Json<T, LIMIT>
where
    T: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Json")
            .field("value", &self.0)
            .field("limit", &LIMIT)
            .finish()
    }
}

impl<T, const LIMIT: usize> Deref for Json<T, LIMIT> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T, const LIMIT: usize> DerefMut for Json<T, LIMIT> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<'a, 'r, C, B, T, const LIMIT: usize> FromRequest<'a, WebContext<'r, C, B>> for Json<T, LIMIT>
where
    B: BodyStream + Default,
    T: for<'de> Deserialize<'de>,
{
    type Type<'b> = Json<T, LIMIT>;
    type Error = Error<C>;

    async fn from_request(ctx: &'a WebContext<'r, C, B>) -> Result<Self, Self::Error> {
        HeaderRef::<'a, { header::CONTENT_TYPE }>::from_request(ctx).await?;
        let (bytes, _) = <(BytesMut, Limit<LIMIT>)>::from_request(ctx).await?;
        serde_json::from_slice(&bytes).map(Json).map_err(Into::into)
    }
}

/// lazy deserialize type that wrap around [Json]. It lowers the deserialization to handler
/// function where zero copy deserialize can happen.
///
/// # Example
/// ```rust
/// # use serde::Deserialize;
/// # use xitca_web::{
/// #   error::Error,
/// #   http::StatusCode,
/// #   handler::{handler_service, json::LazyJson},
/// #   App, WebContext
/// # };
/// // a json object with zero copy deserialization.
/// #[derive(Deserialize)]
/// struct Post<'a> {
///     title: &'a str,
///     content: &'a str    
/// }
///
/// // handler function utilize Lazy type to lower the Json type into handler function.
/// async fn handler(lazy: LazyJson<Post<'_>>) -> Result<String, Error> {
///     // actual deserialize happens here.
///     let Post { title, content } = lazy.deserialize()?;
///     // the Post type and it's &str referencing LazyJson would live until the handler
///     // function return.
///     Ok(format!("Post {{ title: {title}, content: {content} }}"))
/// }
///
/// App::new()
///     .at("/post", handler_service(handler))
///     # .at("/", handler_service(|_: &WebContext<'_>| async { "used for infer type" }));
/// ```
pub struct LazyJson<T, const LIMIT: usize = DEFAULT_LIMIT> {
    bytes: Vec<u8>,
    _json: PhantomData<T>,
}

impl<T, const LIMIT: usize> LazyJson<T, LIMIT> {
    pub fn deserialize<'de, C>(&'de self) -> Result<T, Error<C>>
    where
        T: Deserialize<'de>,
    {
        serde_json::from_slice(&self.bytes).map_err(Into::into)
    }
}

impl<'a, 'r, C, B, T, const LIMIT: usize> FromRequest<'a, WebContext<'r, C, B>> for LazyJson<T, LIMIT>
where
    B: BodyStream + Default,
    T: Deserialize<'static>,
{
    type Type<'b> = LazyJson<T, LIMIT>;
    type Error = Error<C>;

    async fn from_request(ctx: &'a WebContext<'r, C, B>) -> Result<Self, Self::Error> {
        HeaderRef::<'a, { header::CONTENT_TYPE }>::from_request(ctx).await?;
        let (bytes, _) = <(Vec<u8>, Limit<LIMIT>)>::from_request(ctx).await?;
        Ok(LazyJson {
            bytes,
            _json: PhantomData,
        })
    }
}

impl<'r, C, B, T> Responder<WebContext<'r, C, B>> for Json<T>
where
    T: Serialize,
{
    type Response = WebResponse;
    type Error = Error<C>;

    #[inline]
    async fn respond(self, ctx: WebContext<'r, C, B>) -> Result<Self::Response, Self::Error> {
        self._respond(|bytes| ctx.into_response(bytes))
    }

    #[inline]
    fn map(self, res: Self::Response) -> Result<Self::Response, Self::Error> {
        self._respond(|bytes| res.map(|_| bytes.into()))
    }
}

impl<T> Json<T> {
    fn _respond<F, C>(self, func: F) -> Result<WebResponse, Error<C>>
    where
        T: Serialize,
        F: FnOnce(Bytes) -> WebResponse,
    {
        let mut bytes = BytesMut::new();
        serde_json::to_writer(BufMutWriter(&mut bytes), &self.0)?;
        let mut res = func(bytes.freeze());
        res.headers_mut().insert(CONTENT_TYPE, JSON);
        Ok(res)
    }
}

impl<'r, C, B> Responder<WebContext<'r, C, B>> for serde_json::Value {
    type Response = WebResponse;
    type Error = Error<C>;

    #[inline]
    async fn respond(self, ctx: WebContext<'r, C, B>) -> Result<Self::Response, Self::Error> {
        Json(self).respond(ctx).await
    }

    #[inline]
    fn map(self, res: Self::Response) -> Result<Self::Response, Self::Error> {
        Responder::<WebContext<'r, C, B>>::map(Json(self), res)
    }
}

error_from_service!(serde_json::Error);
forward_blank_bad_request!(serde_json::Error);

impl<T> PathGen for Json<T> {}

impl<T> RouteGen for Json<T> {
    type Route<R> = RouterMapErr<R>;

    fn route_gen<R>(route: R) -> Self::Route<R> {
        RouterMapErr(route)
    }
}

impl<T> Service for Json<T>
where
    T: Clone,
{
    type Response = Self;
    type Error = Infallible;

    async fn call(&self, _: ()) -> Result<Self::Response, Self::Error> {
        Ok(self.clone())
    }
}

impl<'r, C, B, T> Service<WebContext<'r, C, B>> for Json<T>
where
    T: Serialize + Clone,
{
    type Response = WebResponse;
    type Error = Error<C>;

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

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

    use crate::{
        handler::handler_service,
        http::{header::CONTENT_LENGTH, WebRequest},
        test::collect_string_body,
        App,
    };

    use super::*;

    #[derive(serde::Deserialize, serde::Serialize, Clone)]
    struct Gacha<'a> {
        credit_card: &'a str,
    }

    #[test]
    fn extract_lazy() {
        let mut ctx = WebContext::new_test(&());
        let mut ctx = ctx.as_web_ctx();

        let body = serde_json::to_string(&Gacha {
            credit_card: "declined",
        })
        .unwrap();

        ctx.req_mut().headers_mut().insert(CONTENT_TYPE, JSON);
        ctx.req_mut().headers_mut().insert(CONTENT_LENGTH, body.len().into());

        *ctx.body_borrow_mut() = body.into();

        async fn handler(lazy: LazyJson<Gacha<'_>>) -> &'static str {
            let ga = lazy.deserialize::<()>().unwrap();
            assert_eq!(ga.credit_card, "declined");
            "bankruptcy"
        }

        let service = handler_service(handler).call(()).now_or_panic().unwrap();

        let body = service.call(ctx).now_or_panic().unwrap().into_body();
        let res = collect_string_body(body).now_or_panic().unwrap();

        assert_eq!(res, "bankruptcy");
    }

    #[test]
    fn service() {
        let res = App::new()
            .at("/", Json(Gacha { credit_card: "mom" }))
            .finish()
            .call(())
            .now_or_panic()
            .unwrap()
            .call(WebRequest::default())
            .now_or_panic()
            .unwrap();
        assert_eq!(res.status().as_u16(), 200);
    }
}