metamorphose 0.1.18-alpha.1

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

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

// 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 Widget types map to implementation of methods
// <field name, Widget type name>
#[derive(Deserialize)]
struct TransMapWidgetType {
    pub map_widget_type: std::collections::HashMap<String, String>,
}

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

pub trait ToForm {
    // Get map of widget types
    // <field name, Widget type name>
    fn map_widget_type() -> Result<HashMap<String, String>, 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>>;
}

// Create Form
// *************************************************************************************************
#[Form]
#[derive(Serialize, Deserialize, Default, Clone, Debug)]
pub struct LoginForm {
    #[field_attrs(widget = "inputEmail")]
    pub email: String,
    #[field_attrs(widget = "inputPassword")]
    pub password: String,
}

// Tests
// *************************************************************************************************
#[test]
fn it_work() {
    // Checking Widgets
    // ---------------------------------------------------------------------------------------------
    let mut widgets: HashMap<String, Widget> = HashMap::new();
    // email
    let mut widget = Widget::default();
    widget.id = "login-form--email".to_string();
    widget.widget = "inputEmail".to_string();
    widget.input_type = "email".to_string();
    widget.name = "email".to_string();
    widgets.insert("email".to_string(), widget);
    // password
    let mut widget = Widget::default();
    widget.id = "login-form--password".to_string();
    widget.widget = "inputPassword".to_string();
    widget.input_type = "password".to_string();
    widget.name = "password".to_string();
    widgets.insert("password".to_string(), widget);

    assert_eq!(widgets, LoginForm::widgets().unwrap());
}