iota_sdk_graphql_client/
pagination.rs

1// Copyright (c) 2025 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::query_types::PageInfo;
5
6/// A page of items returned by the GraphQL server.
7#[derive(Clone, Debug)]
8pub struct Page<T> {
9    /// Information about the page, such as the cursor and whether there are
10    /// more pages.
11    pub page_info: PageInfo,
12    /// The data returned by the server.
13    pub data: Vec<T>,
14}
15
16impl<T> Page<T> {
17    /// Return the page information.
18    pub fn page_info(&self) -> &PageInfo {
19        &self.page_info
20    }
21
22    /// Return the data in the page.
23    pub fn data(&self) -> &[T] {
24        &self.data
25    }
26
27    /// Create a new page with the provided data and page information.
28    pub fn new(page_info: PageInfo, data: Vec<T>) -> Self {
29        Self { page_info, data }
30    }
31
32    /// Check if the page has no data.
33    pub fn is_empty(&self) -> bool {
34        self.data.is_empty()
35    }
36
37    /// Create a page with no data.
38    pub fn new_empty() -> Self {
39        Self::new(PageInfo::default(), vec![])
40    }
41
42    /// Return a tuple of page info and the data.
43    pub fn into_parts(self) -> (PageInfo, Vec<T>) {
44        (self.page_info, self.data)
45    }
46
47    pub fn map<F: Fn(T) -> U, U>(self, map_fn: F) -> Page<U> {
48        Page {
49            page_info: self.page_info,
50            data: self.data.into_iter().map(map_fn).collect(),
51        }
52    }
53}
54
55/// Pagination direction.
56#[derive(Clone, Debug, Default)]
57pub enum Direction {
58    #[default]
59    Forward,
60    Backward,
61}
62
63/// Pagination options for querying the GraphQL server. It defaults to forward
64/// pagination with the GraphQL server's max page size.
65#[derive(Clone, Debug, Default)]
66pub struct PaginationFilter {
67    /// The direction of pagination.
68    pub direction: Direction,
69    /// An opaque cursor used for pagination.
70    pub cursor: Option<String>,
71    /// The maximum number of items to return. If this is omitted, it will
72    /// lazily query the service configuration for the max page size.
73    pub limit: Option<i32>,
74}
75
76#[derive(Clone, Debug, Default)]
77pub struct PaginationFilterResponse {
78    pub after: Option<String>,
79    pub before: Option<String>,
80    pub first: Option<i32>,
81    pub last: Option<i32>,
82}