Skip to main content

fusion_common/page/
paged.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Serialize, Deserialize)]
4#[cfg_attr(feature = "with-openapi", derive(utoipa::ToSchema))]
5#[cfg_attr(target_arch = "wasm32", derive(tsify::Tsify), tsify(into_wasm_abi, from_wasm_abi))]
6pub struct Paged {
7  pub total: u64,
8  pub has_more: bool,
9}
10
11impl Paged {
12  pub fn new(total: u64) -> Self {
13    Self { total, has_more: false }
14  }
15
16  pub fn with_has_more(mut self, has_more: bool) -> Self {
17    self.has_more = has_more;
18    self
19  }
20}
21
22#[derive(Debug, Clone, Serialize, Deserialize)]
23#[cfg_attr(feature = "with-openapi", derive(utoipa::ToSchema))]
24#[cfg_attr(target_arch = "wasm32", derive(tsify::Tsify), tsify(into_wasm_abi, from_wasm_abi))]
25pub struct PageResult<T> {
26  pub page: Paged,
27  pub result: Vec<T>,
28}
29
30impl<T> PageResult<T> {
31  pub fn new(total: u64, result: Vec<T>) -> Self {
32    Self { page: Paged::new(total), result }
33  }
34
35  pub fn with_has_more(mut self, has_more: bool) -> Self {
36    self.page = self.page.with_has_more(has_more);
37    self
38  }
39}