use json::{JsonValue, object};
use crate::Field;
pub struct Text {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub def: String,
pub length: i32,
pub dec: String,
}
impl Text {
pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
Self {
require,
field: field.to_string(),
mode: "text".to_string(),
title: title.to_string(),
def: default.to_string(),
length: 0,
dec: "".to_string(),
}
}
}
impl Field for Text {
fn sql(&mut self, model: &str) -> String {
let sql = format!("{} text", self.field);
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 File {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub length: i32,
pub encrypt: bool,
pub size: usize,
pub option: Vec<String>,
}
impl File {
pub fn new(require: bool, field: &str, title: &str, length: i32, encrypt: bool, size: usize, option: Vec<&str>) -> Self {
let len = {
if length <= 0 {
1
} else {
length
}
};
let size = {
if size <= 0 {
1024 * 1024
} else {
size
}
};
Self {
require,
field: field.to_string(),
mode: "file".to_string(),
title: title.to_string(),
length: len,
encrypt,
size,
option: option.iter().map(|c| c.to_string()).collect(),
}
}
}
impl Field for File {
fn sql(&mut self, model: &str) -> String {
let sql = format!("{} text", self.field);
let options = format!("{}", self.option.join(","));
match model {
"sqlite" => sql,
_ => format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.length, options)
}
}
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("encrypt", JsonValue::from(self.encrypt.clone())).unwrap();
field.insert("size", JsonValue::from(self.size.clone())).unwrap();
field.insert("option", JsonValue::from(self.option.clone())).unwrap();
field
}
}
pub struct Table {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub table: String,
pub fields: Vec<String>,
pub api: String,
}
impl Table {
pub fn new(require: bool, field: &str, title: &str, table: &str, fields: Vec<&str>, api: &str) -> Self {
Self {
require,
field: field.to_string(),
mode: "table".to_string(),
title: title.to_string(),
table: table.to_string(),
api: api.to_string(),
fields: fields.iter().map(|c| c.to_string()).collect(),
}
}
}
impl Field for Table {
fn sql(&mut self, model: &str) -> String {
let sql = format!("{} text", self.field);
match model {
"sqlite" => sql,
_ => format!("{} comment '{}|{}|{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.table, self.fields.join("|"), self.api)
}
}
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("table", JsonValue::from(self.table.clone())).unwrap();
field.insert("api", JsonValue::from(self.api.clone())).unwrap();
field.insert("fields", JsonValue::from(self.fields.clone())).unwrap();
field
}
}
pub struct Json {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub def: JsonValue,
pub length: i32,
}
impl Json {
pub fn new(require: bool, field: &str, title: &str, def: JsonValue) -> Self {
Self {
require,
field: field.to_string(),
mode: "json".to_string(),
title: title.to_string(),
def,
length: 0,
}
}
}
impl Field for Json {
fn sql(&mut self, model: &str) -> String {
let sql = format!("{} text", self.field);
match model {
"sqlite" => sql,
_ => format!("{} comment '{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.def.to_string())
}
}
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("table", JsonValue::from(self.def.clone())).unwrap();
field
}
}
pub struct Url {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub def: String,
pub length: i32,
pub dec: String,
}
impl Url {
pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
Self {
require,
field: field.to_string(),
mode: "url".to_string(),
title: title.to_string(),
def: default.to_string(),
length: 0,
dec: "".to_string(),
}
}
}
impl Field for Url {
fn sql(&mut self, model: &str) -> String {
let sql = format!("{} text", self.field);
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
}
}