1use json::{JsonValue, object};
2use crate::Field;
3
4pub struct Tree {
14 pub require: bool,
15 pub field: String,
16 pub mode: String,
17 pub title: String,
18 pub table: String,
19 pub pid_field: String,
20 pub fields: Vec<String>,
21 pub api: String,
22 pub show: bool,
23 pub describe: String,
24 pub def: String,
25 pub multiple: bool,
26 pub multiple_count: i32,
27 pub example: JsonValue,
28}
29
30impl Tree {
31 pub fn new(require: bool, field: &str, title: &str, table: &str, pid_field: &str, fields: Vec<&str>, api: &str) -> Self {
32 Self {
33 require,
34 field: field.to_string(),
35 mode: "tree".to_string(),
36 title: title.to_string(),
37 table: table.to_string(),
38 api: api.to_string(),
39 show: true,
40 fields: fields.iter().map(|c| c.to_string()).collect(),
41 describe: "".to_string(),
42 def: "".to_string(),
43 multiple: false,
44 multiple_count: 1,
45 pid_field: pid_field.to_string(),
46 example: JsonValue::Null,
47 }
48 }
49 pub fn multiple(&mut self, count: i32) -> &mut Tree {
51 self.multiple = count > 1;
52 self.multiple_count = count;
53 self
54 }
55}
56
57impl Field for Tree {
58 fn sql(&mut self, model: &str) -> String {
59 match model {
60 "sqlite" => {
61 format!("`{}` varchar({}) default ''", self.field, {
62 if self.multiple {
63 64 * self.multiple_count + self.multiple_count
64 } else {
65 64
66 }
67 })
68 }
69 "pgsql" => {
70 let sql = format!("{} varchar({}) default ''", self.field, {
71 if self.multiple {
72 64 * self.multiple_count + self.multiple_count
73 } else {
74 64
75 }
76 });
77 format!("{} comment '{}|{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.title, self.pid_field, self.require, self.multiple, self.multiple_count)
78 }
79 _ => {
80 let sql = format!("`{}` varchar({}) default ''", self.field, {
81 if self.multiple {
82 64 * self.multiple_count + self.multiple_count
83 } else {
84 64
85 }
86 });
87 format!("{} comment '{}|{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.title, self.pid_field, self.require, self.multiple, self.multiple_count)
88 }
89 }
90 }
91 fn hide(&mut self) -> &mut Self {
92 self.show = false;
93 self
94 }
95 fn describe(&mut self, text: &str) -> &mut Self {
96 self.describe = text.to_string();
97 self
98 }
99
100 fn field(&mut self) -> JsonValue {
101 let mut field = object! {};
102 field.insert("require", JsonValue::from(self.require)).unwrap();
103 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
104 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
105 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
106 field.insert("table", JsonValue::from(self.table.clone())).unwrap();
107 field.insert("api", JsonValue::from(self.api.clone())).unwrap();
108 field.insert("fields", JsonValue::from(self.fields.clone())).unwrap();
109
110 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
111
112 field.insert("multiple", JsonValue::from(self.multiple)).unwrap();
113 field.insert("multiple_count", JsonValue::from(self.multiple_count)).unwrap();
114
115 field.insert("pid_field", JsonValue::from(self.pid_field.clone())).unwrap();
116
117 field.insert("show", JsonValue::from(self.show)).unwrap();
118 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
119 field.insert("example", self.example.clone()).unwrap();
120 field
121 }
122
123 fn swagger(&mut self) -> JsonValue {
124 object! {
125 "type": self.mode.clone(),
126 "example": self.example.clone(),
127 }
128 }
129
130 fn example(&mut self, data: JsonValue) -> &mut Self {
131 self.example = data.clone();
132 self
133 }
134}
135
136#[derive(Debug, Clone)]
146pub struct Table {
147 pub require: bool,
148 pub field: String,
149 pub mode: String,
150 pub title: String,
151 pub table: String,
152 pub fields: Vec<String>,
153 pub api: String,
154 pub show: bool,
155 pub describe: String,
156 pub def: String,
157 pub multiple: bool,
158 pub multiple_count: i32,
159 pub example: JsonValue,
160}
161
162impl Table {
163 pub fn new(require: bool, field: &str, title: &str, table: &str, fields: Vec<&str>, api: &str) -> Self {
164 Self {
165 require,
166 field: field.to_string(),
167 mode: "table".to_string(),
168 title: title.to_string(),
169 table: table.to_string(),
170 api: api.to_string(),
171 show: true,
172 fields: fields.iter().map(|c| c.to_string()).collect(),
173 describe: "".to_string(),
174 def: "".to_string(),
175 multiple: false,
176 multiple_count: 1,
177 example: JsonValue::Null,
178 }
179 }
180 pub fn multiple(&mut self, count: i32) -> &mut Table {
182 self.multiple = count > 1;
183 self.multiple_count = count;
184 self
185 }
186}
187
188impl Field for Table {
189 fn sql(&mut self, model: &str) -> String {
190 match model {
191 "sqlite" => {
192 format!("`{}` varchar({}) default ''", self.field, {
193 if self.multiple {
194 64 * self.multiple_count + self.multiple_count
195 } else {
196 64
197 }
198 })
199 }
200 "pgsql" => {
201 let sql = format!("{} varchar({}) default ''", self.field, {
202 if self.multiple {
203 64 * self.multiple_count + self.multiple_count
204 } else {
205 64
206 }
207 });
208 format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.title, self.require, self.multiple, self.multiple_count)
209 }
210 _ => {
211 let sql = format!("`{}` varchar({}) default ''", self.field, {
212 if self.multiple {
213 64 * self.multiple_count + self.multiple_count
214 } else {
215 64
216 }
217 });
218 format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.title, self.require, self.multiple, self.multiple_count)
219 }
220 }
221 }
222 fn hide(&mut self) -> &mut Self {
223 self.show = false;
224 self
225 }
226 fn describe(&mut self, text: &str) -> &mut Self {
227 self.describe = text.to_string();
228 self
229 }
230
231 fn field(&mut self) -> JsonValue {
232 let mut field = object! {};
233 field.insert("require", JsonValue::from(self.require)).unwrap();
234 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
235 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
236 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
237 field.insert("table", JsonValue::from(self.table.clone())).unwrap();
238 field.insert("api", JsonValue::from(self.api.clone())).unwrap();
239 field.insert("fields", JsonValue::from(self.fields.clone())).unwrap();
240
241 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
242
243 field.insert("multiple", JsonValue::from(self.multiple)).unwrap();
244 field.insert("multiple_count", JsonValue::from(self.multiple_count)).unwrap();
245
246 field.insert("show", JsonValue::from(self.show)).unwrap();
247 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
248 field.insert("example", self.example.clone()).unwrap();
249 field
250 }
251
252 fn swagger(&mut self) -> JsonValue {
253 object! {
254 "type": self.mode.clone(),
255 "example": self.example.clone(),
256 }
257 }
258 fn example(&mut self, data: JsonValue) -> &mut Self {
259 self.example = data;
260 self
261 }
262}