Skip to main content

wave_api/
pagination.rs

1use serde::Deserialize;
2
3/// Flattened page of results from a Wave connection type.
4#[derive(Debug, Clone)]
5pub struct Page<T> {
6    /// Items on this page.
7    pub items: Vec<T>,
8    /// Current page number (1-based).
9    pub current_page: i64,
10    /// Total number of pages.
11    pub total_pages: Option<i64>,
12    /// Total number of items across all pages.
13    pub total_count: Option<i64>,
14}
15
16impl<T> Page<T> {
17    /// Whether there are more pages after this one.
18    pub fn has_next_page(&self) -> bool {
19        match self.total_pages {
20            Some(tp) => self.current_page < tp,
21            None => false,
22        }
23    }
24}
25
26/// Raw offset page info from the API.
27#[derive(Debug, Clone, Deserialize)]
28#[serde(rename_all = "camelCase")]
29pub struct OffsetPageInfo {
30    pub current_page: i64,
31    pub total_pages: Option<i64>,
32    pub total_count: Option<i64>,
33}
34
35/// Raw connection wrapper for deserialization. Can be used directly for
36/// custom queries, but most users should use the flattened [`Page<T>`] returned
37/// by client methods.
38#[derive(Debug, Clone, Deserialize)]
39#[serde(rename_all = "camelCase")]
40pub struct Connection<T> {
41    pub edges: Vec<Edge<T>>,
42    pub page_info: OffsetPageInfo,
43}
44
45/// Raw edge wrapper for deserialization.
46#[derive(Debug, Clone, Deserialize)]
47pub struct Edge<T> {
48    pub node: T,
49}
50
51impl<T> Connection<T> {
52    /// Flatten a Connection into a Page.
53    pub fn into_page(self) -> Page<T> {
54        Page {
55            items: self.edges.into_iter().map(|e| e.node).collect(),
56            current_page: self.page_info.current_page,
57            total_pages: self.page_info.total_pages,
58            total_count: self.page_info.total_count,
59        }
60    }
61}