Skip to main content

modkit_odata/
page.rs

1use serde::{Deserialize, Serialize};
2
3#[cfg_attr(feature = "with-utoipa", derive(utoipa::ToSchema))]
4#[derive(Clone, Debug, Serialize, Deserialize)]
5pub struct PageInfo {
6    pub next_cursor: Option<String>,
7    pub prev_cursor: Option<String>,
8    pub limit: u64,
9}
10
11#[cfg_attr(feature = "with-utoipa", derive(utoipa::ToSchema))]
12#[derive(Clone, Debug, Serialize, Deserialize)]
13pub struct Page<T> {
14    pub items: Vec<T>,
15    pub page_info: PageInfo,
16}
17
18impl<T> Page<T> {
19    /// Create a new page with items and page info
20    #[must_use]
21    pub fn new(items: Vec<T>, page_info: PageInfo) -> Self {
22        Self { items, page_info }
23    }
24
25    /// Create an empty page with the given limit
26    #[must_use]
27    pub fn empty(limit: u64) -> Self {
28        Self {
29            items: Vec::new(),
30            page_info: PageInfo {
31                next_cursor: None,
32                prev_cursor: None,
33                limit,
34            },
35        }
36    }
37
38    /// Map items while preserving `page_info` (Domain->DTO mapping convenience)
39    pub fn map_items<U>(self, mut f: impl FnMut(T) -> U) -> Page<U> {
40        Page {
41            items: self.items.into_iter().map(&mut f).collect(),
42            page_info: self.page_info,
43        }
44    }
45}