Skip to main content

openlark_docs/common/
mod.rs

1/// 通用模块
2///
3/// 提供openlark-docs项目中通用的工具、宏和类型定义。
4pub mod api_endpoints;
5pub mod api_utils;
6pub mod builders;
7pub mod chain;
8pub mod request_builder;
9
10// 重新导出API端点枚举
11pub use api_endpoints::{BaseApiV2, BitableApiV1, MinutesApiV1, SheetsApiV3};
12
13/// 通用常量定义
14pub mod constants {
15    /// 默认分页大小
16    pub const DEFAULT_PAGE_SIZE: i32 = 20;
17    /// 最大分页大小
18    pub const MAX_PAGE_SIZE: i32 = 100;
19    /// 默认超时时间(秒)
20    pub const DEFAULT_TIMEOUT_SECS: u64 = 30;
21}
22
23/// 通用类型别名
24pub mod types {
25    /// 通用字符串类型
26    pub type AppToken = String;
27    pub type TableId = String;
28    pub type RecordId = String;
29    pub type FormId = String;
30    pub type ViewId = String;
31    pub type FieldId = String;
32    pub type RoleId = String;
33    pub type UserId = String;
34}
35
36/// 批量操作相关类型
37pub mod batch {
38    use serde::{Deserialize, Serialize};
39
40    /// 通用批量操作参数
41    #[derive(Debug, Clone, Default, Serialize, Deserialize)]
42    pub struct BatchCommonParams {
43        /// 用户ID类型
44        pub user_id_type: Option<String>,
45        /// 客户端令牌
46        pub client_token: Option<String>,
47    }
48
49    impl BatchCommonParams {
50        pub fn new() -> Self {
51            Self::default()
52        }
53
54        /// 设置用户ID类型
55        pub fn with_user_id_type(mut self, user_id_type: impl ToString) -> Self {
56            self.user_id_type = Some(user_id_type.to_string());
57            self
58        }
59
60        /// 设置客户端令牌
61        pub fn with_client_token(mut self, client_token: impl ToString) -> Self {
62            self.client_token = Some(client_token.to_string());
63            self
64        }
65    }
66
67    /// 通用批量请求体
68    #[derive(Debug, Clone, Default, Serialize, Deserialize)]
69    pub struct BatchCommonBody {
70        /// 请求列表
71        pub requests: Vec<serde_json::Value>,
72    }
73
74    /// 通用批量操作结果
75    #[derive(Debug, Clone, Default, Serialize, Deserialize)]
76    pub struct BatchOperationResult {
77        /// 成功标识
78        pub success: bool,
79        /// 结果列表
80        pub results: Vec<BatchItemResult>,
81        /// 错误信息(如果有)
82        pub error: Option<String>,
83    }
84
85    impl BatchOperationResult {
86        /// 创建成功的结果
87        pub fn success() -> Self {
88            Self {
89                success: true,
90                results: Vec::new(),
91                error: None,
92            }
93        }
94
95        /// 创建失败的结果
96        pub fn failure(error: impl ToString) -> Self {
97            Self {
98                success: false,
99                results: Vec::new(),
100                error: Some(error.to_string()),
101            }
102        }
103    }
104
105    /// 批量操作中的单项结果
106    #[derive(Debug, Clone, Default, Serialize, Deserialize)]
107    pub struct BatchItemResult {
108        /// 成功标识
109        pub success: bool,
110        /// 数据(如果有)
111        pub data: Option<serde_json::Value>,
112        /// 错误信息(如果有)
113        pub error: Option<String>,
114    }
115}
116
117/// 通用特征定义
118pub mod traits {
119    /// API请求基础特征
120    pub trait ApiRequest {
121        type Response;
122        fn validate(&self) -> openlark_core::SDKResult<()>;
123        fn build_path(&self) -> String;
124    }
125
126    /// 可分页请求特征
127    pub trait PaginatedRequest {
128        fn page_token(self, token: impl Into<String>) -> Self;
129        fn page_size(self, size: i32) -> Self;
130    }
131
132    /// 可筛选请求特征
133    pub trait FilterableRequest {
134        fn add_filter(self, filter: serde_json::Value) -> Self;
135    }
136}