use serde::{Deserialize, Serialize};
use crate::card::icon::FeishuCardTextIcon;
#[derive(Debug, Serialize, Deserialize)]
pub struct FeishuCardUserList {
tag: String,
#[serde(skip_serializing_if = "Option::is_none")]
lines: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
show_name: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
show_avatar: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
size: Option<String>,
persons: Vec<FeishuCardUserId>,
#[serde(skip_serializing_if = "Option::is_none")]
icon: Option<FeishuCardTextIcon>,
}
impl Default for FeishuCardUserList {
fn default() -> Self {
FeishuCardUserList {
tag: "person_list".to_string(),
lines: None,
show_name: None,
show_avatar: None,
size: None,
persons: vec![],
icon: None,
}
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct FeishuCardUserId {
id: String,
}
impl FeishuCardUserList {
pub fn new() -> Self {
FeishuCardUserList::default()
}
pub fn lines(mut self, lines: i32) -> Self {
self.lines = Some(lines);
self
}
pub fn show_name(mut self, show_name: bool) -> Self {
self.show_name = Some(show_name);
self
}
pub fn show_avatar(mut self, show_avatar: bool) -> Self {
self.show_avatar = Some(show_avatar);
self
}
pub fn size(mut self, size: &str) -> Self {
self.size = Some(size.to_string());
self
}
pub fn persons(mut self, persons: Vec<&str>) -> Self {
self.persons = persons
.iter()
.map(|id| FeishuCardUserId { id: id.to_string() })
.collect();
self
}
pub fn icon(mut self, icon: FeishuCardTextIcon) -> Self {
self.icon = Some(icon);
self
}
}
#[cfg(test)]
mod test {
use serde_json::json;
use crate::card::{
components::content_components::user_list::FeishuCardUserList, icon::FeishuCardTextIcon,
};
#[test]
fn test_user_list() {
let user_list = FeishuCardUserList::new()
.lines(1)
.show_name(true)
.show_avatar(true)
.size("small")
.persons(vec!["user_id"])
.icon(
FeishuCardTextIcon::new()
.tag("standard_icon")
.token("token")
.color("red"),
);
let json = json!({
"tag": "person_list",
"lines": 1,
"show_name": true,
"show_avatar": true,
"size": "small",
"persons": [{ "id": "user_id" }],
"icon": {
"tag": "standard_icon",
"token": "token",
"color": "red"
}
});
assert_eq!(serde_json::to_value(&user_list).unwrap(), json);
let user_list = FeishuCardUserList::new()
.lines(1)
.show_name(true)
.show_avatar(true)
.size("large")
.persons(vec![
"ou_0fdb0e7663af7128e7d9f8adeb2b689e",
"ou_47a09ae5a1353f3276924161dc63a2be",
])
.icon(
FeishuCardTextIcon::new()
.token("chat-forbidden_outlined")
.color("orange")
.img_key("img_v2_38811724"),
);
let json = json!({
"tag": "person_list",
"lines": 1, "show_name": true, "show_avatar": true, "size": "large", "persons": [
{ "id": "ou_0fdb0e7663af7128e7d9f8adeb2b689e" },
{ "id": "ou_47a09ae5a1353f3276924161dc63a2be" }
],
"icon": {
"tag": "standard_icon", "token": "chat-forbidden_outlined", "color": "orange", "img_key": "img_v2_38811724" }
});
assert_eq!(serde_json::to_value(&user_list).unwrap(), json)
}
}