use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::card::components::content_components::plain_text::PlainText;
#[derive(Debug, Serialize, Deserialize)]
pub struct FeishuCardInput {
tag: String,
#[serde(skip_serializing_if = "Option::is_none")]
name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
required: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
disabled: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
placeholder: Option<PlainText>,
#[serde(skip_serializing_if = "Option::is_none")]
default_value: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
width: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
max_length: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
label: Option<PlainText>,
#[serde(skip_serializing_if = "Option::is_none")]
label_position: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
value: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
confirm: Option<InputConfirm>,
#[serde(skip_serializing_if = "Option::is_none")]
fallback: Option<InputFallback>,
}
impl Default for FeishuCardInput {
fn default() -> Self {
FeishuCardInput {
tag: "input".to_string(),
name: None,
required: None,
disabled: None,
placeholder: None,
default_value: None,
width: None,
max_length: None,
label: None,
label_position: None,
value: None,
confirm: None,
fallback: None,
}
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct InputConfirm {
title: PlainText,
text: PlainText,
}
impl InputConfirm {
pub fn new(title: &str, text: &str) -> Self {
InputConfirm {
title: PlainText::text(title),
text: PlainText::text(text),
}
}
pub fn title(mut self, title: PlainText) -> Self {
self.title = title;
self
}
pub fn text(mut self, text: PlainText) -> Self {
self.text = text;
self
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct InputFallback {
tag: String,
text: PlainText,
}
impl Default for InputFallback {
fn default() -> Self {
InputFallback {
tag: "fallback_text".to_string(),
text: PlainText::default(),
}
}
}
impl InputFallback {
pub fn new() -> Self {
Self::default()
}
pub fn text(mut self, text: PlainText) -> Self {
self.text = text;
self
}
}
impl FeishuCardInput {
pub fn new() -> Self {
FeishuCardInput::default()
}
pub fn name(mut self, name: &str) -> Self {
self.name = Some(name.to_string());
self
}
pub fn required(mut self, required: bool) -> Self {
self.required = Some(required);
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = Some(disabled);
self
}
pub fn placeholder(mut self, placeholder: PlainText) -> Self {
self.placeholder = Some(placeholder);
self
}
pub fn default_value(mut self, default_value: &str) -> Self {
self.default_value = Some(default_value.to_string());
self
}
pub fn width(mut self, width: &str) -> Self {
self.width = Some(width.to_string());
self
}
pub fn max_length(mut self, max_length: u32) -> Self {
self.max_length = Some(max_length);
self
}
pub fn label(mut self, label: PlainText) -> Self {
self.label = Some(label);
self
}
pub fn label_position(mut self, label_position: &str) -> Self {
self.label_position = Some(label_position.to_string());
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 fallback(mut self, fallback: InputFallback) -> Self {
self.fallback = Some(fallback);
self
}
pub fn build(self) -> FeishuCardInput {
self
}
}
#[cfg(test)]
mod tests {
use serde_json::json;
use crate::card::components::content_components::plain_text::PlainText;
use super::*;
#[test]
fn test_input_builder() {
let input = FeishuCardInput::new()
.name("input1")
.required(false)
.disabled(false)
.placeholder(PlainText::text("请输入"))
.default_value("demo")
.width("default")
.max_length(5)
.label(PlainText::text("请输入文本:"))
.label_position("left")
.value(json!({"k": "v"}))
.confirm(InputConfirm::new("title", "content"))
.fallback(InputFallback::new().text(PlainText::text("自定义声明")));
let json = json!({
"tag": "input", "name": "input1", "required": false, "disabled": false, "placeholder": {
"tag": "plain_text",
"content": "请输入"
},
"default_value": "demo", "width": "default", "max_length": 5, "label": {
"tag": "plain_text",
"content": "请输入文本:"
},
"label_position": "left", "value": {
"k": "v"
},
"confirm": {
"title": {
"tag": "plain_text",
"content": "title"
},
"text": {
"tag": "plain_text",
"content": "content"
}
},
"fallback": {
"tag": "fallback_text", "text": {
"content": "自定义声明", "tag": "plain_text" }
}
});
assert_eq!(serde_json::to_value(input).unwrap(), json);
}
}