df-fields 0.1.16

This is a shortcut tool related to database fields
Documentation
use json::{JsonValue, object};
use crate::{Field};

/// 字符串
///
/// * require 是否必填
/// * field 字段名
/// * mode 模式 string
/// * title 字段描述
/// * length 字段总长度(含小数位)
/// * def 默认值
/// * dec 后缀内容
pub struct Str {
    pub require: bool,
    pub field: String,
    pub mode: String,
    pub title: String,
    pub def: String,
    pub length: i32,
    pub dec: String,
}

impl Str {
    pub fn new(require: bool, field: &str, title: &str, length: i32, default: &str) -> Str {
        Self {
            require,
            field: field.to_string(),
            mode: "string".to_string(),
            title: title.to_string(),
            def: default.to_string(),
            length,
            dec: "".to_string(),
        }
    }
}

impl Field for Str {
    fn sql(&mut self, model: &str) -> String {
        let mut sql = format!("{} varchar({})", self.field, self.length);
        if self.require {
            sql = format!("{} not null", sql.clone())
        };
        sql = format!("{} default '{}'", sql.clone(), self.def);
        match model {
            "sqlite" => sql,
            _ => format!("{} comment '{}|{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def, self.dec)
        }
    }
    fn field(&mut self) -> JsonValue {
        let mut field = object! {};
        field.insert("require", JsonValue::from(self.require.clone())).unwrap();
        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
        field.insert("length", JsonValue::from(self.length.clone())).unwrap();
        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
        field.insert("dec", JsonValue::from(self.dec.clone())).unwrap();
        field
    }
}


/// 密码
///
/// * field 字段名
/// * mode 模式 pass
/// * title 字段描述
/// * length 字段总长度(含小数位)
/// * default 默认值
/// * empty 是否可空
pub struct Pass {
    pub require: bool,
    pub field: String,
    pub mode: String,
    pub title: String,
    pub def: String,
    pub length: i32,
    pub dec: String,
}

impl Pass {
    pub fn new(require: bool, field: &str, title: &str, length: i32, default: &str) -> Self {
        Self {
            require,
            field: field.to_string(),
            mode: "pass".to_string(),
            title: title.to_string(),
            def: default.to_string(),
            length,
            dec: "".to_string(),
        }
    }
}

impl Field for Pass {
    fn sql(&mut self, model: &str) -> String {
        let mut sql = format!("{} varchar({})", self.field, self.length);
        if self.require {
            sql = format!("{} not null", sql.clone())
        };
        sql = format!("{} default '{}'", sql.clone(), self.def);
        match model {
            "sqlite" => sql,
            _ => format!("{} comment '{}|{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def, self.dec)
        }
    }
    fn field(&mut self) -> JsonValue {
        let mut field = object! {};
        field.insert("require", JsonValue::from(self.require.clone())).unwrap();
        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
        field.insert("length", JsonValue::from(self.length.clone())).unwrap();
        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
        field.insert("dec", JsonValue::from(self.dec.clone())).unwrap();
        field
    }
}

/// 自定义主键
///
/// * field 字段名
/// * mode 模式
/// * title 字段描述
/// * length 字段总长度(含小数位)
/// * def 默认值
pub struct Key {
    pub require: bool,
    pub field: String,
    pub mode: String,
    pub title: String,
    pub def: String,
    pub length: i32,
    pub dec: String,
}

impl Key {
    pub fn new(require: bool, field: &str, title: &str, length: i32) -> Self {
        Self {
            require,
            field: field.to_string(),
            mode: "key".to_string(),
            title: title.to_string(),
            def: "".to_string(),
            length,
            dec: "".to_string(),
        }
    }
}

impl Field for Key {
    fn sql(&mut self, model: &str) -> String {
        let mut sql = "".to_string();
        sql = format!("{}{}  varchar({})", sql.clone(), self.field, self.length);
        sql = format!("{}", sql.clone());
        //PRIMARY KEY
        match model {
            "sqlite" => sql,
            _ => format!("{} comment '{}|{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def, self.dec)
        }
    }

    fn field(&mut self) -> JsonValue {
        let mut field = object! {};
        field.insert("require", JsonValue::from(self.require.clone())).unwrap();
        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
        field.insert("length", JsonValue::from(self.length.clone())).unwrap();
        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
        field.insert("dec", JsonValue::from(self.dec.clone())).unwrap();
        field
    }
}


/// 电话
///
/// * field 字段名
/// * mode 模式 pass
/// * title 字段描述
/// * length 字段总长度(含小数位)
/// * default 默认值
/// * empty 是否可空
pub struct Tel {
    pub require: bool,
    pub field: String,
    pub mode: String,
    pub title: String,
    pub def: String,
    pub length: i32,
}

impl Tel {
    pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
        Self {
            require,
            field: field.to_string(),
            mode: "tel".to_string(),
            title: title.to_string(),
            def: default.to_string(),
            length: 15,
        }
    }
}

impl Field for Tel {
    fn sql(&mut self, model: &str) -> String {
        let mut sql = format!("{} varchar({})", self.field, self.length);
        if self.require {
            sql = format!("{} not null", sql.clone())
        };
        sql = format!("{} default '{}'", sql.clone(), self.def);
        match model {
            "sqlite" => sql,
            _ => format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def)
        }
    }
    fn field(&mut self) -> JsonValue {
        let mut field = object! {};
        field.insert("require", JsonValue::from(self.require.clone())).unwrap();
        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
        field.insert("length", JsonValue::from(self.length.clone())).unwrap();
        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
        field
    }
}


/// 电子邮件
///
/// * field 字段名
/// * mode 模式 pass
/// * title 字段描述
/// * length 字段总长度(含小数位)
/// * default 默认值
/// * empty 是否可空
pub struct Email {
    pub require: bool,
    pub field: String,
    pub mode: String,
    pub title: String,
    pub def: String,
    pub length: i32,
}

impl Email {
    pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
        Self {
            require,
            field: field.to_string(),
            mode: "email".to_string(),
            title: title.to_string(),
            def: default.to_string(),
            length: 50,
        }
    }
}

