canic_core/dto/
page.rs

1use candid::CandidType;
2use serde::{Deserialize, Serialize};
3
4///
5/// Page
6/// Generic pagination envelope
7///
8
9#[derive(CandidType, Clone, Debug, Deserialize, Serialize)]
10pub struct Page<T> {
11    pub entries: Vec<T>,
12    pub total: u64,
13}
14
15///
16/// PageRequest
17/// Pagination envelope to avoid passing raw integers around
18///
19
20#[derive(CandidType, Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
21pub struct PageRequest {
22    pub limit: u64,
23    pub offset: u64,
24}
25
26impl PageRequest {
27    pub const MAX_LIMIT: u64 = 1_000;
28    pub const DEFAULT: Self = Self {
29        limit: 50,
30        offset: 0,
31    };
32
33    #[must_use]
34    pub const fn new(limit: u64, offset: u64) -> Self {
35        Self { limit, offset }
36    }
37
38    #[must_use]
39    pub fn bounded(limit: u64, offset: u64) -> Self {
40        let limit = limit.min(Self::MAX_LIMIT);
41
42        Self { limit, offset }
43    }
44
45    #[must_use]
46    pub fn clamped(self) -> Self {
47        Self::bounded(self.limit, self.offset)
48    }
49}
50
51impl Default for PageRequest {
52    fn default() -> Self {
53        Self::DEFAULT
54    }
55}