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 UserProfile {
    pub first_name: String,
    pub last_name: String,
    pub nickname: String,
}

// Tests
// *************************************************************************************************
#[test]
fn it_work() {
    // Checking for the existence of an auto-generated `hash` field
    let user_profile: UserProfile = Default::default();
    assert_eq!(String::new(), user_profile.hash);

    // Checking structure of model
    let collection_name = "test_service_name__user_profile".to_string();

    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_relation_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: collection_name.clone(),
        fields_count: 4,
        fields_name: vec![
            "first_name".to_string(),
            "last_name".to_string(),
            "nickname".to_string(),
            "hash".to_string(),
        ],
        map_field_type: map_field_type.clone(),
        map_widget_type: map_widget_type.clone(),
        map_relation_models: map_relation_models.clone(),
        ignore_fields: Vec::new(),
    };
    assert_eq!(meta, UserProfile::meta().unwrap());
}