facet_urlencoded/
query.rs

1//! The `Query<T>` wrapper type for URL query parameters.
2
3use core::fmt;
4use core::ops::{Deref, DerefMut};
5
6/// A wrapper type for URL query parameter deserialization.
7///
8/// This type can be used standalone for convenient query string parsing,
9/// and when the `axum` feature is enabled, it implements Axum's
10/// `FromRequestParts` trait for extracting query parameters from the URL.
11///
12/// Unlike `Form<T>`, `Query<T>` does not consume the request body and can
13/// be used alongside other extractors.
14///
15/// # Example
16///
17/// ```
18/// use facet::Facet;
19/// use facet_urlencoded::Query;
20///
21/// #[derive(Debug, Facet)]
22/// struct SearchParams {
23///     q: String,
24///     page: u64,
25/// }
26///
27/// // Wrap a value
28/// let query = Query(SearchParams {
29///     q: "rust".to_string(),
30///     page: 1,
31/// });
32///
33/// // Access the inner value
34/// println!("Search query: {}", query.q);
35/// ```
36#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
37pub struct Query<T>(pub T);
38
39impl<T> Query<T> {
40    /// Consume the wrapper and return the inner value.
41    #[inline]
42    pub fn into_inner(self) -> T {
43        self.0
44    }
45}
46
47impl<T> From<T> for Query<T> {
48    #[inline]
49    fn from(inner: T) -> Self {
50        Query(inner)
51    }
52}
53
54impl<T> Deref for Query<T> {
55    type Target = T;
56
57    #[inline]
58    fn deref(&self) -> &Self::Target {
59        &self.0
60    }
61}
62
63impl<T> DerefMut for Query<T> {
64    #[inline]
65    fn deref_mut(&mut self) -> &mut Self::Target {
66        &mut self.0
67    }
68}
69
70impl<T> AsRef<T> for Query<T> {
71    #[inline]
72    fn as_ref(&self) -> &T {
73        &self.0
74    }
75}
76
77impl<T> AsMut<T> for Query<T> {
78    #[inline]
79    fn as_mut(&mut self) -> &mut T {
80        &mut self.0
81    }
82}
83
84impl<T: fmt::Display> fmt::Display for Query<T> {
85    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86        self.0.fmt(f)
87    }
88}