use crate::Field;
use json::{object, JsonValue};
pub struct Radio {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub def: String,
pub option: Vec<String>,
pub length: i32,
pub show: bool,
pub describe: String,
pub example: JsonValue,
table_name: String,
}
impl Radio {
pub fn new(
require: bool,
field: &str,
title: &str,
mut option: Vec<&str>,
default: &str,
) -> Self {
if !require && default.is_empty() && !option.contains(&"") {
option.push("")
}
Self {
require,
field: field.to_string(),
mode: "radio".to_string(),
title: title.to_string(),
def: default.to_string(),
option: option.iter().map(|c| c.to_string()).collect(),
length: 0,
show: true,
describe: "".to_string(),
example: JsonValue::Null,
table_name: "".to_string(),
}
}
pub fn table_name(&mut self, table_name: &str) -> &mut Self {
self.table_name = table_name.to_string();
self
}
}
impl Field for Radio {
fn sql(&mut self, model: &str) -> String {
let not_null = if self.require { " not null" } else { "" };
let length = self.option.iter().map(|o| o.len()).max().unwrap_or(1);
match model {
"sqlite" => {
format!(
"`{}` varchar({}){} default '{}'",
self.field, length, not_null, self.def
)
}
"pgsql" => {
let sql = format!(
r#""{}" varchar({}) default '{}'"#,
self.field, length, self.def
);
format!(
"{} --{}|{}|{}|{}|{}|{}",
sql,
self.mode,
self.require,
self.title,
self.length,
self.def,
self.option.join("|")
)
}
_ => {
let sql = format!(
"`{}` set('{}'){} default '{}'",
self.field,
self.option.join("','"),
not_null,
self.def
);
format!(
"{} comment '{}|{}|{}|{}|{}|{}'",
sql,
self.mode,
self.require,
self.title,
self.length,
self.def,
self.option.join("|")
)
}
}
}
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("option", JsonValue::from(self.option.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 {
self.mode = match self.mode.as_str() {
"radio" => "string",
_ => self.mode.as_str(),
}
.to_string();
object! {
"type": self.mode.clone(),
"example": self.example.clone(),
}
}
fn example(&mut self, data: JsonValue) -> &mut Self {
self.example = data.clone();
self
}
}
pub struct Select {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub option: Vec<String>,
pub def: Vec<String>,
pub length: i32,
pub show: bool,
pub describe: String,
pub example: JsonValue,
pub table_name: String,
}
impl Select {
pub fn new(
require: bool,
field: &str,
title: &str,
mut option: Vec<&str>,
default: Vec<&str>,
) -> Self {
if !require && default.is_empty() && !option.contains(&"") {
option.push("")
}
Self {
require,
field: field.to_string(),
mode: "select".to_string(),
title: title.to_string(),
def: default.iter().map(|c| c.to_string()).collect(),
option: option.iter().map(|c| c.to_string()).collect(),
length: 0,
show: true,
describe: "".to_string(),
example: JsonValue::Null,
table_name: String::new(),
}
}
pub fn table_name(&mut self, table_name: &str) -> &mut Self {
self.table_name = table_name.to_string();
self
}
}
impl Field for Select {
fn sql(&mut self, model: &str) -> String {
let not_null = if self.require { " not null" } else { "" };
let n = self.option.len();
let total_chars: usize = self.option.iter().map(|o| o.len()).sum();
let length = if n == 0 {
2
} else {
2 + n * 2 + (n - 1) + total_chars
};
let default_val = if self.def.is_empty() {
"".to_string()
} else {
self.def.join(",")
};
match model {
"sqlite" => {
format!(
"{} varchar({}){} default '{}'",
self.field, length, not_null, default_val
)
}
"pgsql" => {
let json_default = "[]";
let sql = format!(r#""{}" TEXT default '{}'"#, self.field, json_default);
format!(
"{} --{}|{}|{}|{}|{}|{}",
sql,
self.mode,
self.require,
self.title,
self.length,
self.def.join("|"),
self.option.join("|")
)
}
_ => {
let json_default = if self.def.is_empty() {
"('[]')".to_string()
} else {
format!("('[\"{}\"]')", self.def.join("\",\""))
};
let sql = format!("`{}` json default {}", self.field, json_default);
format!(
"{} comment '{}|{}|{}|{}|{}|{}'",
sql,
self.mode,
self.require,
self.title,
self.length,
self.def.join("|"),
self.option.join("|")
)
}
}
}
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("option", JsonValue::from(self.option.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
}
}