use crate::Field;
use json::{object, JsonValue};
pub struct Tree {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub table: String,
pub pid_field: String,
pub fields: Vec<String>,
pub api: String,
pub show: bool,
pub describe: String,
pub def: String,
pub multiple: bool,
pub multiple_count: i32,
pub example: JsonValue,
}
impl Tree {
pub fn new(
require: bool,
field: &str,
title: &str,
table: &str,
pid_field: &str,
fields: Vec<&str>,
api: &str,
) -> Self {
Self {
require,
field: field.to_string(),
mode: "tree".to_string(),
title: title.to_string(),
table: table.to_string(),
api: api.to_string(),
show: true,
fields: fields.iter().map(|c| c.to_string()).collect(),
describe: "".to_string(),
def: "".to_string(),
multiple: false,
multiple_count: 1,
pid_field: pid_field.to_string(),
example: JsonValue::Null,
}
}
pub fn multiple(&mut self, count: i32) -> &mut Tree {
self.multiple = count > 1;
self.multiple_count = count;
self
}
}
impl Field for Tree {
fn sql(&mut self, model: &str) -> String {
let not_null = if self.require { " not null" } else { "" };
let length = if self.multiple {
64 * self.multiple_count + self.multiple_count
} else {
64
};
match model {
"sqlite" => {
format!(
"`{}` varchar({}){} default ''",
self.field, length, not_null
)
}
"pgsql" => {
let sql = format!(r#""{}" varchar({}) default ''"#, self.field, length);
format!(
"{} --{}|{}|{}|{}|{}|{}",
sql,
self.mode,
self.title,
self.pid_field,
self.require,
self.multiple,
self.multiple_count
)
}
_ => {
let sql = format!(
"`{}` varchar({}){} default ''",
self.field, length, not_null
);
format!(
"{} comment '{}|{}|{}|{}|{}|{}'",
sql,
self.mode,
self.title,
self.pid_field,
self.require,
self.multiple,
self.multiple_count
)
}
}
}
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("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
.insert("def", JsonValue::from(self.def.clone()))
.unwrap();
field
.insert("multiple", JsonValue::from(self.multiple))
.unwrap();
field
.insert("multiple_count", JsonValue::from(self.multiple_count))
.unwrap();
field
.insert("pid_field", JsonValue::from(self.pid_field.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
}
}
#[derive(Debug, Clone)]
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,
pub show: bool,
pub describe: String,
pub def: String,
pub multiple: bool,
pub multiple_count: i32,
pub example: JsonValue,
}
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(),
show: true,
fields: fields.iter().map(|c| c.to_string()).collect(),
describe: "".to_string(),
def: "".to_string(),
multiple: false,
multiple_count: 1,
example: JsonValue::Null,
}
}
pub fn multiple(&mut self, count: i32) -> &mut Table {
self.multiple = count > 1;
self.multiple_count = count;
self
}
}
impl Field for Table {
fn sql(&mut self, model: &str) -> String {
let not_null = if self.require { " not null" } else { "" };
let length = if self.multiple {
64 * self.multiple_count + self.multiple_count
} else {
64
};
match model {
"sqlite" => {
format!(
"`{}` varchar({}){} default ''",
self.field, length, not_null
)
}
"pgsql" => {
let sql = format!(r#""{}" varchar({}) default ''"#, self.field, length);
format!(
"{} --{}|{}|{}|{}|{}",
sql, self.mode, self.title, self.require, self.multiple, self.multiple_count
)
}
_ => {
let sql = format!(
"`{}` varchar({}){} default ''",
self.field, length, not_null
);
format!(
"{} comment '{}|{}|{}|{}|{}'",
sql, self.mode, self.title, self.require, self.multiple, self.multiple_count
)
}
}
}
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("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
.insert("def", JsonValue::from(self.def.clone()))
.unwrap();
field
.insert("multiple", JsonValue::from(self.multiple))
.unwrap();
field
.insert("multiple_count", JsonValue::from(self.multiple_count))
.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(Debug, Clone)]
pub struct TableMultiple {
pub require: bool,
pub field: String,
pub title: String,
pub table: String,
pub fields: Vec<String>,
pub api: String,
pub show: bool,
pub describe: String,
pub multiple_count: i32,
pub example: JsonValue,
}
impl TableMultiple {
pub fn new(
require: bool,
field: &str,
title: &str,
table: &str,
fields: Vec<&str>,
api: &str,
) -> Self {
Self {
require,
field: field.to_string(),
title: title.to_string(),
table: table.to_string(),
api: api.to_string(),
show: true,
fields: fields.iter().map(|c| c.to_string()).collect(),
describe: "".to_string(),
multiple_count: 0,
example: json::array![],
}
}
pub fn multiple_count(mut self, count: i32) -> Self {
self.multiple_count = count;
self
}
}
impl Field for TableMultiple {
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)
}
"pgsql" => {
let sql = format!(r#""{}" TEXT default '[]'"#, self.field);
format!(
"{} --table_multiple|{}|{}",
sql, self.title, self.multiple_count
)
}
_ => {
let sql = format!("`{}` text{}", self.field, not_null);
format!(
"{} comment 'table_multiple|{}|{}'",
sql, self.title, self.multiple_count
)
}
}
}
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("table_multiple"))
.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.insert("def", JsonValue::from("")).unwrap();
field.insert("multiple", JsonValue::from(true)).unwrap();
field
.insert("multiple_count", JsonValue::from(self.multiple_count))
.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": "table_multiple",
"example": self.example.clone(),
}
}
fn example(&mut self, data: JsonValue) -> &mut Self {
self.example = data;
self
}
}