use serde::{Deserialize, Serialize};
use crate::card::{
components::{
content_components::plain_text::PlainText, interactive_components::input::InputConfirm,
CardElement,
},
interactions::Behaviors,
};
#[derive(Debug, Serialize, Deserialize)]
pub struct InteractiveContainer {
tag: String,
#[serde(skip_serializing_if = "Option::is_none")]
width: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
height: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
background_style: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
has_border: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
border_color: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
corner_radius: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
padding: Option<String>,
behaviors: Vec<Behaviors>,
#[serde(skip_serializing_if = "Option::is_none")]
hover_tips: Option<PlainText>,
#[serde(skip_serializing_if = "Option::is_none")]
disabled: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
disabled_tips: Option<PlainText>,
#[serde(skip_serializing_if = "Option::is_none")]
confirm: Option<InputConfirm>,
elements: Vec<CardElement>,
}
impl Default for InteractiveContainer {
fn default() -> Self {
InteractiveContainer {
tag: "interactive_container".to_string(),
width: None,
height: None,
background_style: None,
has_border: None,
border_color: None,
corner_radius: None,
padding: None,
behaviors: vec![],
hover_tips: None,
disabled: None,
disabled_tips: None,
confirm: None,
elements: vec![],
}
}
}
impl InteractiveContainer {
pub fn new() -> Self {
InteractiveContainer::default()
}
pub fn width(mut self, width: &str) -> Self {
self.width = Some(width.to_string());
self
}
pub fn height(mut self, height: &str) -> Self {
self.height = Some(height.to_string());
self
}
pub fn background_style(mut self, background_style: &str) -> Self {
self.background_style = Some(background_style.to_string());
self
}
pub fn has_border(mut self, has_border: bool) -> Self {
self.has_border = Some(has_border);
self
}
pub fn border_color(mut self, border_color: &str) -> Self {
self.border_color = Some(border_color.to_string());
self
}
pub fn corner_radius(mut self, corner_radius: &str) -> Self {
self.corner_radius = Some(corner_radius.to_string());
self
}
pub fn padding(mut self, padding: &str) -> Self {
self.padding = Some(padding.to_string());
self
}
pub fn behaviors(mut self, behaviors: Vec<Behaviors>) -> Self {
self.behaviors = behaviors;
self
}
pub fn hover_tips(mut self, hover_tips: PlainText) -> Self {
self.hover_tips = Some(hover_tips);
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = Some(disabled);
self
}
pub fn disabled_tips(mut self, disabled_tips: PlainText) -> Self {
self.disabled_tips = Some(disabled_tips);
self
}
pub fn confirm(mut self, confirm: InputConfirm) -> Self {
self.confirm = Some(confirm);
self
}
pub fn elements(mut self, elements: Vec<CardElement>) -> Self {
self.elements = elements;
self
}
}
#[cfg(test)]
mod test {
use serde_json::json;
use crate::card::{
components::{
containers::interactive::InteractiveContainer,
content_components::plain_text::PlainText,
},
interactions::{Behaviors, CallbackBehavior, FormBehavior, OpenUrlBehavior},
};
#[test]
fn test_interactive() {
let interactive = InteractiveContainer::new()
.width("fill")
.height("auto")
.background_style("default")
.has_border(false)
.border_color("grey")
.corner_radius("40px")
.padding("10px 20px 10px 20px")
.behaviors(vec![
Behaviors::OpenUrl(
OpenUrlBehavior::new("https://www.baidu.com")
.android_url("https://developer.android.com/")
.ios_url("lark://msgcard/unsupported_action")
.pc_url("https://www.windows.com"),
),
Behaviors::Callback(CallbackBehavior::new(json!({
"key": "value"
}))),
Behaviors::Form(FormBehavior::new().behavior("submit")),
])
.hover_tips(PlainText::text("demo"))
.disabled(false)
.disabled_tips(PlainText::text("demo"))
.elements(vec![]);
let expect = json!({
"tag": "interactive_container", "width": "fill", "height": "auto", "background_style": "default", "has_border": false, "border_color": "grey", "corner_radius": "40px", "padding": "10px 20px 10px 20px", "behaviors": [
{
"type": "open_url", "default_url": "https://www.baidu.com", "android_url": "https://developer.android.com/", "ios_url": "lark://msgcard/unsupported_action", "pc_url": "https://www.windows.com" },
{
"type": "callback", "value": {
"key": "value"
}
},
{
"type": "form_action", "behavior": "submit" }
],
"disabled": false,
"disabled_tips": { "tag": "plain_text", "content": "demo" },
"hover_tips": {
"tag": "plain_text",
"content": "demo"
},
"elements": [] });
assert_eq!(json!(interactive), expect);
}
}