1use json::{JsonValue, object};
2use crate::Field;
3
4pub struct Dict {
14 pub require: bool,
15 pub field: String,
16 pub mode: String,
17 pub title: String,
18 pub table: String,
19 pub fields: String,
20 pub api: String,
21 pub def: String,
22 pub show: bool,
23 pub describe: String,
24 pub multiple: bool,
25 pub multiple_count: i32,
26 pub example: JsonValue,
27}
28
29impl Dict {
30 pub fn new(require: bool, field: &str, title: &str, table: &str, fields: &str, api: &str) -> Self {
31 Self {
32 require,
33 field: field.to_string(),
34 mode: "dict".to_string(),
35 title: title.to_string(),
36 table: table.to_string(),
37 api: api.to_string(),
38 show: true,
39 fields: fields.to_string(),
40 describe: "".to_string(),
41 def: "".to_string(),
42 multiple: false,
43 multiple_count: 1,
44 example: JsonValue::Null,
45 }
46 }
47 pub fn multiple(&mut self, count: i32) -> &mut Dict {
49 self.multiple = count > 1;
50 self.multiple_count = count;
51 self
52 }
53 pub fn default(&mut self, def: &str) -> &mut Dict {
55 self.def = def.to_string();
56 self
57 }
58}
59
60impl Field for Dict {
61 fn sql(&mut self, model: &str) -> String {
62 match model {
63 "sqlite" => {
64 format!("{} varchar({}) default '{}'", self.field, {
65 if self.multiple {
66 64 * self.multiple_count + self.multiple_count
67 } else {
68 64
69 }
70 }, self.def)
71 }
72 _ => {
73 let sql = format!("{} varchar({}) default '{}'", self.field, {
74 if self.multiple {
75 64 * self.multiple_count + self.multiple_count
76 } else {
77 64
78 }
79 }, self.def);
80 format!("{} comment '{}|{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.title, self.require, self.multiple, self.multiple_count, self.def)
81 }
82 }
83 }
84 fn hide(&mut self) -> &mut Self {
85 self.show = false;
86 self
87 }
88 fn describe(&mut self, text: &str) -> &mut Self {
89 self.describe = text.to_string();
90 self
91 }
92
93 fn field(&mut self) -> JsonValue {
94 let mut field = object! {};
95 field.insert("require", JsonValue::from(self.require)).unwrap();
96 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
97 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
98 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
99 field.insert("table", JsonValue::from(self.table.clone())).unwrap();
100 field.insert("api", JsonValue::from(self.api.clone())).unwrap();
101 field.insert("fields", JsonValue::from(self.fields.clone())).unwrap();
102 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
103 field.insert("multiple", JsonValue::from(self.multiple)).unwrap();
104 field.insert("multiple_count", JsonValue::from(self.multiple_count)).unwrap();
105
106 field.insert("show", JsonValue::from(self.show)).unwrap();
107 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
108 field.insert("example", self.example.clone()).unwrap();
109 field
110 }
111
112 fn swagger(&mut self) -> JsonValue {
113 object! {
114 "type": self.mode.clone(),
115 "example": self.example.clone(),
116 }
117 }
118
119 fn example(&mut self, data: JsonValue) -> &mut Self {
120 self.example = data.clone();
121 self
122 }
123}