use json::{JsonValue, object};
use crate::{Field};
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
}
}
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
}
}
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());
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
}
}
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
}
}
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
}
}
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
}
}