modql/filter/list_options/
mod.rs1mod order_by;
2
3pub use order_by::*;
4use serde::Deserialize;
5
6#[derive(Default, Debug, Clone, Deserialize)]
7pub struct ListOptions {
8 pub limit: Option<i64>,
9 pub offset: Option<i64>,
10 pub order_bys: Option<OrderBys>,
11}
12
13impl ListOptions {
16 pub fn from_limit(limit: i64) -> Self {
17 Self {
18 limit: Some(limit),
19 ..Default::default()
20 }
21 }
22
23 pub fn from_offset_limit(offset: i64, limit: i64) -> Self {
24 Self {
25 limit: Some(limit),
26 offset: Some(offset),
27 ..Default::default()
28 }
29 }
30
31 pub fn from_order_bys(order_bys: impl Into<OrderBys>) -> Self {
32 Self {
33 order_bys: Some(order_bys.into()),
34 ..Default::default()
35 }
36 }
37}
38
39impl From<OrderBys> for ListOptions {
44 fn from(val: OrderBys) -> Self {
45 Self {
46 order_bys: Some(val),
47 ..Default::default()
48 }
49 }
50}
51
52impl From<OrderBys> for Option<ListOptions> {
53 fn from(val: OrderBys) -> Self {
54 Some(ListOptions {
55 order_bys: Some(val),
56 ..Default::default()
57 })
58 }
59}
60
61impl From<OrderBy> for ListOptions {
62 fn from(val: OrderBy) -> Self {
63 Self {
64 order_bys: Some(OrderBys::from(val)),
65 ..Default::default()
66 }
67 }
68}
69
70impl From<OrderBy> for Option<ListOptions> {
71 fn from(val: OrderBy) -> Self {
72 Some(ListOptions {
73 order_bys: Some(OrderBys::from(val)),
74 ..Default::default()
75 })
76 }
77}
78
79#[cfg(feature = "with-sea-query")]
83mod with_sea_query {
84 use super::*;
85 use sea_query::SelectStatement;
86
87 impl ListOptions {
88 pub fn apply_to_sea_query(self, select_query: &mut SelectStatement) {
89 fn as_positive_u64(num: i64) -> u64 {
90 if num < 0 {
91 0
92 } else {
93 num as u64
94 }
95 }
96 if let Some(limit) = self.limit {
97 select_query.limit(as_positive_u64(limit)); }
99
100 if let Some(offset) = self.offset {
101 select_query.offset(as_positive_u64(offset)); }
103
104 if let Some(order_bys) = self.order_bys {
105 for (col, order) in order_bys.into_sea_col_order_iter() {
106 select_query.order_by(col, order);
107 }
108 }
109 }
110 }
111}
112