1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use futures::future;
use serde::de::DeserializeOwned;
use serde_urlencoded;
use crate::filter::{filter_fn_one, Filter, One};
use crate::reject::{self, Rejection};
pub fn query<T: DeserializeOwned + Send + 'static>(
) -> impl Filter<Extract = One<T>, Error = Rejection> + Copy {
filter_fn_one(|route| {
let query_string = route.query().unwrap_or_else(|| {
tracing::debug!("route was called without a query string, defaulting to empty");
""
});
let query_encoded = serde_urlencoded::from_str(query_string).map_err(|e| {
tracing::debug!("failed to decode query string '{}': {:?}", query_string, e);
reject::invalid_query()
});
future::ready(query_encoded)
})
}
pub fn raw() -> impl Filter<Extract = One<String>, Error = Rejection> + Copy {
filter_fn_one(|route| {
let route = route
.query()
.map(|q| q.to_owned())
.map(Ok)
.unwrap_or_else(|| Err(reject::invalid_query()));
future::ready(route)
})
}