use metamorphose::{Form, Model};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
const _SERVICE_NAME: &str = "test_service_name";
const _DATABASE_NAME: &str = "test_database_name";
#[derive(Deserialize, PartialEq, Debug)]
pub struct Meta {
pub model_name: String,
pub service_name: String,
pub database_name: String,
pub collection_name: String,
pub fields_count: usize,
pub fields_name: Vec<String>,
pub is_add_docs: bool,
pub is_up_docs: bool,
pub is_del_docs: bool,
pub map_field_type: std::collections::HashMap<String, String>,
pub map_widget_type: std::collections::HashMap<String, String>,
pub map_default_values: std::collections::HashMap<String, (String, String)>,
pub map_related_models:
std::collections::HashMap<String, std::collections::HashMap<String, String>>,
pub ignore_fields: Vec<String>,
}
impl Default for Meta {
fn default() -> Self {
Meta {
model_name: String::new(),
service_name: String::new(),
database_name: String::new(),
collection_name: String::new(),
fields_count: 0_usize,
fields_name: Vec::new(),
is_add_docs: true,
is_up_docs: true,
is_del_docs: true,
map_field_type: std::collections::HashMap::new(),
map_widget_type: std::collections::HashMap::new(),
map_default_values: std::collections::HashMap::new(),
map_related_models: std::collections::HashMap::new(),
ignore_fields: Vec::new(),
}
}
}
pub trait ToModel {
fn key_store() -> Result<String, Box<dyn std::error::Error>>;
fn meta() -> Result<Meta, Box<dyn std::error::Error>>;
fn widgets() -> Result<HashMap<String, Widget>, Box<dyn std::error::Error>>;
}
#[derive(Deserialize, PartialEq, Debug)]
pub struct Widget {
pub id: String, pub label: String,
pub widget: String,
pub input_type: String, pub name: String, pub value: String,
pub placeholder: String,
pub pattern: String, pub minlength: usize,
pub maxlength: usize,
pub required: bool,
pub checked: bool, pub unique: bool,
pub hidden: bool,
pub disabled: bool,
pub readonly: bool,
pub step: String,
pub min: String,
pub max: String,
pub other_attrs: String, pub css_classes: String, pub choices: Vec<(String, String)>,
pub hint: String,
pub warning: String, pub error: String, }
impl Default for Widget {
fn default() -> Self {
Widget {
id: String::new(),
label: String::new(),
widget: String::from("inputText"),
input_type: String::from("text"),
name: String::new(),
value: String::new(),
placeholder: String::new(),
pattern: String::new(),
minlength: 0_usize,
maxlength: 256_usize,
required: false,
checked: false,
unique: false,
hidden: false,
disabled: false,
readonly: false,
step: String::from("0"),
min: String::from("0"),
max: String::from("0"),
other_attrs: String::new(),
css_classes: String::new(),
choices: Vec::new(),
hint: String::new(),
warning: String::new(),
error: String::new(),
}
}
}
#[derive(Deserialize)]
struct TransMapWidgetType {
pub map_widget_type: std::collections::HashMap<String, String>,
}
#[derive(Deserialize)]
struct TransMapWidgets {
pub map_widgets: std::collections::HashMap<String, Widget>,
}
pub trait ToForm {
fn key_store() -> Result<String, Box<dyn std::error::Error>>;
fn widgets() -> Result<HashMap<String, Widget>, Box<dyn std::error::Error>>;
}
#[Model]
#[derive(Serialize, Deserialize, Default, Clone, Debug)]
pub struct TestModel {
#[serde(default)]
#[field_attrs(
widget = "selectI64",
label = "Lorem ipsum dolor sit amet",
default = 9_223_372_036_854_775_807,
pattern = "",
required = false,
unique = false,
hidden = false,
disabled = false,
readonly = false,
choices = "[[-9_223_372_036_854_775_808, \"Number 1\"], [9_223_372_036_854_775_807, \"Number 2\"]]",
other_attrs = "autofocus size=\"60\"",
css_classes = "class_name class_name_2",
hint = "Lorem ipsum dolor sit amet"
)]
test_field: Option<i64>,
}
#[Form]
#[derive(Serialize, Deserialize, Default, Clone, Debug)]
pub struct TestForm {
#[serde(default)]
#[field_attrs(
widget = "selectI64",
label = "Lorem ipsum dolor sit amet",
default = 9_223_372_036_854_775_807,
pattern = "",
required = false,
unique = false,
hidden = false,
disabled = false,
readonly = false,
choices = "[[-9_223_372_036_854_775_808, \"Number 1\"], [9_223_372_036_854_775_807, \"Number 2\"]]",
other_attrs = "autofocus size=\"60\"",
css_classes = "class_name class_name_2",
hint = "Lorem ipsum dolor sit amet"
)]
test_field: Option<i64>,
}
#[test]
fn it_work() {
let mut widgets: HashMap<String, Widget> = HashMap::new();
let mut widget = Widget::default();
widget.id = "test-model--hash".to_string();
widget.widget = "inputText".to_string();
widget.input_type = "text".to_string();
widget.name = "hash".to_string();
widget.hidden = true;
widgets.insert("hash".to_string(), widget);
let mut widget: Widget = Default::default();
widget.id = "test-model--test-field".to_string();
widget.label = "Lorem ipsum dolor sit amet".to_string();
widget.widget = "selectI64".to_string();
widget.input_type = "select".to_string();
widget.name = "test_field".to_string();
widget.value = i64::MAX.to_string();
widget.placeholder = String::new();
widget.pattern = String::new();
widget.minlength = 0_usize;
widget.maxlength = 256_usize;
widget.required = false;
widget.checked = false;
widget.unique = false;
widget.hidden = false;
widget.disabled = false;
widget.readonly = false;
widget.step = "0".to_string();
widget.min = "0".to_string();
widget.max = "0".to_string();
widget.other_attrs = "autofocus size=\"60\"".to_string();
widget.css_classes = "class_name class_name_2".to_string();
widget.choices = vec![
(i64::MIN.to_string(), "Number 1".to_string()),
(i64::MAX.to_string(), "Number 2".to_string()),
];
widget.hint = "Lorem ipsum dolor sit amet".to_string();
widget.warning = String::new();
widget.error = String::new();
widgets.insert("test_field".to_string(), widget);
assert_eq!(widgets, TestModel::widgets().unwrap());
widgets.remove("hash");
let mut widget = widgets.get_mut("test_field").unwrap();
widget.id = "test-form--test-field".to_string();
assert_eq!(widgets, TestForm::widgets().unwrap());
}