File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -126,6 +126,37 @@ where
126126 }
127127}
128128
129+ impl < T > Query < T >
130+ where
131+ T : DeserializeOwned ,
132+ {
133+ /// Attempts to construct a [`Query`] from a reference to a [`Uri`].
134+ ///
135+ /// # Example
136+ /// ```
137+ /// use axum_extra::extract::Query;
138+ /// use http::Uri;
139+ /// use serde::Deserialize;
140+ ///
141+ /// #[derive(Deserialize)]
142+ /// struct ExampleParams {
143+ /// foo: String,
144+ /// bar: u32,
145+ /// }
146+ ///
147+ /// let uri: Uri = "http://example.com/path?foo=hello&bar=42".parse().unwrap();
148+ /// let result: Query<ExampleParams> = Query::try_from_uri(&uri).unwrap();
149+ /// assert_eq!(result.foo, String::from("hello"));
150+ /// assert_eq!(result.bar, 42);
151+ /// ```
152+ pub fn try_from_uri ( value : & Uri ) -> Result < Self , QueryRejection > {
153+ let query = value. query ( ) . unwrap_or_default ( ) ;
154+ let params =
155+ serde_html_form:: from_str ( query) . map_err ( FailedToDeserializeQueryString :: from_err) ?;
156+ Ok ( Self ( params) )
157+ }
158+ }
159+
129160axum_core:: __impl_deref!( Query ) ;
130161
131162define_rejection ! {
You can’t perform that action at this time.
0 commit comments