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