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 show: bool,
pub describe: 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,
show: true,
describe: "".to_string(),
}
}
pub fn length(mut self, length: i32) -> Self {
self.length = length;
self
}
}
impl Field for Text {
fn sql(&mut self, model: &str) -> String {
match model {
"sqlite" => {
format!("`{}` TEXT default '{}' ", self.field, self.def)
}
_ => {
let sql = match self.length {
1 => format!("`{}` MEDIUMTEXT ", self.field),
2 => format!("`{}` LONGTEXT ", self.field),
_ => format!("`{}` text ", self.field)
};
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.insert("show", JsonValue::from(self.show.clone())).unwrap();
field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
field
}
fn hide(&mut self) -> &mut Self {
self.show = false;
self
}
fn describe(&mut self, text: &str) -> &mut Self {
self.describe = text.to_string();
self
}
}
pub struct Editor {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub def: String,
pub length: i32,
pub show: bool,
pub describe: String,
}
impl Editor {
pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
Self {
require,
field: field.to_string(),
mode: "editor".to_string(),
title: title.to_string(),
def: default.to_string(),
length: 0,
show: true,
describe: "".to_string(),
}
}
pub fn length(mut self, length: i32) -> Self {
self.length = length;
self
}
}
impl Field for Editor {
fn sql(&mut self, model: &str) -> String {
match model {
"sqlite" => {
format!("`{}` TEXT default '{}' ", self.field, self.def)
}
_ => {
let sql = match self.length {
1 => format!("`{}` MEDIUMTEXT ", self.field),
2 => format!("`{}` LONGTEXT ", self.field),
_ => format!("`{}` text ", self.field)
};
format!("{} comment '{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length)
}
}
}
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("show", JsonValue::from(self.show.clone())).unwrap();
field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
field
}
fn hide(&mut self) -> &mut Self {
self.show = false;
self
}
fn describe(&mut self, text: &str) -> &mut Self {
self.describe = text.to_string();
self
}
}
pub struct Json {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub def: JsonValue,
pub length: i32,
pub show: bool,
pub describe: String,
}
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,
show: true,
describe: "".to_string(),
}
}
pub fn length(mut self, length: i32) -> Self {
self.length = length;
self
}
}
impl Field for Json {
fn sql(&mut self, model: &str) -> String {
match model {
"sqlite" => {
format!("`{}` TEXT default '{}' ", self.field, self.def)
}
_ => {
let sql = match self.length {
1 => format!("`{}` MEDIUMTEXT ", self.field),
2 => format!("`{}` LONGTEXT ", self.field),
_ => format!("`{}` text ", self.field)
};
format!("{} comment '{}|{}|{}'", sql.clone(), self.mode, self.title, self.length)
}
}
}
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("def", JsonValue::from(self.def.clone())).unwrap();
field.insert("show", JsonValue::from(self.show.clone())).unwrap();
field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
field
}
fn hide(&mut self) -> &mut Self {
self.show = false;
self
}
fn describe(&mut self, text: &str) -> &mut Self {
self.describe = text.to_string();
self
}
}
pub struct Array {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub def: JsonValue,
pub length: i32,
pub show: bool,
pub describe: String,
}
impl Array {
pub fn new(require: bool, field: &str, title: &str, def: JsonValue) -> Self {
Self {
require,
field: field.to_string(),
mode: "array".to_string(),
title: title.to_string(),
def,
length: 0,
show: true,
describe: "".to_string(),
}
}
pub fn length(mut self, length: i32) -> Self {
self.length = length;
self
}
}
impl Field for Array {
fn sql(&mut self, model: &str) -> String {
match model {
"sqlite" => {
format!("`{}` TEXT default '{}' ", self.field, self.def)
}
_ => {
let sql = match self.length {
1 => format!("`{}` MEDIUMTEXT ", self.field),
2 => format!("`{}` LONGTEXT ", self.field),
_ => format!("`{}` text ", self.field)
};
format!("{} comment '{}|{}|{}'", sql.clone(), self.mode, self.title, self.length)
}
}
}
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("def", JsonValue::from(self.def.clone())).unwrap();
field.insert("show", JsonValue::from(self.show.clone())).unwrap();
field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
field
}
fn hide(&mut self) -> &mut Self {
self.show = false;
self
}
fn describe(&mut self, text: &str) -> &mut Self {
self.describe = text.to_string();
self
}
}
pub struct Object {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub def: JsonValue,
pub length: i32,
pub show: bool,
pub describe: String,
}
impl Object {
pub fn new(require: bool, field: &str, title: &str, def: JsonValue) -> Self {
Self {
require,
field: field.to_string(),
mode: "object".to_string(),
title: title.to_string(),
def,
length: 0,
show: true,
describe: "".to_string(),
}
}
pub fn length(mut self, length: i32) -> Self {
self.length = length;
self
}
}
impl Field for Object {
fn sql(&mut self, model: &str) -> String {
match model {
"sqlite" => {
format!("`{}` TEXT default '{}' ", self.field, self.def)
}
_ => {
let sql = match self.length {
1 => format!("`{}` MEDIUMTEXT ", self.field),
2 => format!("`{}` LONGTEXT ", self.field),
_ => format!("`{}` text ", self.field)
};
format!("{} comment '{}|{}|{}'", sql.clone(), self.mode, self.title, self.length)
}
}
}
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("def", JsonValue::from(self.def.clone())).unwrap();
field.insert("show", JsonValue::from(self.show.clone())).unwrap();
field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
field
}
fn hide(&mut self) -> &mut Self {
self.show = false;
self
}
fn describe(&mut self, text: &str) -> &mut Self {
self.describe = text.to_string();
self
}
}
pub struct Url {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub def: String,
pub length: i32,
pub show: bool,
pub describe: 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,
show: true,
describe: "".to_string(),
}
}
}
impl Field for Url {
fn sql(&mut self, model: &str) -> String {
match model {
"sqlite" => {
format!("`{}` TEXT default '{}' ", self.field, self.def)
}
_ => {
let sql = format!("`{}` text ", self.field);
format!("{} comment '{}|{}'", sql.clone(), self.mode, self.title)
}
}
}
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("show", JsonValue::from(self.show.clone())).unwrap();
field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
field
}
fn hide(&mut self) -> &mut Self {
self.show = false;
self
}
fn describe(&mut self, text: &str) -> &mut Self {
self.describe = text.to_string();
self
}
}