canic_types/
page.rs

1use candid::CandidType;
2use serde::{Deserialize, Serialize};
3
4///
5/// PageRequest
6/// Common pagination envelope to avoid passing raw integers around.
7///
8
9#[derive(CandidType, Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
10pub struct PageRequest {
11    pub limit: u64,
12    pub offset: u64,
13}
14
15impl PageRequest {
16    pub const MAX_LIMIT: u64 = 1_000;
17    pub const DEFAULT: Self = Self {
18        limit: 50,
19        offset: 0,
20    };
21
22    #[must_use]
23    pub const fn new(limit: u64, offset: u64) -> Self {
24        Self { limit, offset }
25    }
26
27    #[must_use]
28    pub fn bounded(limit: u64, offset: u64) -> Self {
29        let limit = limit.min(Self::MAX_LIMIT);
30
31        Self { limit, offset }
32    }
33
34    #[must_use]
35    pub fn clamped(self) -> Self {
36        Self::bounded(self.limit, self.offset)
37    }
38}