use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::card::components::content_components::plain_text::PlainText;
#[derive(Debug, Serialize, Deserialize)]
pub struct ImagePicker {
tag: String,
#[serde(skip_serializing_if = "Option::is_none")]
style: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
multi_select: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
layout: Option<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")]
can_preview: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
aspect_ratio: Option<String>,
#[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")]
value: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
options: Option<Vec<SelectImageOption>>,
}
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct SelectImageOption {
img_key: String,
#[serde(skip_serializing_if = "Option::is_none")]
value: Option<String>,
#[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")]
hover_tips: Option<PlainText>,
}
impl SelectImageOption {
pub fn new(img_key: &str) -> Self {
Self {
img_key: img_key.to_string(),
value: None,
disabled: None,
disabled_tips: None,
hover_tips: None,
}
}
pub fn value(mut self, value: &str) -> Self {
self.value = Some(value.to_string());
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 hover_tips(mut self, hover_tips: PlainText) -> Self {
self.hover_tips = Some(hover_tips);
self
}
}
impl Default for ImagePicker {
fn default() -> Self {
Self {
tag: "select_img".to_string(),
style: None,
multi_select: None,
layout: None,
name: None,
required: None,
can_preview: None,
aspect_ratio: None,
disabled: None,
disabled_tips: None,
value: None,
options: None,
}
}
}
impl ImagePicker {
pub fn new() -> Self {
Self::default()
}
pub fn style(mut self, style: &str) -> Self {
self.style = Some(style.to_string());
self
}
pub fn multi_select(mut self, multi_select: bool) -> Self {
self.multi_select = Some(multi_select);
self
}
pub fn layout(mut self, layout: &str) -> Self {
self.layout = Some(layout.to_string());
self
}
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 can_preview(mut self, can_preview: bool) -> Self {
self.can_preview = Some(can_preview);
self
}
pub fn aspect_ratio(mut self, aspect_ratio: &str) -> Self {
self.aspect_ratio = Some(aspect_ratio.to_string());
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 value(mut self, value: Value) -> Self {
self.value = Some(value);
self
}
pub fn options(mut self, options: Vec<SelectImageOption>) -> Self {
self.options = Some(options);
self
}
}
#[cfg(test)]
mod test {
use serde_json::json;
use super::*;
#[test]
fn test_image_picker() {
let image_picker = ImagePicker::new()
.style("laser")
.multi_select(false)
.layout("bisect")
.name("choice_123")
.required(false)
.can_preview(false)
.aspect_ratio("16:9")
.disabled(false)
.disabled_tips(PlainText::text("用户禁用提示文案"))
.value(json!({"key": "value"}))
.options(vec![SelectImageOption::new("xxxxxxxxxxxxxx")
.value("picture1")
.disabled(false)
.disabled_tips(PlainText::text("用户禁用提示文案"))
.hover_tips(PlainText::text("第一张图"))]);
let json = json!( {
"tag": "select_img", "style": "laser", "multi_select": false, "layout": "bisect", "name": "choice_123", "required": false, "can_preview": false, "aspect_ratio": "16:9", "disabled": false, "disabled_tips": { "tag": "plain_text",
"content": "用户禁用提示文案"
},
"value": { "key": "value"
},
"options": [
{
"img_key": "xxxxxxxxxxxxxx", "value": "picture1", "disabled": false, "disabled_tips": { "tag": "plain_text",
"content": "用户禁用提示文案"
},
"hover_tips": { "tag": "plain_text",
"content": "第一张图"
},
}
]
});
assert_eq!(json!(image_picker), json);
}
}