cat_gateway/service/common/objects/generic/
pagination.rs

1//! Pagination response object to be included in every paged response.
2
3use poem_openapi::{types::Example, Object};
4
5use crate::service::common;
6
7/// Description for the `CurrentPage` object.
8#[allow(dead_code)]
9pub(crate) const CURRENT_PAGE_DESCRIPTION: &str =
10    "The Page of results is being returned, and the Limit of results.
11The data returned is constrained by this limit.
12The limit applies to the total number of records returned.
13*Note: The Limit may not be exactly as requested, if it was constrained by the response.
14The caller must read this record to ensure the correct data requested was returned.*";
15
16#[derive(Object)]
17#[oai(example = true)]
18/// Current Page of data being returned.
19pub(crate) struct CurrentPage {
20    #[allow(clippy::missing_docs_in_private_items)] // Type is pre documented
21    pub page: common::types::generic::query::pagination::Page,
22    #[allow(clippy::missing_docs_in_private_items)] // Type is pre documented
23    pub limit: common::types::generic::query::pagination::Limit,
24    #[allow(clippy::missing_docs_in_private_items)] // Type is pre documented
25    pub remaining: common::types::generic::query::pagination::Remaining,
26}
27
28impl Example for CurrentPage {
29    fn example() -> Self {
30        Self {
31            page: common::types::generic::query::pagination::Page::example(),
32            limit: common::types::generic::query::pagination::Limit::example(),
33            remaining: common::types::generic::query::pagination::Remaining::example(),
34        }
35    }
36}
37
38impl CurrentPage {
39    /// Create a new `CurrentPage` object.
40    ///
41    /// # Errors
42    ///  - Invalid `page` value, must be in range
43    ///  - Invalid `limit` value, must be in range
44    ///  - Invalid `remaining` value, must be in range
45    #[allow(dead_code)]
46    fn new(page: u32, limit: u32, remaining: u32) -> anyhow::Result<Self> {
47        Ok(Self {
48            page: page.try_into()?,
49            limit: limit.try_into()?,
50            remaining: remaining.try_into()?,
51        })
52    }
53}