xitca_web/handler/types/
query.rs

1//! type extractor for request uri query
2
3use core::{fmt, marker::PhantomData};
4
5use serde::de::Deserialize;
6
7use crate::{
8    context::WebContext,
9    error::{Error, ErrorStatus},
10    handler::FromRequest,
11};
12
13pub struct Query<T>(pub T);
14
15impl<T> fmt::Debug for Query<T>
16where
17    T: fmt::Debug,
18{
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        f.debug_struct("Query").field("value", &self.0).finish()
21    }
22}
23
24impl<'a, 'r, C, B, T> FromRequest<'a, WebContext<'r, C, B>> for Query<T>
25where
26    T: for<'de> Deserialize<'de>,
27{
28    type Type<'b> = Query<T>;
29    type Error = Error;
30
31    #[inline]
32    async fn from_request(ctx: &'a WebContext<'r, C, B>) -> Result<Self, Self::Error> {
33        serde_urlencoded::from_str(ctx.req().uri().query().unwrap_or_default())
34            .map(Query)
35            .map_err(Error::from_service)
36    }
37}
38
39/// lazy deserialize type.
40/// it lowers the deserialization to handler function where zero copy deserialize can happen.
41pub struct LazyQuery<'a, T> {
42    query: &'a [u8],
43    _query: PhantomData<T>,
44}
45
46impl<T> LazyQuery<'_, T> {
47    pub fn deserialize<'de>(&'de self) -> Result<T, Error>
48    where
49        T: Deserialize<'de>,
50    {
51        serde_urlencoded::from_bytes(self.query).map_err(Into::into)
52    }
53}
54
55impl<'a, 'r, C, B, T> FromRequest<'a, WebContext<'r, C, B>> for LazyQuery<'a, T>
56where
57    T: Deserialize<'static>,
58{
59    type Type<'b> = LazyQuery<'b, T>;
60    type Error = Error;
61
62    #[inline]
63    async fn from_request(ctx: &'a WebContext<'r, C, B>) -> Result<Self, Self::Error> {
64        let query = ctx.req().uri().query().ok_or(ErrorStatus::bad_request())?;
65        Ok(LazyQuery {
66            query: query.as_bytes(),
67            _query: PhantomData,
68        })
69    }
70}
71
72#[cfg(test)]
73mod test {
74    use xitca_unsafe_collection::futures::NowOrPanic;
75
76    use crate::{handler::handler_service, http::Uri, service::Service, test::collect_string_body};
77
78    use super::*;
79
80    #[derive(serde::Deserialize)]
81    struct Id {
82        id: String,
83    }
84
85    #[derive(serde::Deserialize)]
86    struct Id2<'a> {
87        id: &'a str,
88    }
89
90    #[test]
91    fn query() {
92        let mut req = WebContext::new_test(());
93        let mut req = req.as_web_ctx();
94
95        *req.req_mut().uri_mut() = Uri::from_static("/996/251/?id=dagongren");
96
97        let Query(id) = Query::<Id>::from_request(&req).now_or_panic().unwrap();
98        assert_eq!(id.id, "dagongren");
99    }
100
101    #[test]
102    fn query_lazy() {
103        let mut ctx = WebContext::new_test(());
104        let mut ctx = ctx.as_web_ctx();
105
106        *ctx.req_mut().uri_mut() = Uri::from_static("/996/251/?id=dagongren");
107
108        async fn handler(lazy: LazyQuery<'_, Id2<'_>>) -> &'static str {
109            let id = lazy.deserialize().unwrap();
110            assert_eq!(id.id, "dagongren");
111            "kubi"
112        }
113
114        let service = handler_service(handler).call(()).now_or_panic().unwrap();
115
116        let body = service.call(ctx).now_or_panic().unwrap().into_body();
117        let res = collect_string_body(body).now_or_panic().unwrap();
118
119        assert_eq!(res, "kubi");
120    }
121}