use crate::Field;
use json::{object, JsonValue};
#[derive(Debug, Clone)]
pub struct Location {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub length: i32,
pub show: bool,
pub describe: String,
pub def: String,
pub example: JsonValue,
}
impl Location {
pub fn new(require: bool, field: &str, title: &str) -> Self {
Self {
require,
field: field.to_string(),
mode: "location".to_string(),
title: title.to_string(),
length: 0,
def: "".to_string(),
show: true,
describe: "".to_string(),
example: JsonValue::Null,
}
}
pub fn def(&mut self, def: &str) -> &mut Self {
self.def = def.to_string();
self
}
#[allow(dead_code)]
fn length(&mut self, code: i32) -> &mut Self {
self.length = code;
self
}
}
impl Field for Location {
fn sql(&mut self, model: &str) -> String {
let not_null = if self.require { " not null" } else { "" };
match model {
"sqlite" => {
let length = 39;
format!(
"`{}` varchar({}){} default '{}'",
self.field, length, not_null, self.def
)
}
"pgsql" => {
let length = 39;
let sql = format!(
r#""{}" varchar({}) default '{}'"#,
self.field, length, self.def
);
format!(
"{} --{}|{}|{}|{}|{}",
sql, self.title, self.mode, self.require, self.def, self.length
)
}
"mysql" => {
let sql = format!("`{}` POINT SRID {}{}", self.field, self.length, not_null);
format!(
"{} comment '{}|{}|{}|{}|{}'",
sql, self.title, self.mode, self.require, self.def, self.length
)
}
_ => {
let sql = format!(
"`{}` varchar({}){} default '{}'",
self.field, self.length, not_null, self.def
);
format!(
"{} comment '{}|{}|{}|{}|{}'",
sql, self.title, self.mode, self.require, self.def, 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("show", JsonValue::from(self.show)).unwrap();
field
.insert("describe", JsonValue::from(self.describe.clone()))
.unwrap();
field
.insert("def", JsonValue::from(self.def.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 Address {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub length: usize,
pub show: bool,
pub describe: String,
pub def: String,
pub example: JsonValue,
}
impl Address {
pub fn new(require: bool, field: &str, title: &str, length: usize, def: &str) -> Self {
Self {
require,
field: field.to_string(),
mode: "address".to_string(),
title: title.to_string(),
length,
def: def.to_string(),
show: true,
describe: "".to_string(),
example: JsonValue::Null,
}
}
}
impl Field for Address {
fn sql(&mut self, model: &str) -> String {
let not_null = if self.require { " not null" } else { "" };
match model {
"sqlite" => {
format!(
"`{}` varchar({}){} default '{}'",
self.field, self.length, not_null, self.def
)
}
"pgsql" => {
let sql = format!(
r#""{}" varchar({}) default '{}' "#,
self.field, self.length, self.def
);
format!(
"{} --{}|{}|{}|{}|{}",
sql, self.title, self.mode, self.require, self.def, self.length
)
}
_ => {
let sql = format!(
"`{}` varchar({}){} default '{}'",
self.field, self.length, not_null, self.def
);
format!(
"{} comment '{}|{}|{}|{}|{}'",
sql, self.title, self.mode, self.require, self.def, 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("show", JsonValue::from(self.show)).unwrap();
field
.insert("describe", JsonValue::from(self.describe.clone()))
.unwrap();
field
.insert("def", JsonValue::from(self.def.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 LocationAddress {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub length: i32,
pub show: bool,
pub describe: String,
pub def: JsonValue,
pub example: JsonValue,
}
impl LocationAddress {
pub fn new(require: bool, field: &str, title: &str, def: JsonValue) -> Self {
Self {
require,
field: field.to_string(),
mode: "location_address".to_string(),
title: title.to_string(),
length: 0,
def,
show: true,
describe: "".to_string(),
example: JsonValue::Null,
}
}
pub fn length(mut self, length: i32) -> Self {
self.length = length;
self
}
}
impl Field for LocationAddress {
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, self.length)
}
_ => {
let sql = format!("`{}` TEXT{} default '{}'", self.field, not_null, self.def);
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("show", JsonValue::from(self.show)).unwrap();
field
.insert("describe", JsonValue::from(self.describe.clone()))
.unwrap();
field.insert("def", self.def.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 Polygon {
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 Polygon {
pub fn new(require: bool, field: &str, title: &str, def: JsonValue) -> Self {
Self {
require,
field: field.to_string(),
mode: "polygon".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 Polygon {
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, self.length)
}
_ => {
let sql = match self.length {
1 => format!(
"`{}` MEDIUMTEXT{} default '{}'",
self.field, not_null, self.def
),
2 => format!(
"`{}` LONGTEXT{} default '{}'",
self.field, not_null, self.def
),
_ => format!("`{}` TEXT{} default '{}'", self.field, not_null, self.def),
};
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.clone();
self
}
}
#[cfg(test)]
mod test {
use crate::Field;
#[test]
fn test() {
let res = crate::location::Location::new(true, "str", "地理定位")
.describe("1313")
.length(0)
.field();
println!("{res:#}");
}
}