use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::card::{
components::{
content_components::plain_text::PlainText, interactive_components::input::InputConfirm,
},
href::FeishuCardHrefVal,
};
#[derive(Debug, Serialize, Deserialize)]
pub struct FeishuCardOverflow {
tag: String,
#[serde(skip_serializing_if = "Option::is_none")]
width: Option<String>,
options: Vec<OverflowOption>,
#[serde(skip_serializing_if = "Option::is_none")]
value: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
confirm: Option<InputConfirm>,
}
impl Default for FeishuCardOverflow {
fn default() -> Self {
Self {
tag: "overflow".to_string(),
width: None,
options: vec![],
value: None,
confirm: None,
}
}
}
impl FeishuCardOverflow {
pub fn new() -> Self {
Self::default()
}
pub fn width(mut self, width: &str) -> Self {
self.width = Some(width.to_string());
self
}
pub fn options(mut self, options: Vec<OverflowOption>) -> Self {
self.options = options;
self
}
pub fn value(mut self, value: Value) -> Self {
self.value = Some(value);
self
}
pub fn confirm(mut self, confirm: InputConfirm) -> Self {
self.confirm = Some(confirm);
self
}
pub fn add_option(mut self, option: OverflowOption) -> Self {
self.options.push(option);
self
}
}
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct OverflowOption {
text: Option<PlainText>,
multi_url: Option<FeishuCardHrefVal>,
value: Option<String>,
}
impl OverflowOption {
pub fn new() -> Self {
Self::default()
}
pub fn text(mut self, text: PlainText) -> Self {
self.text = Some(text);
self
}
pub fn multi_url(mut self, multi_url: FeishuCardHrefVal) -> Self {
self.multi_url = Some(multi_url);
self
}
pub fn value(mut self, value: impl ToString) -> Self {
self.value = Some(value.to_string());
self
}
}
#[cfg(test)]
mod test {
use serde_json::json;
use crate::card::{
components::{
content_components::plain_text::PlainText,
interactive_components::{
input::InputConfirm,
overflow::{FeishuCardOverflow, OverflowOption},
},
},
href::FeishuCardHrefVal,
};
#[test]
fn test_overflow() {
let overflow = FeishuCardOverflow::new()
.width("fill")
.options(vec![OverflowOption::new()
.text(PlainText::text("这是一个链接跳转"))
.multi_url(
FeishuCardHrefVal::new().url("https://open.feishu.cn/document/home/index"),
)
.value("document")])
.value(json!({"key_1": "value_1"}))
.confirm(InputConfirm::new("title", "content"));
let json = json!({
"tag": "overflow",
"width": "fill", "options": [
{ "text": {
"tag": "plain_text", "content": "这是一个链接跳转" },
"multi_url": { "url": "https://open.feishu.cn/document/home/index", },
"value": "document" }
],
"value": {
"key_1": "value_1"
},
"confirm": {
"title": {
"tag": "plain_text",
"content": "title"
},
"text": {
"tag": "plain_text",
"content": "content"
}
}
});
assert_eq!(serde_json::to_value(&overflow).unwrap(), json);
}
}