open_lark/card/components/content_components/
multi_image_layout.rs1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Serialize, Deserialize)]
4pub struct FeishuCardMultiImageLayout {
5 tag: String,
7 combination_mode: String,
20 #[serde(skip_serializing_if = "Option::is_none")]
25 corner_radius: Option<String>,
26 img_list: Vec<RawImage>,
28}
29
30impl Default for FeishuCardMultiImageLayout {
31 fn default() -> Self {
32 FeishuCardMultiImageLayout {
33 tag: "img_combination".to_string(),
34 combination_mode: "".to_string(),
35 corner_radius: None,
36 img_list: vec![],
37 }
38 }
39}
40
41#[derive(Debug, Serialize, Deserialize)]
42struct RawImage {
43 img_key: String,
44}
45
46impl FeishuCardMultiImageLayout {
47 pub fn new() -> Self {
48 FeishuCardMultiImageLayout::default()
49 }
50
51 pub fn combination_mode(mut self, combination_mode: &str) -> Self {
52 self.combination_mode = combination_mode.to_string();
53 self
54 }
55
56 pub fn corner_radius(mut self, corner_radius: &str) -> Self {
57 self.corner_radius = Some(corner_radius.to_string());
58 self
59 }
60
61 pub fn img_list(mut self, img_list: Vec<&str>) -> Self {
62 self.img_list = img_list
63 .iter()
64 .map(|img_key| RawImage {
65 img_key: img_key.to_string(),
66 })
67 .collect();
68 self
69 }
70}
71
72#[cfg(test)]
73mod test {
74 use serde_json::json;
75
76 use super::*;
77
78 #[test]
79 fn test_multi_image_layout() {
80 let multi_image_layout = FeishuCardMultiImageLayout::new()
81 .combination_mode("trisect")
82 .img_list(vec![
83 "img_v2_4c772db0-9aff-4eba-bbf4-6e6121cabcef",
84 "img_v2_4c772db0-9aff-4eba-bbf4-6e6121cabcef",
85 "img_v2_4c772db0-9aff-4eba-bbf4-6e6121cabcef",
86 ]);
87
88 assert_eq!(
89 serde_json::to_value(multi_image_layout).unwrap(),
90 json!({
91 "tag": "img_combination",
92 "combination_mode": "trisect",
93 "img_list": [
94 {
95 "img_key": "img_v2_4c772db0-9aff-4eba-bbf4-6e6121cabcef"
96 },
97 {
98 "img_key": "img_v2_4c772db0-9aff-4eba-bbf4-6e6121cabcef"
99 },
100 {
101 "img_key": "img_v2_4c772db0-9aff-4eba-bbf4-6e6121cabcef"
102 }
103 ]
104 })
105 );
106 }
107}