use serde::{Deserialize, Serialize};
use crate::card::icon::FeishuCardTextIcon;
#[derive(Debug, Serialize, Deserialize)]
pub struct FeishuCardText {
tag: String,
#[serde(skip_serializing_if = "Option::is_none")]
text: Option<PlainText>,
#[serde(skip_serializing_if = "Option::is_none")]
icon: Option<FeishuCardTextIcon>,
}
impl Default for FeishuCardText {
fn default() -> Self {
FeishuCardText {
tag: "div".to_string(),
text: None,
icon: None,
}
}
}
impl FeishuCardText {
pub fn new() -> Self {
FeishuCardText::default()
}
pub fn text(mut self, text: PlainText) -> Self {
self.text = Some(text);
self
}
pub fn icon(mut self, icon: FeishuCardTextIcon) -> Self {
self.icon = Some(icon);
self
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct PlainText {
tag: String,
content: String,
#[serde(skip_serializing_if = "Option::is_none")]
text_size: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
text_color: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
text_align: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
lines: Option<i32>,
}
impl Default for PlainText {
fn default() -> Self {
PlainText {
tag: "plain_text".to_string(),
content: "".to_string(),
text_size: None,
text_color: None,
text_align: None,
lines: None,
}
}
}
impl PlainText {
pub fn text(content: &str) -> Self {
Self {
tag: "plain_text".to_string(),
content: content.to_string(),
text_size: None,
text_color: None,
text_align: None,
lines: None,
}
}
pub fn markdown(content: &str) -> Self {
Self {
tag: "lark_md".to_string(),
content: content.to_string(),
text_size: None,
text_color: None,
text_align: None,
lines: None,
}
}
pub fn tag(mut self, tag: &str) -> Self {
self.tag = tag.to_string();
self
}
pub fn content(mut self, content: &str) -> Self {
self.content = content.to_string();
self
}
pub fn text_size(mut self, text_size: &str) -> Self {
self.text_size = Some(text_size.to_string());
self
}
pub fn text_color(mut self, text_color: &str) -> Self {
self.text_color = Some(text_color.to_string());
self
}
pub fn text_align(mut self, text_align: &str) -> Self {
self.text_align = Some(text_align.to_string());
self
}
pub fn lines(mut self, lines: i32) -> Self {
self.lines = Some(lines);
self
}
}
#[cfg(test)]
mod test {
use serde_json::json;
#[test]
fn test_message_card_text() {
use super::*;
let text = FeishuCardText::new()
.text(
PlainText::text("这是一段普通文本示例。")
.text_size("cus-0")
.text_align("center")
.text_color("default"),
)
.icon(
FeishuCardTextIcon::new()
.token("app-default_filled")
.color("blue"),
);
let json = json!({
"tag": "div",
"text": {
"tag": "plain_text",
"content": "这是一段普通文本示例。",
"text_size": "cus-0",
"text_align": "center",
"text_color": "default"
},
"icon": {
"tag": "standard_icon",
"token": "app-default_filled",
"color": "blue"
}
}
);
assert_eq!(json, serde_json::to_value(&text).unwrap());
}
}