use metamorphose::Model;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
const _SERVICE_NAME: &str = "test_service_name";
const _DATABASE_NAME: &str = "test_database_name";
#[derive(Default, 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 map_field_type: std::collections::HashMap<String, String>,
pub map_widget_type: std::collections::HashMap<String, String>,
pub map_relation_models: HashMap<String, HashMap<String, String>>,
pub ignore_fields: Vec<String>,
}
pub trait ToModel {
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 TransMapWidgets {
pub map_widgets: std::collections::HashMap<String, Widget>,
}
#[Model]
#[derive(Serialize, Deserialize, Default, Clone, Debug)]
pub struct TestModel {
#[field_attrs(
widget = "checkBoxU32",
label = "Lorem ipsum dolor sit amet",
default = 4294967295,
required = false,
checked = false,
unique = false,
hidden = false,
disabled = false,
readonly = false,
other_attrs = "autofocus",
css_classes = "class_name class_name_2",
hint = "Lorem ipsum dolor sit amet"
)]
test_field: u32,
}
#[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 = "checkBoxU32".to_string();
widget.value = u32::MAX.to_string();
widget.input_type = "checkbox".to_string();
widget.name = "test_field".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".to_string();
widget.css_classes = "class_name class_name_2".to_string();
widget.choices = Vec::new();
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());
}