open_lark/card/components/content_components/
note.rs1use serde::{Deserialize, Serialize};
2
3use crate::card::{
4 components::content_components::{image::FeishuCardImage, plain_text::PlainText},
5 icon::FeishuCardTextIcon,
6};
7
8#[derive(Debug, Serialize, Deserialize)]
10pub struct FeishuCardNote {
11 tag: String,
13 elements: Vec<FeishuCardNoteElement>,
15}
16
17impl Default for FeishuCardNote {
18 fn default() -> Self {
19 FeishuCardNote {
20 tag: "note".to_string(),
21 elements: vec![],
22 }
23 }
24}
25
26#[derive(Debug, Serialize, Deserialize)]
28#[serde(untagged)]
29pub enum FeishuCardNoteElement {
30 Icon(FeishuCardTextIcon),
31 Image(Box<FeishuCardImage>),
32 Text(PlainText),
33}
34
35impl FeishuCardNote {
36 pub fn new() -> Self {
37 FeishuCardNote::default()
38 }
39
40 pub fn icon(mut self, icon: FeishuCardTextIcon) -> Self {
41 self.elements.push(FeishuCardNoteElement::Icon(icon));
42 self
43 }
44
45 pub fn image(mut self, image: FeishuCardImage) -> Self {
46 self.elements
47 .push(FeishuCardNoteElement::Image(Box::new(image)));
48 self
49 }
50
51 pub fn text(mut self, text: PlainText) -> Self {
52 self.elements.push(FeishuCardNoteElement::Text(text));
53 self
54 }
55}
56
57#[cfg(test)]
58mod test {
59 #[test]
60 fn test_note() {
61 use crate::card::components::content_components::note::*;
62 use serde_json::json;
63
64 let note = FeishuCardNote::new()
65 .icon(
66 FeishuCardTextIcon::new()
67 .tag("custom_icon")
68 .token("chat-forbidden_outlined")
69 .img_key("img_v2_041b28e3-5680-48c2-9af2-497ace79333g"),
70 )
71 .text(PlainText::text("备注信息1"))
72 .image(
73 FeishuCardImage::new()
74 .img_key("img_v2_041b28e3-5680-48c2-9af2-497ace79333g")
75 .alt(PlainText::text("这是备注图片"))
76 .build(),
77 )
78 .text(PlainText::text("备注信息2"));
79 let json = json!({
80 "tag": "note",
81 "elements": [
82 {
83 "tag": "custom_icon",
84 "token": "chat-forbidden_outlined",
85 "img_key": "img_v2_041b28e3-5680-48c2-9af2-497ace79333g"
86 },
87 {
88 "tag": "plain_text",
89 "content": "备注信息1"
90 },
91 {
92 "tag": "img",
93 "img_key": "img_v2_041b28e3-5680-48c2-9af2-497ace79333g",
94 "alt": {
95 "tag": "plain_text",
96 "content": "这是备注图片"
97 }
98 },
99 {
100 "tag": "plain_text",
101 "content": "备注信息2"
102 }
103 ]
104 });
105
106 assert_eq!(serde_json::to_value(¬e).unwrap(), json);
107 }
108}