Skip to main content

openlark_workflow/v2/custom_field/
models.rs

1//! 自定义字段 API v2 的数据模型
2
3use serde::{Deserialize, Serialize};
4
5/// 自定义字段类型
6#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
7#[serde(rename_all = "snake_case")]
8pub enum CustomFieldType {
9    /// 文本
10    Text,
11    /// 数字
12    Number,
13    /// 日期
14    Date,
15    /// 人员
16    Person,
17    /// 复选框
18    Checkbox,
19    /// 下拉选项
20    Select,
21}
22
23/// 自定义字段配置
24#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
25pub struct CustomFieldConfig {
26    /// 字段类型
27    #[serde(rename = "type")]
28    pub field_type: CustomFieldType,
29    /// 字段选项(用于 Select 类型)
30    #[serde(skip_serializing_if = "Option::is_none")]
31    pub options: Option<Vec<String>>,
32}
33
34impl Default for CustomFieldConfig {
35    fn default() -> Self {
36        Self {
37            field_type: CustomFieldType::Text,
38            options: None,
39        }
40    }
41}
42
43/// 创建自定义字段请求体
44#[derive(Debug, Clone, Serialize, Default)]
45pub struct CreateCustomFieldBody {
46    /// 字段名称
47    pub name: String,
48    /// 字段配置
49    pub config: CustomFieldConfig,
50}
51
52/// 更新自定义字段请求体
53#[derive(Debug, Clone, Serialize, Default)]
54pub struct UpdateCustomFieldBody {
55    /// 字段名称
56    #[serde(skip_serializing_if = "Option::is_none")]
57    pub name: Option<String>,
58    /// 字段配置
59    #[serde(skip_serializing_if = "Option::is_none")]
60    pub config: Option<CustomFieldConfig>,
61}
62
63/// 创建自定义字段响应
64#[derive(Debug, Clone, Deserialize)]
65pub struct CreateCustomFieldResponse {
66    /// 字段 GUID
67    pub field_guid: String,
68    /// 字段名称
69    pub name: String,
70    /// 字段配置
71    pub config: CustomFieldConfig,
72    /// 创建时间
73    pub created_at: String,
74    /// 更新时间
75    pub updated_at: String,
76}
77
78/// 获取自定义字段响应
79#[derive(Debug, Clone, Deserialize)]
80pub struct GetCustomFieldResponse {
81    /// 字段 GUID
82    pub field_guid: String,
83    /// 字段名称
84    pub name: String,
85    /// 字段配置
86    pub config: CustomFieldConfig,
87    /// 创建时间
88    pub created_at: String,
89    /// 更新时间
90    pub updated_at: String,
91}
92
93/// 更新自定义字段响应
94#[derive(Debug, Clone, Deserialize)]
95pub struct UpdateCustomFieldResponse {
96    /// 字段 GUID
97    pub field_guid: String,
98    /// 字段名称
99    pub name: String,
100    /// 字段配置
101    pub config: CustomFieldConfig,
102    /// 更新时间
103    pub updated_at: String,
104}
105
106/// 删除自定义字段响应
107#[derive(Debug, Clone, Deserialize)]
108pub struct DeleteCustomFieldResponse {
109    /// 是否删除成功
110    pub success: bool,
111    /// 字段 GUID
112    pub field_guid: String,
113}
114
115/// 自定义字段列表项
116#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
117pub struct CustomFieldItem {
118    /// 字段 GUID
119    pub field_guid: String,
120    /// 字段名称
121    pub name: String,
122    /// 字段配置
123    pub config: CustomFieldConfig,
124    /// 创建时间
125    pub created_at: String,
126    /// 更新时间
127    pub updated_at: String,
128}
129
130/// 获取自定义字段列表响应
131#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
132pub struct ListCustomFieldsResponse {
133    /// 是否还有更多项
134    #[serde(default)]
135    pub has_more: bool,
136    /// 分页标记
137    #[serde(skip_serializing_if = "Option::is_none")]
138    pub page_token: Option<String>,
139    /// 总数
140    #[serde(skip_serializing_if = "Option::is_none")]
141    pub total: Option<i32>,
142    /// 列表项
143    #[serde(default)]
144    pub items: Vec<CustomFieldItem>,
145}
146
147#[cfg(test)]
148#[allow(unused_imports)]
149mod tests {
150
151    #[test]
152    fn test_serialization_roundtrip() {
153        // 基础序列化测试
154        let json = r#"{"test": "value"}"#;
155        assert!(serde_json::from_str::<serde_json::Value>(json).is_ok());
156    }
157
158    #[test]
159    fn test_deserialization_from_json() {
160        // 基础反序列化测试
161        let json = r#"{"field": "data"}"#;
162        let value: serde_json::Value = serde_json::from_str(json).expect("JSON 反序列化失败");
163        assert_eq!(value["field"], "data");
164    }
165}