br-fields 2.3.5

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

/// 数字
///
/// * require 是否必填
/// * field 字段名
/// * mode 模式 string
/// * title 字段描述
/// * length 字段总长度(含小数位)
/// * def 默认值
/// * dec 符号
pub struct Int {
    pub require: bool,
    pub field: String,
    pub mode: String,
    pub title: String,
    pub def: i64,
    pub length: i32,
    pub show: bool,
    pub describe: String,
    pub example: JsonValue,
}

impl Int {
    pub fn new(require: bool, field: &str, title: &str, length: i32, default: i64) -> Self {
        Self {
            require,
            field: field.to_string(),
            mode: "int".to_string(),
            title: title.to_string(),
            def: default,
            length,
            show: true,
            describe: "".to_string(),
            example: JsonValue::Null,
        }
    }
}

impl Field for Int {
    fn sql(&mut self, model: &str) -> String {
        let not_null = if self.require { " not null" } else { "" };
        match model {
            "sqlite" => format!("`{}` INTEGER{} default {}", self.field, not_null, self.def),
            "pgsql" => {
                let sql = format!(r#""{}" INTEGER default {}"#, self.field, self.def);
                format!(
                    "{} --{}|{}|{}|{}|{}",
                    sql, self.mode, self.require, self.title, self.length, self.def
                )
            }
            _ => {
                let sql = format!(
                    "`{}` int({}){} default {}",
                    self.field, self.length, not_null, self.def
                );
                format!(
                    "{} comment '{}|{}|{}|{}|{}'",
                    sql.clone(),
                    self.mode,
                    self.require,
                    self.title,
                    self.length,
                    self.def
                )
            }
        }
    }
    fn hide(&mut self) -> &mut Self {
        self.show = false;
        self
    }
    fn describe(&mut self, text: &str) -> &mut Self {
        self.describe = text.to_string();
        self
    }
    fn field(&mut self) -> JsonValue {
        let mut field = object! {};
        field
            .insert("require", JsonValue::from(self.require))
            .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))
            .unwrap();
        field.insert("def", JsonValue::from(self.def)).unwrap();

        field.insert("show", JsonValue::from(self.show)).unwrap();
        field
            .insert("describe", JsonValue::from(self.describe.clone()))
            .unwrap();
        field.insert("example", self.example.clone()).unwrap();
        field
    }

    fn swagger(&mut self) -> JsonValue {
        self.mode = match self.mode.as_str() {
            "int" => "integer",
            _ => self.mode.as_str(),
        }
        .to_string();
        object! {
            "type": self.mode.clone(),
            "example": self.example.clone(),
        }
    }

    fn example(&mut self, data: JsonValue) -> &mut Self {
        self.example = data.clone();
        self
    }
}

/// 开关
///
/// * field 字段名
/// * mode 模式 string
/// * title 字段描述
/// * default 默认值
/// * empty 是否可空
pub struct Switch {
    pub require: bool,
    pub field: String,
    pub mode: String,
    pub title: String,
    pub def: bool,

    pub show: bool,
    pub describe: String,
    pub example: JsonValue,
}

impl Switch {
    pub fn new(require: bool, field: &str, title: &str, default: bool) -> Self {
        Self {
            require,
            field: field.to_string(),
            mode: "switch".to_string(),
            title: title.to_string(),
            def: default,
            show: true,
            describe: "".to_string(),
            example: JsonValue::Null,
        }
    }
}

impl Field for Switch {
    fn sql(&mut self, model: &str) -> String {
        let not_null = if self.require { " not null" } else { "" };
        match model {
            "sqlite" => {
                let def = if self.def { 1 } else { 0 };
                format!("`{}` INTEGER{} default {}", self.field, not_null, def)
            }
            "pgsql" => {
                let sql = format!(r#""{}" BOOLEAN default {}"#, self.field, self.def);
                format!(
                    "{} --{}|{}|{}|{}|BOOLEAN",
                    sql, self.mode, self.require, self.title, self.def
                )
            }
            "mysql" => {
                let sql = format!("`{}` BOOLEAN{} default {}", self.field, not_null, self.def);
                format!(
                    "{} comment '{}|{}|{}|{}|BOOLEAN'",
                    sql.clone(),
                    self.mode,
                    self.require,
                    self.title,
                    self.def
                )
            }
            _ => {
                let sql = format!(
                    "`{}` tinyint(1){} default {}",
                    self.field, not_null, self.def
                );
                format!(
                    "{} comment '{}|{}|{}|{}'",
                    sql.clone(),
                    self.mode,
                    self.require,
                    self.title,
                    self.def
                )
            }
        }
    }
    fn hide(&mut self) -> &mut Self {
        self.show = false;
        self
    }
    fn describe(&mut self, text: &str) -> &mut Self {
        self.describe = text.to_string();
        self
    }
    fn field(&mut self) -> JsonValue {
        let mut field = object! {};
        field
            .insert("require", JsonValue::from(self.require))
            .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("def", JsonValue::from(self.def)).unwrap();

        field.insert("show", JsonValue::from(self.show)).unwrap();
        field
            .insert("describe", JsonValue::from(self.describe.clone()))
            .unwrap();
        field.insert("example", self.example.clone()).unwrap();
        field
    }

    fn swagger(&mut self) -> JsonValue {
        object! {
            "type": self.mode.clone(),
            "example": self.example.clone(),
        }
    }

    fn example(&mut self, data: JsonValue) -> &mut Self {
        self.example = data.clone();
        self
    }
}