use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Serialize, Deserialize)]
pub struct FeishuCardChart {
tag: String,
#[serde(skip_serializing_if = "Option::is_none")]
aspect_ratio: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
color_theme: Option<String>,
chart_spec: Value,
#[serde(skip_serializing_if = "Option::is_none")]
preview: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
height: Option<String>,
}
impl Default for FeishuCardChart {
fn default() -> Self {
Self {
tag: "chart".to_string(),
aspect_ratio: None,
color_theme: None,
chart_spec: Value::Null,
preview: None,
height: None,
}
}
}
impl FeishuCardChart {
pub fn new() -> Self {
FeishuCardChart::default()
}
pub fn aspect_ratio(mut self, aspect_ratio: &str) -> Self {
self.aspect_ratio = Some(aspect_ratio.to_string());
self
}
pub fn color_theme(mut self, color_theme: &str) -> Self {
self.color_theme = Some(color_theme.to_string());
self
}
pub fn chart_spec(mut self, chart_spec: Value) -> Self {
self.chart_spec = chart_spec;
self
}
pub fn preview(mut self, preview: bool) -> Self {
self.preview = Some(preview);
self
}
pub fn height(mut self, height: &str) -> Self {
self.height = Some(height.to_string());
self
}
}
#[cfg(test)]
mod test {
use serde_json::json;
use crate::card::components::content_components::chart::FeishuCardChart;
#[test]
fn test_chart() {
let chart = FeishuCardChart::new()
.aspect_ratio("1:1")
.color_theme("brand")
.chart_spec(json!({
"series": [
{
"type": "bar",
"data": [1, 2, 3, 4, 5]
}
]
}))
.preview(true)
.height("auto");
let json = json!({
"tag": "chart",
"aspect_ratio": "1:1",
"color_theme": "brand",
"chart_spec": {
"series": [
{
"type": "bar",
"data": [1, 2, 3, 4, 5]
}
]
},
"preview": true,
"height": "auto"
});
assert_eq!(serde_json::to_value(&chart).unwrap(), json);
}
}