use crate::datetime::Datetime;
use crate::Field;
use chrono::Local;
use json::{object, JsonValue};
use lazy_static::lazy_static;
use rand::RngExt;
use regex::Regex;
use std::sync::atomic::{AtomicU16, Ordering};
static SEQ: AtomicU16 = AtomicU16::new(0);
#[derive(Debug, Clone)]
pub struct Str {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub def: String,
pub length: usize,
pub show: bool,
pub describe: String,
pub example: JsonValue,
}
impl Str {
pub fn new(require: bool, field: &str, title: &str, length: usize, default: &str) -> Str {
Self {
require,
field: field.to_string(),
mode: "string".to_string(),
title: title.to_string(),
def: default.to_string(),
length,
show: true,
describe: "".to_string(),
example: JsonValue::Null,
}
}
}
impl Field for Str {
fn require(&mut self, require: bool) -> &mut Self {
self.require = require;
self
}
fn sql(&mut self, model: &str) -> String {
let not_null = if self.require { " not null" } else { "" };
match model {
"sqlite" => format!(
"`{}` varchar({}){} default '{}'",
self.field, self.length, not_null, self.def
),
"pgsql" => {
let sql = format!(
r#""{}" varchar({}) default '{}'"#,
self.field, self.length, self.def
);
format!(
"{} --{}|{}|{}|{}|{}",
sql, self.mode, self.require, self.title, self.length, self.def
)
}
_ => {
let sql = format!(
"`{}` varchar({}){} default '{}'",
self.field, self.length, not_null, self.def
);
format!(
"{} comment '{}|{}|{}|{}|{}'",
sql, 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.clone()))
.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;
self
}
}
pub struct Pass {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub def: String,
pub length: usize,
pub show: bool,
pub describe: String,
pub example: JsonValue,
}
impl Pass {
pub fn new(require: bool, field: &str, title: &str, length: usize, default: &str) -> Self {
Self {
require,
field: field.to_string(),
mode: "pass".to_string(),
title: title.to_string(),
def: default.to_string(),
length,
show: true,
describe: "".to_string(),
example: json::Null,
}
}
pub fn random_pass(len: usize) -> String {
let text = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789";
let mut code = "".to_string();
for _ in 0..len {
let index = rand::rng().random_range(0..=text.len() - 1);
let t = &text[index..=index];
code = format!("{}{}", code, t);
}
code
}
}
impl Field for Pass {
fn require(&mut self, require: bool) -> &mut Self {
self.require = require;
self
}
fn sql(&mut self, model: &str) -> String {
let not_null = if self.require { " not null" } else { "" };
match model {
"sqlite" => format!(
"`{}` varchar({}){} default '{}'",
self.field, self.length, not_null, self.def
),
"pgsql" => {
let sql = format!(
r#""{}" varchar({}) default '{}'"#,
self.field, self.length, self.def
);
format!(
"{} --{}|{}|{}|{}|{}",
sql, self.mode, self.require, self.title, self.length, self.def
)
}
_ => {
let sql = format!(
"`{}` varchar({}){} default '{}'",
self.field, self.length, not_null, self.def
);
format!(
"{} comment '{}|{}|{}|{}|{}'",
sql, 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.clone()))
.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;
self
}
}
#[derive(Clone, Debug)]
pub struct Key {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub length: usize,
pub def: String,
pub auto: bool,
pub show: bool,
pub describe: String,
pub example: JsonValue,
}
impl Key {
pub fn new(require: bool, field: &str, title: &str, length: usize) -> Self {
Self {
require,
field: field.to_string(),
mode: "key".to_string(),
title: title.to_string(),
length,
auto: false,
show: true,
def: "".to_string(),
describe: "".to_string(),
example: JsonValue::Null,
}
}
pub fn auto(mut self, auto: bool) -> Self {
if auto {
self.length = 0;
}
self.auto = auto;
self
}
}
impl Field for Key {
fn require(&mut self, require: bool) -> &mut Self {
self.require = require;
self
}
fn sql(&mut self, model: &str) -> String {
let not_null = if self.require { " not null" } else { "" };
match model {
"sqlite" => match self.auto {
true => {
format!("`{}` INTEGER PRIMARY KEY AUTOINCREMENT", self.field)
}
false => {
format!(
"`{}` varchar({}){} default ''",
self.field, self.length, not_null
)
}
},
"pgsql" => match self.auto {
true => {
format!(
r#""{}" SERIAL PRIMARY KEY --{}|{}|{}"#,
self.field, self.mode, self.title, self.length
)
}
false => {
format!(
r#""{}" varchar({}) default '' --{}|{}|{}"#,
self.field, self.length, self.mode, self.title, self.length
)
}
},
_ => match self.auto {
true => {
let sql = format!("`{}` int{} AUTO_INCREMENT", self.field, not_null);
format!(
"{} comment '{}|{}|{}'",
sql, self.mode, self.title, self.length
)
}
false => {
let sql = format!(
"`{}` varchar({}){} default ''",
self.field, self.length, not_null
);
format!(
"{} comment '{}|{}|{}'",
sql, self.mode, self.title, self.length
)
}
},
}
}
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.clone()))
.unwrap();
field.insert("auto", JsonValue::from(self.auto)).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;
self
}
}
#[derive(Clone, Debug)]
pub struct Keys {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub def: JsonValue,
pub show: bool,
pub describe: String,
pub example: JsonValue,
}
impl Keys {
pub fn new(require: bool, field: &str, title: &str, def: JsonValue) -> Self {
Self {
require,
field: field.to_string(),
mode: "keys".to_string(),
title: title.to_string(),
def,
show: true,
describe: "".to_string(),
example: JsonValue::Null,
}
}
}
impl Field for Keys {
fn require(&mut self, require: bool) -> &mut Self {
self.require = require;
self
}
fn sql(&mut self, model: &str) -> String {
match model {
"sqlite" => format!("`{}` TEXT default '[]'", self.field),
"pgsql" => {
let sql = format!(r#""{}" json default '[]'"#, self.field);
format!("{} --{}|{}|{}", sql, self.mode, self.require, self.title)
}
_ => {
let sql = format!("`{}` json default ('[]')", self.field);
format!(
"{} comment '{}|{}|{}'",
sql, self.mode, self.require, self.title
)
}
}
}
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", self.def.clone()).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;
self
}
}
pub struct Tel {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub def: String,
pub length: i32,
pub show: bool,
pub describe: String,
pub example: JsonValue,
}
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,
show: true,
describe: "".to_string(),
example: JsonValue::Null,
}
}
pub fn is_phone(input: &str) -> bool {
lazy_static! {
static ref CN_RE: Regex = Regex::new(r"^1[3-9]\d{9}$").unwrap();
static ref INTL_RE: Regex = Regex::new(r"^\+?[1-9]\d{1,14}$").unwrap();
static ref INTL_00_RE: Regex = Regex::new(r"^00[1-9]\d{1,14}$").unwrap();
}
CN_RE.is_match(input) || INTL_RE.is_match(input) || INTL_00_RE.is_match(input)
}
}
impl Field for Tel {
fn require(&mut self, require: bool) -> &mut Self {
self.require = require;
self
}
fn sql(&mut self, model: &str) -> String {
let not_null = if self.require { " not null" } else { "" };
match model {
"sqlite" => format!(
"`{}` varchar({}){} default '{}'",
self.field, self.length, not_null, self.def
),
"pgsql" => {
let sql = format!(
r#""{}" varchar({}) default '{}'"#,
self.field, self.length, self.def
);
format!(
"{} --{}|{}|{}|{}|{}",
sql, self.mode, self.require, self.title, self.length, self.def
)
}
_ => {
let sql = format!(
"`{}` varchar({}){} default '{}'",
self.field, self.length, not_null, self.def
);
format!(
"{} comment '{}|{}|{}|{}|{}'",
sql, 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.clone()))
.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;
self
}
}
pub struct Ident {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub def: String,
pub length: i32,
pub show: bool,
pub describe: String,
pub example: JsonValue,
}
impl Ident {
pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
Self {
require,
field: field.to_string(),
mode: "ident".to_string(),
title: title.to_string(),
def: default.to_string(),
length: 18,
show: true,
describe: "".to_string(),
example: JsonValue::Null,
}
}
}
impl Field for Ident {
fn require(&mut self, require: bool) -> &mut Self {
self.require = require;
self
}
fn sql(&mut self, model: &str) -> String {
let not_null = if self.require { " not null" } else { "" };
match model {
"sqlite" => format!(
"`{}` varchar({}){} default '{}'",
self.field, self.length, not_null, self.def
),
"pgsql" => {
let sql = format!(
r#""{}" varchar({}) default '{}'"#,
self.field, self.length, self.def
);
format!(
"{} --{}|{}|{}|{}|{}",
sql, self.mode, self.require, self.title, self.length, self.def
)
}
_ => {
let sql = format!(
"`{}` varchar({}){} default '{}'",
self.field, self.length, not_null, self.def
);
format!(
"{} comment '{}|{}|{}|{}|{}'",
sql, 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.clone()))
.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;
self
}
}
pub struct Email {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub def: String,
pub length: i32,
pub show: bool,
pub describe: String,
pub example: JsonValue,
}
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: 100,
show: true,
describe: "".to_string(),
example: JsonValue::Null,
}
}
pub fn is_email(email: &str) -> bool {
lazy_static! {
static ref EMAIL_RE: Regex =
Regex::new(r"(?i)^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$").unwrap();
}
EMAIL_RE.is_match(email)
}
}
impl Field for Email {
fn require(&mut self, require: bool) -> &mut Self {
self.require = require;
self
}
fn sql(&mut self, model: &str) -> String {
let not_null = if self.require { " not null" } else { "" };
match model {
"sqlite" => format!(
"`{}` varchar({}){} default '{}'",
self.field, self.length, not_null, self.def
),
"pgsql" => {
let sql = format!(
r#""{}" varchar({}) default '{}'"#,
self.field, self.length, self.def
);
format!(
"{} --{}|{}|{}|{}|{}",
sql, self.mode, self.require, self.title, self.length, self.def
)
}
_ => {
let sql = format!(
"`{}` varchar({}){} default '{}'",
self.field, self.length, not_null, self.def
);
format!(
"{} comment '{}|{}|{}|{}|{}'",
sql, 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.clone()))
.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 verify(&mut self, data: JsonValue) -> JsonValue {
data
}
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
}
}
pub struct Color {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub def: String,
pub length: i32,
pub show: bool,
pub describe: String,
pub example: JsonValue,
}
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,
show: true,
describe: "".to_string(),
example: JsonValue::Null,
}
}
}
impl Field for Color {
fn require(&mut self, require: bool) -> &mut Self {
self.require = require;
self
}
fn sql(&mut self, model: &str) -> String {
let not_null = if self.require { " not null" } else { "" };
match model {
"sqlite" => format!(
"`{}` varchar({}){} default '{}'",
self.field, self.length, not_null, self.def
),
"pgsql" => {
let sql = format!(
r#""{}" varchar({}) default '{}'"#,
self.field, self.length, self.def
);
format!(
"{} --{}|{}|{}|{}|{}",
sql, self.mode, self.require, self.title, self.length, self.def
)
}
_ => {
let sql = format!(
"`{}` varchar({}){} default '{}'",
self.field, self.length, not_null, self.def
);
format!(
"{} comment '{}|{}|{}|{}|{}'",
sql, 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.clone()))
.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 verify(&mut self, data: JsonValue) -> JsonValue {
data
}
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
}
}
pub struct Code {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub def: String,
pub length: usize,
pub show: bool,
pub describe: String,
pub dec: String,
pub example: JsonValue,
}
impl Code {
pub fn new(
require: bool,
field: &str,
title: &str,
length: usize,
dec: &str,
default: &str,
) -> Self {
Self {
require,
field: field.to_string(),
mode: "code".to_string(),
title: title.to_string(),
def: default.to_string(),
length: length + dec.len(),
show: true,
describe: "".to_string(),
dec: dec.to_string(),
example: JsonValue::Null,
}
}
pub fn order_code(prefix: &str, machine_id: u16) -> String {
let now = Local::now();
let time_str = now.format("%Y%m%d%H%M%S");
let millis = now.timestamp_subsec_millis();
let seq = SEQ.fetch_add(1, Ordering::SeqCst) % 1000;
let rand_part: u16 = rand::rng().random_range(0..1000);
format!(
"{}{}{:02}{:03}{:03}{:03}",
prefix,
time_str,
machine_id % 100,
millis,
rand_part,
seq
)
}
pub fn code_no_13(prefix: &str) -> String {
let data = Datetime::datetime_format("%Y%m%d%H%f");
let data = data.trim_end_matches("00");
format!("{prefix}{data}")
}
pub fn batch_no(prefix: &str) -> String {
let data = Datetime::datetime_format("%Y%m%d%H");
format!("{prefix}{data}")
}
pub fn random_number_f64(begin: f64, finish: f64) -> f64 {
rand::rng().random_range(begin..=finish)
}
pub fn verification_code(len: usize) -> String {
let text = "0123456789";
let mut code = "".to_string();
for _ in 0..len {
let index = rand::rng().random_range(0..=text.len() - 1);
let t = &text[index..=index];
code = format!("{}{}", code, t);
}
code
}
}
impl Field for Code {
fn require(&mut self, require: bool) -> &mut Self {
self.require = require;
self
}
fn sql(&mut self, model: &str) -> String {
let not_null = if self.require { " not null" } else { "" };
match model {
"sqlite" => format!(
"`{}` varchar({}){} default '{}'",
self.field, self.length, not_null, self.def
),
"pgsql" => {
let sql = format!(
r#""{}" varchar({}) default '{}'"#,
self.field, self.length, self.def
);
format!(
"{} --{}|{}|{}|{}|{}",
sql, self.mode, self.require, self.title, self.length, self.def
)
}
_ => {
let sql = format!(
"`{}` varchar({}){} default '{}'",
self.field, self.length, not_null, self.def
);
format!(
"{} comment '{}|{}|{}|{}|{}|{}'",
sql, self.mode, self.require, self.title, self.length, self.dec, 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.clone()))
.unwrap();
field.insert("show", JsonValue::from(self.show)).unwrap();
field
.insert("describe", JsonValue::from(self.describe.clone()))
.unwrap();
field
.insert("dec", JsonValue::from(self.dec.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
}
}
pub struct BarCode {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub def: String,
pub length: usize,
pub show: bool,
pub describe: String,
pub example: JsonValue,
}
impl BarCode {
pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
Self {
require,
field: field.to_string(),
mode: "barcode".to_string(),
title: title.to_string(),
def: default.to_string(),
length: 20,
show: true,
describe: "".to_string(),
example: JsonValue::Null,
}
}
pub fn create_barcode(prefix: &str) -> String {
let data = Datetime::datetime_format("%Y%m%d%H");
format!("{prefix}{data}")
}
}
impl Field for BarCode {
fn require(&mut self, require: bool) -> &mut Self {
self.require = require;
self
}
fn sql(&mut self, model: &str) -> String {
let not_null = if self.require { " not null" } else { "" };
match model {
"sqlite" => format!(
"`{}` varchar({}){} default '{}'",
self.field, self.length, not_null, self.def
),
"pgsql" => {
let sql = format!(
r#""{}" varchar({}) default '{}'"#,
self.field, self.length, self.def
);
format!(
"{} --{}|{}|{}|{}|{}",
sql, self.mode, self.require, self.title, self.length, self.def
)
}
_ => {
let sql = format!(
"`{}` varchar({}){} default '{}'",
self.field, self.length, not_null, self.def
);
format!(
"{} comment '{}|{}|{}|{}|{}'",
sql, 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.clone()))
.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
}
}
pub struct QrCode {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub def: String,
pub length: usize,
pub show: bool,
pub describe: String,
pub example: JsonValue,
}
impl QrCode {
pub fn new(require: bool, field: &str, title: &str, length: usize, default: &str) -> Self {
Self {
require,
field: field.to_string(),
mode: "qrcode".to_string(),
title: title.to_string(),
def: default.to_string(),
length,
show: true,
describe: "".to_string(),
example: JsonValue::Null,
}
}
}
impl Field for QrCode {
fn require(&mut self, require: bool) -> &mut Self {
self.require = require;
self
}
fn sql(&mut self, model: &str) -> String {
let not_null = if self.require { " not null" } else { "" };
match model {
"sqlite" => format!(
"`{}` varchar({}){} default '{}'",
self.field, self.length, not_null, self.def
),
"pgsql" => {
let sql = format!(
r#""{}" varchar({}) default '{}'"#,
self.field, self.length, self.def
);
format!(
"{} --{}|{}|{}|{}|{}",
sql, self.mode, self.require, self.title, self.length, self.def
)
}
_ => {
let sql = format!(
"`{}` varchar({}){} default '{}'",
self.field, self.length, not_null, self.def
);
format!(
"{} comment '{}|{}|{}|{}|{}'",
sql, 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.clone()))
.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
}
}
#[cfg(test)]
mod tests {
#[test]
fn code() {
let data = crate::str::Code::order_code("Y", 1);
println!("code: {} {}", data, data.len());
let data = crate::str::Code::order_code("Y", 1);
println!("code: {} {}", data, data.len());
let data = crate::str::Code::order_code("Y", 2);
println!("code: {} {}", data, data.len());
let data = crate::str::Code::order_code("Y", 2);
println!("code: {} {}", data, data.len());
let data = crate::str::Code::order_code("Y", 3);
println!("code: {} {}", data, data.len());
let data = crate::str::Code::order_code("Y", 3);
println!("code: {} {}", data, data.len());
}
}