Skip to main content

tdm_server_rust/common/
page_bean.rs

1//! 分页结果包装 (Page Bean)
2//!
3//! 对齐 Java `PageBean<E>`,封装分页查询的总记录数和当前页数据。
4
5use serde::Serialize;
6
7/// 分页数据包装器
8///
9/// 对应 Java `PageBean<E>`,用于列表接口的分页响应。
10///
11/// ## 字段
12///
13/// - `total`: 符合查询条件的总记录数
14/// - `rows`: 当前页的数据列表
15///
16/// ## 使用示例
17///
18/// ```rust
19/// use tdm_server_rust::common::PageBean;
20///
21/// let page = PageBean::new(100, vec!["a", "b", "c"]);
22/// assert_eq!(page.total, 100);
23/// assert_eq!(page.rows.len(), 3);
24///
25/// // 映射元素类型
26/// let mapped = page.map(|s| s.to_uppercase());
27/// ```
28#[derive(Debug, Clone, Serialize)]
29pub struct PageBean<T> {
30    /// 总记录数
31    pub total: i64,
32    /// 当前页数据
33    pub rows: Vec<T>,
34}
35
36impl<T> PageBean<T> {
37    /// 构造分页结果
38    pub fn new(total: i64, rows: Vec<T>) -> Self {
39        Self { total, rows }
40    }
41
42    /// 映射 rows 元素类型
43    pub fn map<U, F: FnMut(T) -> U>(self, mut f: F) -> PageBean<U> {
44        PageBean {
45            total: self.total,
46            rows: self.rows.into_iter().map(&mut f).collect(),
47        }
48    }
49}