Skip to main content

fusion_common/page/
order_by.rs

1use std::ops::Deref;
2
3use serde::{Deserialize, Serialize};
4
5// region:    --- OrderBy
6#[derive(Debug, Clone, Serialize, Deserialize)]
7#[cfg_attr(feature = "with-openapi", derive(utoipa::ToSchema))]
8#[cfg_attr(target_arch = "wasm32", derive(tsify::Tsify), tsify(into_wasm_abi, from_wasm_abi))]
9#[serde(transparent)]
10pub struct OrderBy(String);
11
12impl OrderBy {
13  pub fn to_sql(&self) -> String {
14    if let Some(stripped) = self.0.strip_prefix('!') { format!("{} desc", stripped) } else { format!("{} asc", self.0) }
15  }
16}
17
18impl From<&str> for OrderBy {
19  fn from(value: &str) -> Self {
20    Self(value.to_string())
21  }
22}
23
24impl From<&String> for OrderBy {
25  fn from(value: &String) -> Self {
26    Self(value.to_string())
27  }
28}
29
30impl From<String> for OrderBy {
31  fn from(value: String) -> Self {
32    Self(value)
33  }
34}
35
36impl Deref for OrderBy {
37  type Target = String;
38
39  fn deref(&self) -> &Self::Target {
40    &self.0
41  }
42}
43// endregion: --- OrderBy
44
45// region:    --- OrderBys
46#[derive(Debug, Clone, Serialize, Deserialize)]
47#[cfg_attr(feature = "with-openapi", derive(utoipa::ToSchema))]
48#[cfg_attr(target_arch = "wasm32", derive(tsify::Tsify), tsify(into_wasm_abi, from_wasm_abi))]
49#[serde(transparent)]
50pub struct OrderBys(Vec<OrderBy>);
51
52impl Default for OrderBys {
53  fn default() -> Self {
54    OrderBys::new(vec![])
55  }
56}
57
58impl OrderBys {
59  pub fn new(v: Vec<OrderBy>) -> Self {
60    OrderBys(v)
61  }
62
63  pub fn into_inner(self) -> Vec<OrderBy> {
64    self.0
65  }
66
67  pub fn is_empty(&self) -> bool {
68    self.0.is_empty()
69  }
70}
71
72// This will allow us to iterate over &OrderBys
73impl<'a> IntoIterator for &'a OrderBys {
74  type Item = &'a OrderBy;
75  type IntoIter = std::slice::Iter<'a, OrderBy>;
76
77  fn into_iter(self) -> Self::IntoIter {
78    self.0.iter()
79  }
80}
81
82// This will allow us to iterate over OrderBys directly (consuming it)
83impl IntoIterator for OrderBys {
84  type Item = OrderBy;
85  type IntoIter = std::vec::IntoIter<OrderBy>;
86
87  fn into_iter(self) -> Self::IntoIter {
88    self.0.into_iter()
89  }
90}
91
92// NOTE: If we want the Vec<T> and T, we have to make the individual from
93//       specific to the type. Otherwise, conflict.
94
95impl From<&str> for OrderBys {
96  fn from(val: &str) -> Self {
97    OrderBys(vec![val.into()])
98  }
99}
100impl From<&String> for OrderBys {
101  fn from(val: &String) -> Self {
102    OrderBys(vec![val.into()])
103  }
104}
105impl From<String> for OrderBys {
106  fn from(val: String) -> Self {
107    OrderBys(vec![val.into()])
108  }
109}
110
111impl From<OrderBy> for OrderBys {
112  fn from(val: OrderBy) -> Self {
113    OrderBys(vec![val])
114  }
115}
116
117impl From<Vec<&str>> for OrderBys {
118  fn from(val: Vec<&str>) -> Self {
119    let d = val.into_iter().map(OrderBy::from).collect::<Vec<_>>();
120    OrderBys(d)
121  }
122}
123
124impl From<Vec<&String>> for OrderBys {
125  fn from(val: Vec<&String>) -> Self {
126    let d = val.into_iter().map(OrderBy::from).collect::<Vec<_>>();
127    OrderBys(d)
128  }
129}
130
131impl From<Vec<String>> for OrderBys {
132  fn from(val: Vec<String>) -> Self {
133    let d = val.into_iter().map(OrderBy::from).collect::<Vec<_>>();
134    OrderBys(d)
135  }
136}
137
138// endregion: --- OrderBys