-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathquery.rs
More file actions
75 lines (66 loc) · 1.79 KB
/
Copy pathquery.rs
File metadata and controls
75 lines (66 loc) · 1.79 KB
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
use axum::{
async_trait,
extract::FromRequestParts,
http::{request::Parts, StatusCode},
};
use serde::{
de::{DeserializeOwned, Deserializer},
Deserialize,
};
use serde_with::{serde_as, DisplayFromStr};
use crate::error::ApiError;
const DEFAUT_PAGINATION_SIZE: usize = 30;
pub fn default_pagination_size() -> usize {
DEFAUT_PAGINATION_SIZE
}
#[serde_as]
#[derive(Deserialize, Debug)]
pub struct PaginationQuery {
#[serde_as(as = "DisplayFromStr")]
#[serde(default = "default_pagination_size")]
pub size: usize,
#[serde(default)]
#[serde(deserialize_with = "undefined_to_none")]
pub next: Option<String>,
}
impl Default for PaginationQuery {
fn default() -> Self {
Self {
size: DEFAUT_PAGINATION_SIZE,
next: None,
}
}
}
fn undefined_to_none<'de, D>(d: D) -> Result<Option<String>, D::Error>
where
D: Deserializer<'de>,
{
let v: Option<String> = Deserialize::deserialize(d)?;
match v {
Some(v) if v.as_str() != "undefined" => Ok(Some(v)),
_ => Ok(None),
}
}
pub struct Query<T>(pub T);
#[async_trait]
impl<S, T> FromRequestParts<S> for Query<T>
where
T: Default + DeserializeOwned,
S: Send + Sync,
{
type Rejection = ApiError;
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
let query = parts.uri.query().unwrap_or_default();
if query.is_empty() {
return Ok(Self(T::default()));
}
match serde_urlencoded::from_str(query) {
Ok(v) => Ok(Query(v)),
Err(e) => Err(ApiError::new(
StatusCode::BAD_REQUEST,
format!("Invalid query parameter value for {query}. {e}"),
parts.uri.to_string(),
)),
}
}
}