metamorphose 0.1.18-alpha.1

Macros collection for converting Structure to Model, for a mango-orm project.
Documentation
use metamorphose::Model;

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

// Simulate forwarding from application settings
const _SERVICE_NAME: &str = "test_service_name";
const _DATABASE_NAME: &str = "test_database_name";

// For Models
// *************************************************************************************************
// Model metadata
#[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>>,
    // List of field names that will not be saved to the database
    pub ignore_fields: Vec<String>,
}

pub trait ToModel {
    // Get Model info
    fn meta() -> Result<Meta, Box<dyn std::error::Error>>;
    // Get map of widgets for model fields
    // <field name, Widget>
    fn widgets() -> Result<HashMap<String, Widget>, Box<dyn std::error::Error>>;
}

// For Widgets
// *************************************************************************************************
#[derive(Deserialize, PartialEq, Debug)]
pub struct Widget {
    pub id: String, // "model-name--field-name" ( The value is determined automatically )
    pub label: String,
    pub widget: String,
    pub input_type: String, // The value is determined automatically
    pub name: String,       // The value is determined automatically
    pub value: String,
    pub placeholder: String,
    pub pattern: String, // Validating a field using a client-side regex
    pub minlength: usize,
    pub maxlength: usize,
    pub required: bool,
    pub checked: bool, // For <input type="checkbox|radio">
    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, // "autofocus multiple size=\"some number\" ..."
    pub css_classes: String, // "class-name class-name ..."
    pub choices: Vec<(String, String)>,
    pub hint: String,
    pub warning: String, // The value is determined automatically
    pub error: String,   // The value is determined automatically
}

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(),
        }
    }
}

// For transporting of Widgets map to implementation of methods
// <field name, Widget>
#[derive(Deserialize)]
struct TransMapWidgets {
    pub map_widgets: std::collections::HashMap<String, Widget>,
}

// Create Model
// *************************************************************************************************
#[Model]
#[derive(Serialize, Deserialize, Default, Clone, Debug)]
pub struct TestModel {
    #[field_attrs(
        widget = "inputFile",
        label = "Lorem ipsum dolor sit amet",
        default = "",
        required = true,
        unique = false,
        hidden = false,
        disabled = false,
        readonly = false,
        other_attrs = "autofocus size=\"60\"",
        css_classes = "class_name class_name_2",
        hint = "Lorem ipsum dolor sit amet"
    )]
    test_field: String,
}

// Tests
// *************************************************************************************************
#[test]
fn it_work() {
    let mut widgets: HashMap<String, Widget> = HashMap::new();

    // hash
    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);

    // title
    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 = "inputFile".to_string();
    widget.input_type = "file".to_string();
    widget.name = "test_field".to_string();
    widget.value = String::new();
    widget.placeholder = String::new();
    widget.pattern = String::new();
    widget.minlength = 0_usize;
    widget.maxlength = 256_usize;
    widget.required = true;
    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::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());
}