1#[derive(Debug, Clone, Default, PartialEq, serde::Serialize, serde::Deserialize)]
2#[cfg_attr(feature = "server", derive(schemars::JsonSchema, aide::OperationIo))]
3pub enum QueryParam<T> {
4 #[default]
5 None,
6 Some(T),
7}
8
9impl<T: std::fmt::Display> std::fmt::Display for QueryParam<T> {
10 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
11 match self {
12 Self::None => write!(f, ""),
13 Self::Some(v) => write!(f, "{}", v),
14 }
15 }
16}
17
18impl<T: std::str::FromStr> std::str::FromStr for QueryParam<T> {
19 type Err = String;
20
21 fn from_str(s: &str) -> Result<Self, Self::Err> {
22 if s.is_empty() {
23 return Ok(Self::None);
24 }
25
26 match T::from_str(s) {
27 Ok(v) => Ok(Self::Some(v)),
28 Err(_) => Ok(Self::None),
29 }
30 }
31}
32
33impl<T: std::fmt::Display> QueryParam<T> {
34 pub fn to_string(&self) -> String {
35 format!("{}", self)
36 }
37}