From acef5d8b8367ec174f4421f5520e7e2f32af758d Mon Sep 17 00:00:00 2001 From: Florian Dreschner Date: Mon, 24 May 2021 12:56:40 +0200 Subject: [PATCH] docs: Adds examples to query() --- src/filters/query.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/filters/query.rs b/src/filters/query.rs index b31f096f3..a2a766366 100644 --- a/src/filters/query.rs +++ b/src/filters/query.rs @@ -10,6 +10,58 @@ use crate::reject::{self, Rejection}; /// Creates a `Filter` that decodes query parameters to the type `T`. /// /// If cannot decode into a `T`, the request is rejected with a `400 Bad Request`. +/// +/// # Example +/// +/// ``` +/// use std::collections::HashMap; +/// use warp::{ +/// http::Response, +/// Filter, +/// }; +/// +/// let route = warp::any() +/// .and(warp::query::>()) +/// .map(|map: HashMap| { +/// let mut response: Vec = Vec::new(); +/// for (key, value) in map.into_iter() { +/// response.push(format!("{}={}", key, value)) +/// } +/// Response::builder().body(response.join(";")) +/// }); +/// ``` +/// +/// You can define your custom query object and deserialize with [Serde][Serde]. Ensure to include +/// the crate in your dependencies before usage. +/// +/// ``` +/// use serde_derive::{Deserialize, Serialize}; +/// use std::collections::HashMap; +/// use warp::{ +/// http::Response, +/// Filter, +/// }; +/// +/// #[derive(Serialize, Deserialize)] +/// struct FooQuery { +/// foo: Option, +/// bar: u8, +/// } +/// +/// let route = warp::any() +/// .and(warp::query::()) +/// .map(|q: FooQuery| { +/// if let Some(foo) = q.foo { +/// Response::builder().body(format!("foo={}", foo)) +/// } else { +/// Response::builder().body(format!("bar={}", q.bar)) +/// } +/// }); +/// ``` +/// +/// For more examples, please take a look at [examples/query_string.rs](https://github.com/seanmonstar/warp/blob/master/examples/query_string.rs). +/// +/// [Serde]: https://docs.rs/serde pub fn query( ) -> impl Filter, Error = Rejection> + Copy { filter_fn_one(|route| {