use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct FeishuCardMultiImageLayout {
tag: String,
combination_mode: String,
#[serde(skip_serializing_if = "Option::is_none")]
corner_radius: Option<String>,
img_list: Vec<RawImage>,
}
impl Default for FeishuCardMultiImageLayout {
fn default() -> Self {
FeishuCardMultiImageLayout {
tag: "img_combination".to_string(),
combination_mode: "".to_string(),
corner_radius: None,
img_list: vec![],
}
}
}
#[derive(Debug, Serialize, Deserialize)]
struct RawImage {
img_key: String,
}
impl FeishuCardMultiImageLayout {
pub fn new() -> Self {
FeishuCardMultiImageLayout::default()
}
pub fn combination_mode(mut self, combination_mode: &str) -> Self {
self.combination_mode = combination_mode.to_string();
self
}
pub fn corner_radius(mut self, corner_radius: &str) -> Self {
self.corner_radius = Some(corner_radius.to_string());
self
}
pub fn img_list(mut self, img_list: Vec<&str>) -> Self {
self.img_list = img_list
.iter()
.map(|img_key| RawImage {
img_key: img_key.to_string(),
})
.collect();
self
}
}
#[cfg(test)]
mod test {
use serde_json::json;
use super::*;
#[test]
fn test_multi_image_layout() {
let multi_image_layout = FeishuCardMultiImageLayout::new()
.combination_mode("trisect")
.img_list(vec![
"img_v2_4c772db0-9aff-4eba-bbf4-6e6121cabcef",
"img_v2_4c772db0-9aff-4eba-bbf4-6e6121cabcef",
"img_v2_4c772db0-9aff-4eba-bbf4-6e6121cabcef",
]);
assert_eq!(
serde_json::to_value(multi_image_layout).unwrap(),
json!({
"tag": "img_combination",
"combination_mode": "trisect",
"img_list": [
{
"img_key": "img_v2_4c772db0-9aff-4eba-bbf4-6e6121cabcef"
},
{
"img_key": "img_v2_4c772db0-9aff-4eba-bbf4-6e6121cabcef"
},
{
"img_key": "img_v2_4c772db0-9aff-4eba-bbf4-6e6121cabcef"
}
]
})
);
}
}