impl Field for Email {
    fn sql(&mut self, model: &str) -> String {
        let mut sql = format!("{} varchar({})", self.field, self.length);
        if self.require {
            sql = format!("{} not null", sql.clone())
        };
        sql = format!("{} default '{}'", sql.clone(), self.def);
        match model {
            "sqlite" => sql,
            _ => format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def)
        }
    }
    fn field(&mut self) -> JsonValue {
        let mut field = object! {};
        field.insert("require", JsonValue::from(self.require.clone())).unwrap();
        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
        field.insert("length", JsonValue::from(self.length.clone())).unwrap();
        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
        field
    }
    fn verify(&mut self, data: JsonValue) -> JsonValue {
        data
    }
}


/// 颜色
///
/// * field 字段名
/// * mode 模式 pass
/// * title 字段描述
/// * length 字段总长度(含小数位)
/// * default 默认值
/// * empty 是否可空

pub struct Color {
    pub require: bool,
    pub field: String,
    pub mode: String,
    pub title: String,
    pub def: String,
    pub length: i32,
}

impl Color {
    pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
        Self {
            require,
            field: field.to_string(),
            mode: "color".to_string(),
            title: title.to_string(),
            def: default.to_string(),
            length: 9,
        }
    }
}

impl Field for Color {
    fn sql(&mut self, model: &str) -> String {
        let mut sql = format!("{} varchar({})", self.field, self.length);
        if self.require {
            sql = format!("{} not null", sql.clone())
        };
        sql = format!("{} default '{}'", sql.clone(), self.def);
        match model {
            "sqlite" => sql,
            _ => format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def)
        }
    }
    fn field(&mut self) -> JsonValue {
        let mut field = object! {};
        field.insert("require", JsonValue::from(self.require.clone())).unwrap();
        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
        field.insert("length", JsonValue::from(self.length.clone())).unwrap();
        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
        field
    }
    fn verify(&mut self, data: JsonValue) -> JsonValue {
        data
    }
}