Skip to content

Commit c763969

Browse files
authored
Merge pull request #820 from Fishrock123/request-deserialize-query-borrowed
Request: allow Deserialize<'de> for .query()
2 parents 88a2aa2 + 631e024 commit c763969

File tree

2 files changed

+27
-40
lines changed

2 files changed

+27
-40
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ async-trait = "0.1.41"
4141
femme = { version = "2.1.1", optional = true }
4242
futures-util = "0.3.6"
4343
http-client = { version = "6.1.0", default-features = false }
44-
http-types = { version = "2.10.0", default-features = false, features = ["fs"] }
44+
http-types = { version = "2.11.0", default-features = false, features = ["fs"] }
4545
kv-log-macro = "1.0.7"
4646
log = { version = "0.4.13", features = ["kv_unstable_std"] }
4747
pin-project-lite = "0.2.0"

src/request.rs

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -302,54 +302,41 @@ impl<State> Request<State> {
302302
}
303303

304304
/// Parse the URL query component into a struct, using [serde_qs](https://docs.rs/serde_qs). To
305-
/// get the entire query as an unparsed string, use `request.url().query()`
305+
/// get the entire query as an unparsed string, use `request.url().query()`.
306306
///
307-
/// ```rust
308-
/// # fn main() -> Result<(), std::io::Error> { async_std::task::block_on(async {
309-
/// use tide::prelude::*;
310-
/// let mut app = tide::new();
307+
/// # Examples
311308
///
312-
/// #[derive(Deserialize)]
313-
/// #[serde(default)]
314-
/// struct Page {
315-
/// size: u8,
316-
/// offset: u8,
317-
/// }
318-
/// impl Default for Page {
319-
/// fn default() -> Self {
320-
/// Self {
321-
/// size: 25,
322-
/// offset: 0,
323-
/// }
324-
/// }
325-
/// }
326-
/// app.at("/pages").post(|req: tide::Request<()>| async move {
327-
/// let page: Page = req.query()?;
328-
/// Ok(format!("page {}, with {} items", page.offset, page.size))
329-
/// });
309+
/// ```
310+
/// use std::collections::HashMap;
311+
/// use tide::http::{self, convert::Deserialize};
312+
/// use tide::Request;
330313
///
331-
/// # if false {
332-
/// app.listen("localhost:8000").await?;
333-
/// # }
314+
/// // An owned structure:
334315
///
335-
/// // $ curl localhost:8000/pages
336-
/// // page 0, with 25 items
316+
/// #[derive(Deserialize)]
317+
/// struct Index {
318+
/// page: u32,
319+
/// selections: HashMap<String, String>,
320+
/// }
337321
///
338-
/// // $ curl localhost:8000/pages?offset=1
339-
/// // page 1, with 25 items
322+
/// let req: Request<()> = http::Request::get("https://httpbin.org/get?page=2&selections[width]=narrow&selections[height]=tall").into();
323+
/// let Index { page, selections } = req.query().unwrap();
324+
/// assert_eq!(page, 2);
325+
/// assert_eq!(selections["width"], "narrow");
326+
/// assert_eq!(selections["height"], "tall");
340327
///
341-
/// // $ curl localhost:8000/pages?offset=2&size=50
342-
/// // page 2, with 50 items
328+
/// // Using borrows:
343329
///
344-
/// // $ curl localhost:8000/pages?size=5000
345-
/// // failed with reason: number too large to fit in target type
330+
/// #[derive(Deserialize)]
331+
/// struct Query<'q> {
332+
/// format: &'q str,
333+
/// }
346334
///
347-
/// // $ curl localhost:8000/pages?size=all
348-
/// // failed with reason: invalid digit found in string
349-
/// # Ok(()) })}
335+
/// let req: Request<()> = http::Request::get("https://httpbin.org/get?format=bananna").into();
336+
/// let Query { format } = req.query().unwrap();
337+
/// assert_eq!(format, "bananna");
350338
/// ```
351-
352-
pub fn query<T: serde::de::DeserializeOwned>(&self) -> crate::Result<T> {
339+
pub fn query<'de, T: serde::de::Deserialize<'de>>(&'de self) -> crate::Result<T> {
353340
self.req.query()
354341
}
355342

0 commit comments

Comments
 (0)