Skip to main content

sqlx_data_params/
slice.rs

1use crate::{IntoParams, Params};
2
3#[derive(Clone, Debug)]
4pub struct SliceParams {
5    page: u32,
6    page_size: u32,
7    pub disable_total_count: bool,
8}
9
10impl Default for SliceParams {
11    fn default() -> Self {
12        Self {
13            page: 1,
14            page_size: 20,
15            disable_total_count: true,
16        }
17    }
18}
19
20impl SliceParams {
21    pub fn new(page: u32, per_page: u32) -> Self {
22        Self {
23            page: page.max(1),
24            page_size: per_page.max(1),
25            disable_total_count: true,
26        }
27    }
28
29    #[inline]
30    pub fn page(&self) -> u32 {
31        self.page
32    }
33
34    #[inline]
35    pub fn page_size(&self) -> u32 {
36        self.page_size
37    }
38
39    #[inline]
40    pub fn offset(&self) -> u32 {
41        (self.page.saturating_sub(1)).saturating_mul(self.page_size)
42    }
43
44    #[inline]
45    pub fn limit(&self) -> u32 {
46        self.page_size
47    }
48
49    #[inline]
50    pub fn disable_total_count(&self) -> bool {
51        self.disable_total_count
52    }
53
54    pub fn with_disable_total_count(mut self, disable: bool) -> Self {
55        self.disable_total_count = disable;
56        self
57    }
58}
59
60impl IntoParams for SliceParams {
61    fn into_params(self) -> Params {
62        let page_size = self.page_size;
63        let offset = self.offset();
64        Params {
65            filters: None,
66            search: None,
67            sort_by: None,
68            pagination: Some(crate::pagination::Pagination::Slice(self)),
69            limit: Some(crate::pagination::LimitParam(page_size)),
70            offset: Some(crate::pagination::OffsetParam(offset)),
71        }
72    }
73}