viz_core/types/
query.rs

1//! Represents a query extractor.
2
3use std::{
4    fmt,
5    ops::{Deref, DerefMut},
6};
7
8use serde::de::DeserializeOwned;
9
10use crate::{types::PayloadError, FromRequest, Request, RequestExt, Result};
11
12/// Extracts the data from the query string of a URL.
13pub struct Query<T = ()>(pub T);
14
15impl<T> Query<T> {
16    /// Create new `Query` instance.
17    #[inline]
18    pub const fn new(data: T) -> Self {
19        Self(data)
20    }
21
22    /// Consumes the Query, returning the wrapped value.
23    #[inline]
24    pub fn into_inner(self) -> T {
25        self.0
26    }
27}
28
29impl<T> Clone for Query<T>
30where
31    T: Clone,
32{
33    fn clone(&self) -> Self {
34        Self(self.0.clone())
35    }
36}
37
38impl<T> AsRef<T> for Query<T> {
39    fn as_ref(&self) -> &T {
40        &self.0
41    }
42}
43
44impl<T> Deref for Query<T> {
45    type Target = T;
46
47    fn deref(&self) -> &T {
48        &self.0
49    }
50}
51
52impl<T> DerefMut for Query<T> {
53    fn deref_mut(&mut self) -> &mut T {
54        &mut self.0
55    }
56}
57
58impl<T> fmt::Debug for Query<T>
59where
60    T: fmt::Debug,
61{
62    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63        T::fmt(self, f)
64    }
65}
66
67impl<T> FromRequest for Query<T>
68where
69    T: DeserializeOwned,
70{
71    type Error = PayloadError;
72
73    async fn extract(req: &mut Request) -> Result<Self, Self::Error> {
74        req.query().map(Self)
75    }
76}