open_lark/card/components/interactive_components/
input.rs1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4use crate::card::components::content_components::plain_text::PlainText;
5
6#[derive(Debug, Serialize, Deserialize)]
8pub struct FeishuCardInput {
9 tag: String,
11 #[serde(skip_serializing_if = "Option::is_none")]
16 name: Option<String>,
17 #[serde(skip_serializing_if = "Option::is_none")]
24 required: Option<bool>,
25 #[serde(skip_serializing_if = "Option::is_none")]
30 disabled: Option<bool>,
31 #[serde(skip_serializing_if = "Option::is_none")]
33 placeholder: Option<PlainText>,
34 #[serde(skip_serializing_if = "Option::is_none")]
36 default_value: Option<String>,
37 #[serde(skip_serializing_if = "Option::is_none")]
43 width: Option<String>,
44 #[serde(skip_serializing_if = "Option::is_none")]
47 max_length: Option<u32>,
48 #[serde(skip_serializing_if = "Option::is_none")]
50 label: Option<PlainText>,
51 #[serde(skip_serializing_if = "Option::is_none")]
57 label_position: Option<String>,
58 #[serde(skip_serializing_if = "Option::is_none")]
60 value: Option<Value>,
61 #[serde(skip_serializing_if = "Option::is_none")]
66 confirm: Option<InputConfirm>,
67 #[serde(skip_serializing_if = "Option::is_none")]
74 fallback: Option<InputFallback>,
75}
76
77impl Default for FeishuCardInput {
78 fn default() -> Self {
79 FeishuCardInput {
80 tag: "input".to_string(),
81 name: None,
82 required: None,
83 disabled: None,
84 placeholder: None,
85 default_value: None,
86 width: None,
87 max_length: None,
88 label: None,
89 label_position: None,
90 value: None,
91 confirm: None,
92 fallback: None,
93 }
94 }
95}
96
97#[derive(Debug, Serialize, Deserialize)]
98pub struct InputConfirm {
99 title: PlainText,
100 text: PlainText,
101}
102
103impl InputConfirm {
104 pub fn new(title: &str, text: &str) -> Self {
105 InputConfirm {
106 title: PlainText::text(title),
107 text: PlainText::text(text),
108 }
109 }
110
111 pub fn title(mut self, title: PlainText) -> Self {
112 self.title = title;
113 self
114 }
115
116 pub fn text(mut self, text: PlainText) -> Self {
117 self.text = text;
118 self
119 }
120}
121
122#[derive(Debug, Serialize, Deserialize)]
123pub struct InputFallback {
124 tag: String,
126 text: PlainText,
128}
129
130impl Default for InputFallback {
131 fn default() -> Self {
132 InputFallback {
133 tag: "fallback_text".to_string(),
134 text: PlainText::default(),
135 }
136 }
137}
138
139impl InputFallback {
140 pub fn new() -> Self {
141 Self::default()
142 }
143
144 pub fn text(mut self, text: PlainText) -> Self {
145 self.text = text;
146 self
147 }
148}
149
150impl FeishuCardInput {
151 pub fn new() -> Self {
152 FeishuCardInput::default()
153 }
154
155 pub fn name(mut self, name: &str) -> Self {
156 self.name = Some(name.to_string());
157 self
158 }
159
160 pub fn required(mut self, required: bool) -> Self {
161 self.required = Some(required);
162 self
163 }
164
165 pub fn disabled(mut self, disabled: bool) -> Self {
166 self.disabled = Some(disabled);
167 self
168 }
169
170 pub fn placeholder(mut self, placeholder: PlainText) -> Self {
171 self.placeholder = Some(placeholder);
172 self
173 }
174
175 pub fn default_value(mut self, default_value: &str) -> Self {
176 self.default_value = Some(default_value.to_string());
177 self
178 }
179
180 pub fn width(mut self, width: &str) -> Self {
181 self.width = Some(width.to_string());
182 self
183 }
184
185 pub fn max_length(mut self, max_length: u32) -> Self {
186 self.max_length = Some(max_length);
187 self
188 }
189
190 pub fn label(mut self, label: PlainText) -> Self {
191 self.label = Some(label);
192 self
193 }
194
195 pub fn label_position(mut self, label_position: &str) -> Self {
196 self.label_position = Some(label_position.to_string());
197 self
198 }
199
200 pub fn value(mut self, value: Value) -> Self {
201 self.value = Some(value);
202 self
203 }
204
205 pub fn confirm(mut self, confirm: InputConfirm) -> Self {
206 self.confirm = Some(confirm);
207 self
208 }
209
210 pub fn fallback(mut self, fallback: InputFallback) -> Self {
211 self.fallback = Some(fallback);
212 self
213 }
214
215 pub fn build(self) -> FeishuCardInput {
216 self
217 }
218}
219
220#[cfg(test)]
221mod tests {
222 use serde_json::json;
223
224 use crate::card::components::content_components::plain_text::PlainText;
225
226 use super::*;
227
228 #[test]
229 fn test_input_builder() {
230 let input = FeishuCardInput::new()
231 .name("input1")
232 .required(false)
233 .disabled(false)
234 .placeholder(PlainText::text("请输入"))
235 .default_value("demo")
236 .width("default")
237 .max_length(5)
238 .label(PlainText::text("请输入文本:"))
239 .label_position("left")
240 .value(json!({"k": "v"}))
241 .confirm(InputConfirm::new("title", "content"))
242 .fallback(InputFallback::new().text(PlainText::text("自定义声明")));
243
244 let json = json!({
245 "tag": "input", "name": "input1", "required": false, "disabled": false, "placeholder": {
250 "tag": "plain_text",
252 "content": "请输入"
253 },
254 "default_value": "demo", "width": "default", "max_length": 5, "label": {
258 "tag": "plain_text",
260 "content": "请输入文本:"
261 },
262 "label_position": "left", "value": {
264 "k": "v"
266 },
267 "confirm": {
268 "title": {
270 "tag": "plain_text",
271 "content": "title"
272 },
273 "text": {
274 "tag": "plain_text",
275 "content": "content"
276 }
277 },
278 "fallback": {
279 "tag": "fallback_text", "text": {
282 "content": "自定义声明", "tag": "plain_text" }
285 }
286 });
287
288 assert_eq!(serde_json::to_value(input).unwrap(), json);
289 }
290}