1use crate::Field;
2use json::{object, JsonValue};
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(
32 require: bool,
33 field: &str,
34 title: &str,
35 table: &str,
36 pid_field: &str,
37 fields: Vec<&str>,
38 api: &str,
39 ) -> Self {
40 Self {
41 require,
42 field: field.to_string(),
43 mode: "tree".to_string(),
44 title: title.to_string(),
45 table: table.to_string(),
46 api: api.to_string(),
47 show: true,
48 fields: fields.iter().map(|c| c.to_string()).collect(),
49 describe: "".to_string(),
50 def: "".to_string(),
51 multiple: false,
52 multiple_count: 1,
53 pid_field: pid_field.to_string(),
54 example: JsonValue::Null,
55 }
56 }
57 pub fn multiple(&mut self, count: i32) -> &mut Tree {
59 self.multiple = count > 1;
60 self.multiple_count = count;
61 self
62 }
63}
64
65impl Field for Tree {
66 fn sql(&mut self, model: &str) -> String {
67 let not_null = if self.require { " not null" } else { "" };
68 let length = if self.multiple {
69 64 * self.multiple_count + self.multiple_count
70 } else {
71 64
72 };
73 match model {
74 "sqlite" => {
75 format!("{} varchar({}){} default ''", self.field, length, not_null)
76 }
77 "pgsql" => {
78 format!(
79 r#""{}" varchar({}){} default ''"#,
80 self.field, length, not_null
81 )
82 }
83 _ => {
84 let sql = format!(
85 "`{}` varchar({}){} default ''",
86 self.field, length, not_null
87 );
88 format!(
89 "{} comment '{}|{}|{}|{}|{}|{}'",
90 sql.clone(),
91 self.mode,
92 self.title,
93 self.pid_field,
94 self.require,
95 self.multiple,
96 self.multiple_count
97 )
98 }
99 }
100 }
101 fn hide(&mut self) -> &mut Self {
102 self.show = false;
103 self
104 }
105 fn describe(&mut self, text: &str) -> &mut Self {
106 self.describe = text.to_string();
107 self
108 }
109
110 fn field(&mut self) -> JsonValue {
111 let mut field = object! {};
112 field
113 .insert("require", JsonValue::from(self.require))
114 .unwrap();
115 field
116 .insert("field", JsonValue::from(self.field.clone()))
117 .unwrap();
118 field
119 .insert("mode", JsonValue::from(self.mode.clone()))
120 .unwrap();
121 field
122 .insert("title", JsonValue::from(self.title.clone()))
123 .unwrap();
124 field
125 .insert("table", JsonValue::from(self.table.clone()))
126 .unwrap();
127 field
128 .insert("api", JsonValue::from(self.api.clone()))
129 .unwrap();
130 field
131 .insert("fields", JsonValue::from(self.fields.clone()))
132 .unwrap();
133
134 field
135 .insert("def", JsonValue::from(self.def.clone()))
136 .unwrap();
137
138 field
139 .insert("multiple", JsonValue::from(self.multiple))
140 .unwrap();
141 field
142 .insert("multiple_count", JsonValue::from(self.multiple_count))
143 .unwrap();
144
145 field
146 .insert("pid_field", JsonValue::from(self.pid_field.clone()))
147 .unwrap();
148
149 field.insert("show", JsonValue::from(self.show)).unwrap();
150 field
151 .insert("describe", JsonValue::from(self.describe.clone()))
152 .unwrap();
153 field.insert("example", self.example.clone()).unwrap();
154 field
155 }
156
157 fn swagger(&mut self) -> JsonValue {
158 object! {
159 "type": self.mode.clone(),
160 "example": self.example.clone(),
161 }
162 }
163
164 fn example(&mut self, data: JsonValue) -> &mut Self {
165 self.example = data.clone();
166 self
167 }
168}
169
170#[derive(Debug, Clone)]
180pub struct Table {
181 pub require: bool,
182 pub field: String,
183 pub mode: String,
184 pub title: String,
185 pub table: String,
186 pub fields: Vec<String>,
187 pub api: String,
188 pub show: bool,
189 pub describe: String,
190 pub def: String,
191 pub multiple: bool,
192 pub multiple_count: i32,
193 pub example: JsonValue,
194}
195
196impl Table {
197 pub fn new(
198 require: bool,
199 field: &str,
200 title: &str,
201 table: &str,
202 fields: Vec<&str>,
203 api: &str,
204 ) -> Self {
205 Self {
206 require,
207 field: field.to_string(),
208 mode: "table".to_string(),
209 title: title.to_string(),
210 table: table.to_string(),
211 api: api.to_string(),
212 show: true,
213 fields: fields.iter().map(|c| c.to_string()).collect(),
214 describe: "".to_string(),
215 def: "".to_string(),
216 multiple: false,
217 multiple_count: 1,
218 example: JsonValue::Null,
219 }
220 }
221 pub fn multiple(&mut self, count: i32) -> &mut Table {
223 self.multiple = count > 1;
224 self.multiple_count = count;
225 self
226 }
227}
228
229impl Field for Table {
230 fn sql(&mut self, model: &str) -> String {
231 let not_null = if self.require { " not null" } else { "" };
232 let length = if self.multiple {
233 64 * self.multiple_count + self.multiple_count
234 } else {
235 64
236 };
237 match model {
238 "sqlite" => {
239 format!("{} varchar({}){} default ''", self.field, length, not_null)
240 }
241 "pgsql" => {
242 format!(
243 r#""{}" varchar({}){} default ''"#,
244 self.field, length, not_null
245 )
246 }
247 _ => {
248 let sql = format!(
249 "`{}` varchar({}){} default ''",
250 self.field, length, not_null
251 );
252 format!(
253 "{} comment '{}|{}|{}|{}|{}'",
254 sql.clone(),
255 self.mode,
256 self.title,
257 self.require,
258 self.multiple,
259 self.multiple_count
260 )
261 }
262 }
263 }
264 fn hide(&mut self) -> &mut Self {
265 self.show = false;
266 self
267 }
268 fn describe(&mut self, text: &str) -> &mut Self {
269 self.describe = text.to_string();
270 self
271 }
272
273 fn field(&mut self) -> JsonValue {
274 let mut field = object! {};
275 field
276 .insert("require", JsonValue::from(self.require))
277 .unwrap();
278 field
279 .insert("field", JsonValue::from(self.field.clone()))
280 .unwrap();
281 field
282 .insert("mode", JsonValue::from(self.mode.clone()))
283 .unwrap();
284 field
285 .insert("title", JsonValue::from(self.title.clone()))
286 .unwrap();
287 field
288 .insert("table", JsonValue::from(self.table.clone()))
289 .unwrap();
290 field
291 .insert("api", JsonValue::from(self.api.clone()))
292 .unwrap();
293 field
294 .insert("fields", JsonValue::from(self.fields.clone()))
295 .unwrap();
296
297 field
298 .insert("def", JsonValue::from(self.def.clone()))
299 .unwrap();
300
301 field
302 .insert("multiple", JsonValue::from(self.multiple))
303 .unwrap();
304 field
305 .insert("multiple_count", JsonValue::from(self.multiple_count))
306 .unwrap();
307
308 field.insert("show", JsonValue::from(self.show)).unwrap();
309 field
310 .insert("describe", JsonValue::from(self.describe.clone()))
311 .unwrap();
312 field.insert("example", self.example.clone()).unwrap();
313 field
314 }
315
316 fn swagger(&mut self) -> JsonValue {
317 object! {
318 "type": self.mode.clone(),
319 "example": self.example.clone(),
320 }
321 }
322 fn example(&mut self, data: JsonValue) -> &mut Self {
323 self.example = data;
324 self
325 }
326}