fusion_common/page/
_page.rs1use serde::{Deserialize, Serialize};
2
3pub use super::*;
4
5#[derive(Debug, Clone, Default, Deserialize, Serialize)]
6#[cfg_attr(feature = "with-openapi", derive(utoipa::ToSchema))]
7#[cfg_attr(target_arch = "wasm32", derive(tsify::Tsify), tsify(into_wasm_abi, from_wasm_abi))]
8pub struct Page {
9 pub page: Option<u64>,
11 pub limit: Option<u64>,
13 pub offset: Option<u64>,
15 pub order_bys: Option<OrderBys>,
17}
18
19impl Page {
20 pub fn new_with_limit(limit: u64) -> Self {
21 Self { limit: Some(limit), ..Default::default() }
22 }
23
24 pub fn new_with_offset_limit(offset: u64, limit: u64) -> Self {
25 Self { limit: Some(limit), offset: Some(offset), ..Default::default() }
26 }
27
28 pub fn new_with_order_bys(order_bys: impl Into<OrderBys>) -> Self {
29 Self { order_bys: Some(order_bys.into()), ..Default::default() }
30 }
31
32 pub fn get_offset(&self) -> Option<u64> {
33 self.offset.or_else(|| self.page.map(|page| (page - 1) * self.limit.unwrap_or(0)))
34 }
35}
36
37impl From<OrderBys> for Page {
38 fn from(val: OrderBys) -> Self {
39 Self { order_bys: Some(val), ..Default::default() }
40 }
41}
42
43impl From<OrderBys> for Option<Page> {
44 fn from(val: OrderBys) -> Self {
45 Some(Page { order_bys: Some(val), ..Default::default() })
46 }
47}
48
49impl From<OrderBy> for Page {
50 fn from(val: OrderBy) -> Self {
51 Self { order_bys: Some(OrderBys::from(val)), ..Default::default() }
52 }
53}
54
55impl From<OrderBy> for Option<Page> {
56 fn from(val: OrderBy) -> Self {
57 Some(Page { order_bys: Some(OrderBys::from(val)), ..Default::default() })
58 }
59}