1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use serde::{Deserialize, Serialize};

use crate::card::{
    components::content_components::{image::FeishuCardImage, plain_text::PlainText},
    icon::FeishuCardTextIcon,
};

/// 备注
#[derive(Debug, Serialize, Deserialize)]
pub struct FeishuCardNote {
    /// 组件的标签。备注模块组件的固定值为 note。
    tag: String,
    /// 配置卡片的备注模块信息。支持添加图标、图片以及文本。
    elements: Vec<FeishuCardNoteElement>,
}

impl Default for FeishuCardNote {
    fn default() -> Self {
        FeishuCardNote {
            tag: "note".to_string(),
            elements: vec![],
        }
    }
}

/// 备注组件支持添加图标、图片以及文本
#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum FeishuCardNoteElement {
    Icon(FeishuCardTextIcon),
    Image(Box<FeishuCardImage>),
    Text(PlainText),
}

impl FeishuCardNote {
    pub fn new() -> Self {
        FeishuCardNote::default()
    }

    pub fn icon(mut self, icon: FeishuCardTextIcon) -> Self {
        self.elements.push(FeishuCardNoteElement::Icon(icon));
        self
    }

    pub fn image(mut self, image: FeishuCardImage) -> Self {
        self.elements
            .push(FeishuCardNoteElement::Image(Box::new(image)));
        self
    }

    pub fn text(mut self, text: PlainText) -> Self {
        self.elements.push(FeishuCardNoteElement::Text(text));
        self
    }
}

#[cfg(test)]
mod test {
    #[test]
    fn test_note() {
        use crate::card::components::content_components::note::*;
        use serde_json::json;

        let note = FeishuCardNote::new()
            .icon(
                FeishuCardTextIcon::new()
                    .tag("custom_icon")
                    .token("chat-forbidden_outlined")
                    .img_key("img_v2_041b28e3-5680-48c2-9af2-497ace79333g"),
            )
            .text(PlainText::text("备注信息1"))
            .image(
                FeishuCardImage::new()
                    .img_key("img_v2_041b28e3-5680-48c2-9af2-497ace79333g")
                    .alt(PlainText::text("这是备注图片"))
                    .build(),
            )
            .text(PlainText::text("备注信息2"));
        let json = json!({
          "tag": "note",
          "elements": [
            {
              "tag": "custom_icon",
              "token": "chat-forbidden_outlined",
              "img_key": "img_v2_041b28e3-5680-48c2-9af2-497ace79333g"
            },
            {
              "tag": "plain_text",
              "content": "备注信息1"
            },
            {
              "tag": "img",
              "img_key": "img_v2_041b28e3-5680-48c2-9af2-497ace79333g",
              "alt": {
                "tag": "plain_text",
                "content": "这是备注图片"
              }
            },
            {
              "tag": "plain_text",
              "content": "备注信息2"
            }
          ]
        });

        assert_eq!(serde_json::to_value(&note).unwrap(), json);
    }
}