ic_canister_kit/common/
pages.rs

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use std::cmp::Ordering;

use candid::CandidType;
use serde::{Deserialize, Serialize};

// ============= 分页查询 =============

/// 分页对象
#[derive(CandidType, Serialize, Deserialize, Debug, Clone)]
pub struct QueryPage {
    /// 当前页码 1 开始计数
    pub page: u64,
    /// 每页大小
    pub size: u32,
}

/// 分页查询错误
#[derive(CandidType, Serialize, Deserialize, Debug, Clone)]
pub enum QueryPageError {
    /// 错误的页码,不能为 0
    WrongPage, // page can not be 0

    /// 错误的页面大小
    WrongSize {
        /// 页面大小
        size: u32,
        /// 最大页面大小
        max: u32,
    }, // size can not be 0 and has max value
}
impl std::fmt::Display for QueryPageError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            QueryPageError::WrongPage => write!(f, "page can not be 0"),
            QueryPageError::WrongSize { size, max } => {
                if *size == 0 {
                    write!(f, "size can not be 0")
                } else {
                    write!(f, "max({max}) < size({size})")
                }
            }
        }
    }
}
impl std::error::Error for QueryPageError {}

/// 分页查询结果
#[derive(CandidType, Serialize, Deserialize, Debug, Clone)]
pub struct PageData<T> {
    /// 请求的页码
    pub page: u64,
    /// 请求的页面大小
    pub size: u32,
    /// 总个数
    pub total: u64,
    /// 查到的分页数据
    pub data: Vec<T>,
}

impl<T: Clone> From<PageData<&T>> for PageData<T> {
    fn from(value: PageData<&T>) -> Self {
        PageData {
            page: value.page,
            size: value.size,
            total: value.total,
            data: value.data.into_iter().cloned().collect(),
        }
    }
}

// 空结果
impl QueryPage {
    /// 空结果
    #[inline]
    pub fn empty<T>(&self) -> PageData<T> {
        PageData {
            page: self.page,
            size: self.size,
            total: 0,
            data: Vec::new(),
        }
    }

    /// 检查分页选项是否有效
    #[inline]
    pub fn check(&self, max: u32) -> Result<(), QueryPageError> {
        if self.page == 0 {
            return Err(QueryPageError::WrongPage);
        }
        if self.size == 0 || max < self.size {
            return Err(QueryPageError::WrongSize {
                size: self.size,
                max,
            });
        }
        Ok(())
    }

    /// 分页数据对象
    #[inline]
    pub fn from_data<T>(&self, total: u64, data: Vec<T>) -> PageData<T> {
        PageData {
            page: self.page,
            size: self.size,
            total,
            data,
        }
    }

    #[inline]
    fn inner_query_by_list<'a, T>(
        &self,
        list: &'a [T],
        max: u32,
    ) -> Result<Vec<&'a T>, QueryPageError> {
        self.check(max)?;

        if list.is_empty() {
            return Ok(Vec::new());
        }

        let mut data = Vec::with_capacity(self.size as usize);

        // 偏移序号
        let start = ((self.page - 1) * self.size as u64) as usize;
        let end = ((self.page) * self.size as u64) as usize;

        if end < list.len() {
            data = list[start..end].iter().collect();
        } else if start < list.len() {
            data = list[start..].iter().collect();
        }

        Ok(data)
    }

    /// 对所有数据进行分页查询
    #[inline]
    pub fn query_by_list<'a, T>(
        &self,
        list: &'a [T],
        max: u32,
    ) -> Result<PageData<&'a T>, QueryPageError> {
        let total = list.len() as u64;

        let data = self.inner_query_by_list(list, max)?;

        Ok(self.from_data(total, data))
    }

    /// 对所有数据进行倒序分页查询
    #[inline]
    pub fn query_desc_by_list<'a, T>(
        &self,
        list: &'a [T],
        max: u32,
    ) -> Result<PageData<&'a T>, QueryPageError> {
        // 取出倒序索引
        let index_list: Vec<usize> = (0..list.len()).rev().collect();

        let total = index_list.len() as u64;

        let data = self.inner_query_by_list(&index_list, max)?;

        let data = data.into_iter().map(|i| &list[*i]).collect::<Vec<_>>();

        Ok(self.from_data(total, data))
    }

    /// 倒序过滤分页查询
    #[inline]
    pub fn query_desc_by_list_and_filter<'a, T, F>(
        &self,
        list: &'a [T],
        max: u32,
        filter: F, // 过滤条件
    ) -> Result<PageData<&'a T>, QueryPageError>
    where
        F: Fn(&T) -> bool,
    {
        // 取出过滤后的倒序索引
        let index_list: Vec<usize> = (0..list.len())
            .filter(|i| filter(&list[*i]))
            .rev()
            .collect();

        let total = index_list.len() as u64;

        let data = self.inner_query_by_list(&index_list, max)?;

        let data = data.into_iter().map(|i| &list[*i]).collect::<Vec<_>>();

        Ok(self.from_data(total, data))
    }

    /// 按条件分页查询
    #[inline]
    pub fn custom_query_by_list<T, R, Filter, Compare, Transform>(
        &self,
        list: &[T],
        max: u32,
        filter: Filter,       // 过滤条件
        compare: Compare,     // 排序方法
        transform: Transform, // 变形方法
    ) -> Result<PageData<R>, QueryPageError>
    where
        Filter: Fn(&T) -> bool,
        Compare: Fn(&T, &T) -> Ordering,
        Transform: Fn(&T) -> R,
    {
        // 1. 过滤有效的结果
        let mut list: Vec<&T> = list.iter().filter(|&item| filter(item)).collect();

        // 2. 进行排序
        list.sort_by(|&a, &b| compare(a, b));

        let total = list.len() as u64;

        let data = self.inner_query_by_list(&list, max)?;

        let data = data.into_iter().map(|t| transform(t)).collect::<Vec<_>>();

        Ok(self.from_data(total, data))
    }
}