use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Serialize, Deserialize)]
pub struct FeishuCardTable {
tag: String,
#[serde(skip_serializing_if = "Option::is_none")]
page_size: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
row_height: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
header_style: Option<FeishuCardTableHeaderStyle>,
columns: Vec<FeishuCardTableColumn>,
rows: Value,
}
impl Default for FeishuCardTable {
fn default() -> Self {
FeishuCardTable {
tag: "table".to_string(),
page_size: None,
row_height: None,
header_style: None,
columns: vec![],
rows: Value::Null,
}
}
}
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct FeishuCardTableHeaderStyle {
#[serde(skip_serializing_if = "Option::is_none")]
text_align: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
text_size: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
background_style: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
text_color: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
bold: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
lines: Option<i32>,
}
impl FeishuCardTableHeaderStyle {
pub fn new() -> Self {
Self::default()
}
pub fn text_align(mut self, text_align: &str) -> Self {
self.text_align = Some(text_align.to_string());
self
}
pub fn text_size(mut self, text_size: &str) -> Self {
self.text_size = Some(text_size.to_string());
self
}
pub fn background_style(mut self, background_style: &str) -> Self {
self.background_style = Some(background_style.to_string());
self
}
pub fn text_color(mut self, text_color: &str) -> Self {
self.text_color = Some(text_color.to_string());
self
}
pub fn bold(mut self, bold: bool) -> Self {
self.bold = Some(bold);
self
}
pub fn lines(mut self, lines: i32) -> Self {
self.lines = Some(lines);
self
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct FeishuCardTableColumn {
name: String,
#[serde(skip_serializing_if = "Option::is_none")]
display_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
width: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
horizontal_align: Option<String>,
data_type: String,
#[serde(skip_serializing_if = "Option::is_none")]
format: Option<FeishuCardTableColumnFormat>,
}
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct FeishuCardTableColumnFormat {
#[serde(skip_serializing_if = "Option::is_none")]
precision: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
symbol: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
separator: Option<bool>,
}
impl FeishuCardTableColumnFormat {
pub fn new() -> Self {
FeishuCardTableColumnFormat::default()
}
pub fn precision(mut self, precision: i32) -> Self {
self.precision = Some(precision);
self
}
pub fn symbol(mut self, symbol: &str) -> Self {
self.symbol = Some(symbol.to_string());
self
}
pub fn separator(mut self, separator: bool) -> Self {
self.separator = Some(separator);
self
}
}
impl Default for FeishuCardTableColumn {
fn default() -> Self {
FeishuCardTableColumn {
name: "".to_string(),
display_name: None,
width: None,
horizontal_align: None,
data_type: "".to_string(),
format: None,
}
}
}
impl FeishuCardTableColumn {
pub fn new() -> Self {
Self::default()
}
pub fn name(mut self, name: &str) -> Self {
self.name = name.to_string();
self
}
pub fn display_name(mut self, display_name: &str) -> Self {
self.display_name = Some(display_name.to_string());
self
}
pub fn width(mut self, width: &str) -> Self {
self.width = Some(width.to_string());
self
}
pub fn horizontal_align(mut self, horizontal_align: &str) -> Self {
self.horizontal_align = Some(horizontal_align.to_string());
self
}
pub fn data_type(mut self, data_type: &str) -> Self {
self.data_type = data_type.to_string();
self
}
pub fn format(mut self, format: FeishuCardTableColumnFormat) -> Self {
self.format = Some(format);
self
}
}
impl FeishuCardTable {
pub fn new() -> Self {
Self::default()
}
pub fn page_zie(mut self, page_zie: i32) -> Self {
self.page_size = Some(page_zie);
self
}
pub fn row_height(mut self, row_height: &str) -> Self {
self.row_height = Some(row_height.to_string());
self
}
pub fn header_style(mut self, header_style: FeishuCardTableHeaderStyle) -> Self {
self.header_style = Some(header_style);
self
}
pub fn columns(mut self, columns: Vec<FeishuCardTableColumn>) -> Self {
self.columns = columns;
self
}
pub fn rows(mut self, rows: Value) -> Self {
self.rows = rows;
self
}
}
#[cfg(test)]
mod test {
use serde_json::json;
use crate::card::components::content_components::table::{
FeishuCardTable, FeishuCardTableColumn, FeishuCardTableColumnFormat,
FeishuCardTableHeaderStyle,
};
#[test]
fn test_table() {
let table = FeishuCardTable::new()
.page_zie(1)
.row_height("middle")
.header_style(
FeishuCardTableHeaderStyle::new()
.bold(true)
.background_style("grey")
.lines(1)
.text_size("heading")
.text_align("center")
,
)
.columns(vec![
FeishuCardTableColumn::new()
.name("customer_name")
.display_name("客户名称")
.data_type("text"),
FeishuCardTableColumn::new()
.name("customer_scale")
.display_name("客户规模")
.data_type("options")
.width("90px"),
FeishuCardTableColumn::new()
.name("customer_arr")
.display_name("ARR(万元)")
.data_type("number")
.format(
FeishuCardTableColumnFormat::new()
.precision(2)
.symbol("¥"),
)
.width("120px"),
FeishuCardTableColumn::new()
.name("customer_year")
.display_name("签约年限")
.data_type("text"),
])
.rows(json!([
{
"customer_name": "飞书消息卡片是飞书中的一种功能,它允许用户通过机器人或应用以结构化(JSON)的方式发送和接收消息。",
"customer_scale": [
{
"text": "S2",
"color": "green"
}
],
"customer_arr": 26.57774928467545,
"customer_year": "2年"
}
]));
let json = json!( {
"tag": "table",
"page_size": 1,
"row_height": "middle",
"header_style": {
"bold": true,
"background_style": "grey",
"lines": 1,
"text_size": "heading",
"text_align": "center"
},
"columns": [
{
"name": "customer_name",
"display_name": "客户名称",
"data_type": "text"
},
{
"name": "customer_scale",
"display_name": "客户规模",
"data_type": "options",
"width": "90px"
},
{
"name": "customer_arr",
"display_name": "ARR(万元)",
"data_type": "number",
"format": {
"symbol": "¥",
"precision": 2
},
"width": "120px"
},
{
"name": "customer_year",
"display_name": "签约年限",
"data_type": "text"
}
],
"rows": [
{
"customer_name": "飞书消息卡片是飞书中的一种功能,它允许用户通过机器人或应用以结构化(JSON)的方式发送和接收消息。",
"customer_scale": [
{
"text": "S2",
"color": "green"
}
],
"customer_arr": 26.57774928467545,
"customer_year": "2年"
}
]
});
assert_eq!(serde_json::to_value(&table).unwrap(), json);
}
}