open_lark/card/components/interactive_components/
overflow.rs1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4use crate::card::{
5 components::{
6 content_components::plain_text::PlainText, interactive_components::input::InputConfirm,
7 },
8 href::FeishuCardHrefVal,
9};
10
11#[derive(Debug, Serialize, Deserialize)]
13pub struct FeishuCardOverflow {
14 tag: String,
16 #[serde(skip_serializing_if = "Option::is_none")]
22 width: Option<String>,
23 options: Vec<OverflowOption>,
25 #[serde(skip_serializing_if = "Option::is_none")]
28 value: Option<Value>,
29 #[serde(skip_serializing_if = "Option::is_none")]
34 confirm: Option<InputConfirm>,
35}
36
37impl Default for FeishuCardOverflow {
38 fn default() -> Self {
39 Self {
40 tag: "overflow".to_string(),
41 width: None,
42 options: vec![],
43 value: None,
44 confirm: None,
45 }
46 }
47}
48
49impl FeishuCardOverflow {
50 pub fn new() -> Self {
51 Self::default()
52 }
53
54 pub fn width(mut self, width: &str) -> Self {
55 self.width = Some(width.to_string());
56 self
57 }
58
59 pub fn options(mut self, options: Vec<OverflowOption>) -> Self {
60 self.options = options;
61 self
62 }
63
64 pub fn value(mut self, value: Value) -> Self {
65 self.value = Some(value);
66 self
67 }
68
69 pub fn confirm(mut self, confirm: InputConfirm) -> Self {
70 self.confirm = Some(confirm);
71 self
72 }
73
74 pub fn add_option(mut self, option: OverflowOption) -> Self {
75 self.options.push(option);
76 self
77 }
78}
79
80#[derive(Debug, Serialize, Deserialize, Default)]
82pub struct OverflowOption {
83 text: Option<PlainText>,
85 multi_url: Option<FeishuCardHrefVal>,
87 value: Option<String>,
89}
90
91impl OverflowOption {
92 pub fn new() -> Self {
93 Self::default()
94 }
95
96 pub fn text(mut self, text: PlainText) -> Self {
97 self.text = Some(text);
98 self
99 }
100
101 pub fn multi_url(mut self, multi_url: FeishuCardHrefVal) -> Self {
102 self.multi_url = Some(multi_url);
103 self
104 }
105
106 pub fn value(mut self, value: impl ToString) -> Self {
107 self.value = Some(value.to_string());
108 self
109 }
110}
111
112#[cfg(test)]
113mod test {
114 use serde_json::json;
115
116 use crate::card::{
117 components::{
118 content_components::plain_text::PlainText,
119 interactive_components::{
120 input::InputConfirm,
121 overflow::{FeishuCardOverflow, OverflowOption},
122 },
123 },
124 href::FeishuCardHrefVal,
125 };
126
127 #[test]
128 fn test_overflow() {
129 let overflow = FeishuCardOverflow::new()
130 .width("fill")
131 .options(vec![OverflowOption::new()
132 .text(PlainText::text("这是一个链接跳转"))
133 .multi_url(
134 FeishuCardHrefVal::new().url("https://open.feishu.cn/document/home/index"),
135 )
136 .value("document")])
137 .value(json!({"key_1": "value_1"}))
138 .confirm(InputConfirm::new("title", "content"));
139
140 let json = json!({
141 "tag": "overflow",
142 "width": "fill", "options": [
144 { "text": {
147 "tag": "plain_text", "content": "这是一个链接跳转" },
150 "multi_url": { "url": "https://open.feishu.cn/document/home/index", },
153 "value": "document" }
155 ],
156 "value": {
157 "key_1": "value_1"
159 },
160 "confirm": {
161 "title": {
163 "tag": "plain_text",
164 "content": "title"
165 },
166 "text": {
167 "tag": "plain_text",
168 "content": "content"
169 }
170 }
171 });
172
173 assert_eq!(serde_json::to_value(&overflow).unwrap(), json);
174 }
175}