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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
extern crate warp_framework as warp;
use crate::{de::Config as QsConfig, error};
use serde::de;
use std::sync::Arc;
use warp::{http::StatusCode, reject::Reject, Filter, Rejection, Reply};
impl Reject for error::Error {}
pub fn query<T>(config: QsConfig) -> impl Filter<Extract = (T,), Error = Rejection> + Clone
where
T: de::DeserializeOwned + Send + 'static,
{
let config = Arc::new(config);
warp::query::raw()
.or_else(|_| async {
tracing::debug!("route was called without a query string, defaulting to empty");
Ok::<_, Rejection>((String::new(),))
})
.and_then(move |query: String| {
let config = Arc::clone(&config);
async move {
config.deserialize_str(query.as_str()).map_err(|err| {
tracing::debug!("failed to decode query string '{}': {:?}", query, err);
Rejection::from(err)
})
}
})
}
pub async fn recover_fn(rejection: Rejection) -> Result<impl Reply, Rejection> {
if let Some(err) = rejection.find::<error::Error>() {
Ok(warp::reply::with_status(
err.to_string(),
StatusCode::BAD_REQUEST,
))
} else {
Err(rejection)
}
}