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(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 TransMapWidgets {
pub map_widgets: std::collections::HashMap<String, Widget>,
}
#[Model]
#[derive(Serialize, Deserialize, Default, Clone, Debug)]
pub struct UserProfile {
#[serde(default)]
pub first_name: Option<String>,
#[serde(default)]
pub last_name: Option<String>,
#[serde(default)]
pub nickname: Option<String>,
}
#[test]
fn it_work() {
let user_profile: UserProfile = Default::default();
assert_eq!(None, user_profile.first_name);
assert_eq!(None, user_profile.last_name);
assert_eq!(None, user_profile.nickname);
assert_eq!(None, user_profile.hash);
let collection_name = "test_service_name__user_profile".to_string();
assert_eq!(collection_name, UserProfile::key_store().unwrap());
let map_field_type: HashMap<String, String> = vec![
("first_name".to_string(), "String".to_string()),
("last_name".to_string(), "String".to_string()),
("nickname".to_string(), "String".to_string()),
("hash".to_string(), "String".to_string()),
]
.iter()
.cloned()
.collect();
let map_widget_type: HashMap<String, String> = vec![
("first_name".to_string(), "inputText".to_string()),
("last_name".to_string(), "inputText".to_string()),
("nickname".to_string(), "inputText".to_string()),
("hash".to_string(), "inputText".to_string()),
]
.iter()
.cloned()
.collect();
let map_default_values: std::collections::HashMap<String, (String, String)> = vec![
(
"first_name".to_string(),
("inputText".to_string(), String::new()),
),
(
"last_name".to_string(),
("inputText".to_string(), String::new()),
),
(
"nickname".to_string(),
("inputText".to_string(), String::new()),
),
("hash".to_string(), ("inputText".to_string(), String::new())),
]
.iter()
.cloned()
.collect();
let map_related_models: std::collections::HashMap<
String,
std::collections::HashMap<String, String>,
> = HashMap::new();
let meta = Meta {
service_name: "test_service_name".to_string(),
database_name: "test_database_name".to_string(),
model_name: "UserProfile".to_string(),
collection_name,
fields_count: 4,
fields_name: vec![
"first_name".to_string(),
"last_name".to_string(),
"nickname".to_string(),
"hash".to_string(),
],
is_add_docs: true,
is_up_docs: true,
is_del_docs: true,
map_field_type,
map_widget_type,
map_default_values,
map_related_models,
ignore_fields: Vec::new(),
};
assert_eq!(meta, UserProfile::meta().unwrap());
}