use serde::{Deserialize, Serialize};
use crate::card::components::{interactive_components::input::InputConfirm, CardElement};
#[derive(Debug, Serialize, Deserialize)]
pub struct FormContainer {
tag: String,
name: String,
elements: Vec<CardElement>,
#[serde(skip_serializing_if = "Option::is_none")]
r#type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
confirm: Option<InputConfirm>,
}
impl Default for FormContainer {
fn default() -> Self {
FormContainer {
tag: "form".to_string(),
name: "".to_string(),
elements: vec![],
r#type: None,
confirm: None,
}
}
}
impl FormContainer {
pub fn new() -> Self {
FormContainer::default()
}
pub fn name(mut self, name: &str) -> Self {
self.name = name.to_string();
self
}
pub fn elements(mut self, elements: Vec<CardElement>) -> Self {
self.elements = elements;
self
}
pub fn r#type(mut self, r#type: &str) -> Self {
self.r#type = Some(r#type.to_string());
self
}
pub fn confirm(mut self, confirm: InputConfirm) -> Self {
self.confirm = Some(confirm);
self
}
}
#[cfg(test)]
mod test {
use serde_json::json;
use crate::card::components::{
content_components::plain_text::PlainText,
interactive_components::{button::FeishuCardButton, input::FeishuCardInput},
CardElement,
};
use super::*;
#[test]
fn test_form_container() {
let form = FormContainer::new().name("form_1").elements(vec![
CardElement::InputForm(FeishuCardInput::new().name("reason").required(true)),
CardElement::Button(
FeishuCardButton::new()
.action_type("form_submit")
.name("submit")
.r#type("primary")
.text(PlainText::text("提交").tag("lark_md")),
),
]);
let expect = json!( {
"tag": "form", "name": "form_1", "elements": [
{
"tag": "input", "name": "reason", "required": true },
{
"tag": "button", "action_type": "form_submit", "name": "submit", "text": { "content": "提交",
"tag": "lark_md"
},
"type": "primary", }
]
});
assert_eq!(json!(form), expect);
}
}