use crate::Field;
use json::{object, JsonValue};
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,
pub example: JsonValue,
}
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(),
example: JsonValue::Null,
}
}
pub fn length(mut self, length: i32) -> Self {
self.length = length;
self
}
}
impl Field for Text {
fn sql(&mut self, model: &str) -> String {
let not_null = if self.require { " not null" } else { "" };
match model {
"sqlite" => {
format!("`{}` TEXT{} default '{}'", self.field, not_null, self.def)
}
"pgsql" => {
let sql = format!(r#""{}" TEXT default '{}'"#, self.field, self.def);
format!(
"{} --{}|{}|{}|{}|{}",
sql, self.mode, self.require, self.title, self.length, self.def
)
}
_ => {
let sql = match self.length {
1 => format!("`{}` MEDIUMTEXT{}", self.field, not_null),
2 => format!("`{}` LONGTEXT{}", self.field, not_null),
_ => format!("`{}` text{}", self.field, not_null),
};
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 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,
pub example: JsonValue,
}
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(),
example: JsonValue::Null,
}
}
pub fn length(mut self, length: i32) -> Self {
self.length = length;
self
}
}
impl Field for Editor {
fn sql(&mut self, model: &str) -> String {
let not_null = if self.require { " not null" } else { "" };
match model {
"sqlite" => {
format!("`{}` TEXT{} default '{}'", self.field, not_null, self.def)
}
"pgsql" => {
let sql = format!(r#""{}" TEXT default '{}'"#, self.field, self.def);
format!(
"{} --{}|{}|{}|{}",
sql, self.mode, self.require, self.title, self.length
)
}
_ => {
let sql = match self.length {
1 => format!("`{}` MEDIUMTEXT{}", self.field, not_null),
2 => format!("`{}` LONGTEXT{}", self.field, not_null),
_ => format!("`{}` text{}", self.field, not_null),
};
format!(
"{} comment '{}|{}|{}|{}'",
sql, self.mode, self.require, self.title, self.length
)
}
}
}
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 hide(&mut self) -> &mut Self {
self.show = false;
self
}
fn describe(&mut self, text: &str) -> &mut Self {
self.describe = text.to_string();
self
}
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 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,
pub items: JsonValue,
pub example: JsonValue,
pub max_items: i32,
}
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(),
items: JsonValue::Null,
example: JsonValue::Null,
max_items: 0,
}
}
pub fn length(mut self, length: i32) -> Self {
self.length = length;
self
}
pub fn items(mut self, item: JsonValue) -> Self {
self.items = item;
self
}
pub fn max_items(mut self, mun: i32) -> Self {
self.max_items = mun;
self
}
}
impl Field for Array {
fn sql(&mut self, model: &str) -> String {
let not_null = if self.require { " not null" } else { "" };
let def_str = self.def.to_string();
match model {
"sqlite" => {
format!("`{}` TEXT{} default '{}'", self.field, not_null, def_str)
}
"pgsql" => {
let sql = format!(r#""{}" TEXT default '{}'"#, self.field, def_str);
format!("{} --{}|{}|{}", sql, self.mode, self.title, self.length)
}
_ => {
let sql = match self.length {
1 => format!("`{}` MEDIUMTEXT{}", self.field, not_null),
2 => format!("`{}` LONGTEXT{}", self.field, not_null),
_ => format!("`{}` text{}", self.field, 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", self.def.clone()).unwrap();
field.insert("show", JsonValue::from(self.show)).unwrap();
field
.insert("describe", JsonValue::from(self.describe.clone()))
.unwrap();
field.insert("items", self.items.clone()).unwrap();
field.insert("example", self.example.clone()).unwrap();
field
.insert("max_items", JsonValue::from(self.max_items))
.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(Debug, Clone)]
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,
pub example: JsonValue,
pub items: JsonValue,
}
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(),
example: JsonValue::Null,
items: JsonValue::Null,
}
}
pub fn length(mut self, length: i32) -> Self {
self.length = length;
self
}
pub fn items(mut self, item: JsonValue) -> Self {
self.items = item;
self
}
}
impl Field for Object {
fn sql(&mut self, model: &str) -> String {
let not_null = if self.require { " not null" } else { "" };
let def_str = self.def.to_string();
match model {
"sqlite" => {
format!("`{}` TEXT{} default '{}'", self.field, not_null, def_str)
}
"pgsql" => {
let sql = format!(r#""{}" TEXT default '{}'"#, self.field, def_str);
format!("{} --{}|{}|{}", sql, self.mode, self.title, self.length)
}
_ => {
let sql = match self.length {
1 => format!("`{}` MEDIUMTEXT{}", self.field, not_null),
2 => format!("`{}` LONGTEXT{}", self.field, not_null),
_ => format!("`{}` text{}", self.field, not_null),
};
format!("{} comment '{}|{}'", sql, self.mode, 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("length", JsonValue::from(self.length))
.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.clone();
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,
pub example: JsonValue,
}
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(),
example: JsonValue::Null,
}
}
}
impl Field for Url {
fn sql(&mut self, model: &str) -> String {
let not_null = if self.require { " not null" } else { "" };
match model {
"sqlite" => {
format!("`{}` TEXT{} default '{}'", self.field, not_null, self.def)
}
"pgsql" => {
let sql = format!(r#""{}" TEXT default '{}'"#, self.field, self.def);
format!("{} --{}|{}", sql, self.mode, self.title)
}
_ => {
let sql = format!("`{}` text{}", self.field, not_null);
format!("{} comment '{}|{}'", sql, self.mode, 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("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
}
}