1use crate::Field;
2use json::{object, JsonValue};
3
4pub struct Text {
14 pub require: bool,
15 pub field: String,
16 pub mode: String,
17 pub title: String,
18 pub def: String,
19 pub length: i32,
20 pub show: bool,
21 pub describe: String,
22 pub example: JsonValue,
23}
24
25impl Text {
26 pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
27 Self {
28 require,
29 field: field.to_string(),
30 mode: "text".to_string(),
31 title: title.to_string(),
32 def: default.to_string(),
33 length: 0,
34 show: true,
35 describe: "".to_string(),
36 example: JsonValue::Null,
37 }
38 }
39 pub fn length(mut self, length: i32) -> Self {
43 self.length = length;
44 self
45 }
46}
47
48impl Field for Text {
49 fn sql(&mut self, model: &str) -> String {
50 match model {
51 "sqlite" => {
52 format!("`{}` TEXT default '{}' ", self.field, self.def)
53 }
54 "pgsql" => {
55 let sql = format!(r#""{}" TEXT"#, self.field);
56 format!(
57 "{} comment '{}|{}|{}|{}|{}'",
58 sql.clone(),
59 self.mode,
60 self.require,
61 self.title,
62 self.length,
63 self.def
64 )
65 }
66 _ => {
67 let sql = match self.length {
68 1 => format!("`{}` MEDIUMTEXT ", self.field),
69 2 => format!("`{}` LONGTEXT ", self.field),
70 _ => format!("`{}` text ", self.field),
71 };
72 format!(
73 "{} comment '{}|{}|{}|{}|{}'",
74 sql.clone(),
75 self.mode,
76 self.require,
77 self.title,
78 self.length,
79 self.def
80 )
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("length", JsonValue::from(self.length)).unwrap();
100 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
101
102 field.insert("show", JsonValue::from(self.show)).unwrap();
103 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
104 field.insert("example", self.example.clone()).unwrap();
105
106 field
107 }
108
109 fn swagger(&mut self) -> JsonValue {
110 object! {
111 "type": self.mode.clone(),
112 "example": self.example.clone(),
113 }
114 }
115
116 fn example(&mut self, data: JsonValue) -> &mut Self {
117 self.example = data.clone();
118 self
119 }
120}
121
122pub struct Editor {
131 pub require: bool,
132 pub field: String,
133 pub mode: String,
134 pub title: String,
135 pub def: String,
136 pub length: i32,
137 pub show: bool,
138 pub describe: String,
139 pub example: JsonValue,
140}
141
142impl Editor {
143 pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
144 Self {
145 require,
146 field: field.to_string(),
147 mode: "editor".to_string(),
148 title: title.to_string(),
149 def: default.to_string(),
150 length: 0,
151 show: true,
152 describe: "".to_string(),
153 example: JsonValue::Null,
154 }
155 }
156 pub fn length(mut self, length: i32) -> Self {
160 self.length = length;
161 self
162 }
163}
164
165impl Field for Editor {
166 fn sql(&mut self, model: &str) -> String {
167 match model {
168 "sqlite" => {
169 format!("`{}` TEXT default '{}' ", self.field, self.def)
170 }
171 "pgsql" => {
172 let sql = format!(r#""{}" TEXT"#, self.field);
173 format!(
174 "{} comment '{}|{}|{}|{}'",
175 sql.clone(),
176 self.mode,
177 self.require,
178 self.title,
179 self.length
180 )
181 }
182 _ => {
183 let sql = match self.length {
184 1 => format!("`{}` MEDIUMTEXT ", self.field),
185 2 => format!("`{}` LONGTEXT ", self.field),
186 _ => format!("`{}` text ", self.field),
187 };
188 format!(
189 "{} comment '{}|{}|{}|{}'",
190 sql.clone(),
191 self.mode,
192 self.require,
193 self.title,
194 self.length
195 )
196 }
197 }
198 }
199 fn field(&mut self) -> JsonValue {
200 let mut field = object! {};
201 field.insert("require", JsonValue::from(self.require)).unwrap();
202 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
203 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
204 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
205 field.insert("length", JsonValue::from(self.length)).unwrap();
206 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
207
208 field.insert("show", JsonValue::from(self.show)).unwrap();
209 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
210 field.insert("example", self.example.clone()).unwrap();
211 field
212 }
213 fn hide(&mut self) -> &mut Self {
214 self.show = false;
215 self
216 }
217
218 fn describe(&mut self, text: &str) -> &mut Self {
219 self.describe = text.to_string();
220 self
221 }
222 fn swagger(&mut self) -> JsonValue {
223 object! {
224 "type": self.mode.clone(),
225 "example": self.example.clone(),
226 }
227 }
228 fn example(&mut self, data: JsonValue) -> &mut Self {
229 self.example = data.clone();
230 self
231 }
232}
233
234pub struct Json {
241 pub require: bool,
242 pub field: String,
243 pub mode: String,
244 pub title: String,
245 pub def: JsonValue,
246 pub length: i32,
247
248 pub show: bool,
249 pub describe: String,
250 pub example: JsonValue,
251}
252
253impl Json {
254 pub fn new(require: bool, field: &str, title: &str, def: JsonValue) -> Self {
255 Self {
256 require,
257 field: field.to_string(),
258 mode: "json".to_string(),
259 title: title.to_string(),
260 def,
261 length: 0,
262 show: true,
263 describe: "".to_string(),
264 example: JsonValue::Null,
265 }
266 }
267 pub fn length(mut self, length: i32) -> Self {
271 self.length = length;
272 self
273 }
274}
275
276impl Field for Json {
277 fn sql(&mut self, model: &str) -> String {
278 match model {
279 "sqlite" => {
280 format!("`{}` TEXT default '{}' ", self.field, self.def)
281 }
282 "pgsql" => {
283 let sql = format!(r#""{}" TEXT"#, self.field);
284 format!(
285 "{} comment '{}|{}|{}'",
286 sql.clone(),
287 self.mode,
288 self.title,
289 self.length
290 )
291 }
292 _ => {
293 let sql = match self.length {
294 1 => format!("`{}` MEDIUMTEXT ", self.field),
295 2 => format!("`{}` LONGTEXT ", self.field),
296 _ => format!("`{}` text ", self.field),
297 };
298 format!(
299 "{} comment '{}|{}|{}'",
300 sql.clone(),
301 self.mode,
302 self.title,
303 self.length
304 )
305 }
306 }
307 }
308 fn field(&mut self) -> JsonValue {
309 let mut field = object! {};
310 field.insert("require", JsonValue::from(self.require)).unwrap();
311 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
312 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
313 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
314
315 field.insert("length", JsonValue::from(self.length)).unwrap();
316
317 field.insert("def", self.def.clone()).unwrap();
318
319 field.insert("show", JsonValue::from(self.show)).unwrap();
320 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
321 field.insert("example", self.example.clone()).unwrap();
322 field
323 }
324 fn hide(&mut self) -> &mut Self {
325 self.show = false;
326 self
327 }
328
329 fn describe(&mut self, text: &str) -> &mut Self {
330 self.describe = text.to_string();
331 self
332 }
333 fn swagger(&mut self) -> JsonValue {
334 object! {
335 "type": self.mode.clone(),
336 "example": self.example.clone(),
337 }
338 }
339 fn example(&mut self, data: JsonValue) -> &mut Self {
340 self.example = data.clone();
341 self
342 }
343}
344
345pub struct Array {
352 pub require: bool,
353 pub field: String,
354 pub mode: String,
355 pub title: String,
356 pub def: JsonValue,
357 pub length: i32,
358
359 pub show: bool,
360 pub describe: String,
361 pub items: JsonValue,
362 pub example: JsonValue,
363 pub max_items: i32,
364}
365
366impl Array {
367 pub fn new(require: bool, field: &str, title: &str, def: JsonValue) -> Self {
368 Self {
369 require,
370 field: field.to_string(),
371 mode: "array".to_string(),
372 title: title.to_string(),
373 def,
374 length: 0,
375 show: true,
376 describe: "".to_string(),
377 items: JsonValue::Null,
378 example: JsonValue::Null,
379 max_items: 0,
380 }
381 }
382 pub fn length(mut self, length: i32) -> Self {
386 self.length = length;
387 self
388 }
389 pub fn items(mut self, item: JsonValue) -> Self {
390 self.items = item;
391 self
392 }
393 pub fn max_items(mut self, mun: i32) -> Self {
394 self.max_items = mun;
395 self
396 }
397}
398
399impl Field for Array {
400 fn sql(&mut self, model: &str) -> String {
401 match model {
402 "sqlite" => {
403 format!("`{}` TEXT default '{}' ", self.field, self.def)
404 }
405 "pgsql" => {
406 let sql = format!("{} TEXT ", self.field);
407 format!(
408 "{} comment '{}|{}|{}'",
409 sql.clone(),
410 self.mode,
411 self.title,
412 self.length
413 )
414 }
415 _ => {
416 let sql = match self.length {
417 1 => format!("`{}` MEDIUMTEXT ", self.field),
418 2 => format!("`{}` LONGTEXT ", self.field),
419 _ => format!("`{}` text ", self.field),
420 };
421 format!(
422 "{} comment '{}|{}|{}'",
423 sql.clone(),
424 self.mode,
425 self.title,
426 self.length
427 )
428 }
429 }
430 }
431 fn hide(&mut self) -> &mut Self {
432 self.show = false;
433 self
434 }
435 fn describe(&mut self, text: &str) -> &mut Self {
436 self.describe = text.to_string();
437 self
438 }
439 fn field(&mut self) -> JsonValue {
440 let mut field = object! {};
441 field.insert("require", JsonValue::from(self.require)).unwrap();
442 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
443 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
444 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
445 field.insert("length", JsonValue::from(self.length)).unwrap();
446
447 field.insert("def", self.def.clone()).unwrap();
448
449 field.insert("show", JsonValue::from(self.show)).unwrap();
450 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
451 field.insert("items", self.items.clone()).unwrap();
452 field.insert("example", self.example.clone()).unwrap();
453 field.insert("max_items", JsonValue::from(self.max_items)).unwrap();
454 field
455 }
456
457 fn swagger(&mut self) -> JsonValue {
458 object! {
459 "type": self.mode.clone(),
460 "example": self.example.clone(),
461 }
462 }
463 fn example(&mut self, data: JsonValue) -> &mut Self {
464 self.example = data;
465 self
466 }
467}
468
469#[derive(Debug, Clone)]
471pub struct Object {
472 pub require: bool,
474 pub field: String,
476 pub mode: String,
477 pub title: String,
479 pub def: JsonValue,
481 pub length: i32,
482
483 pub show: bool,
484 pub describe: String,
486 pub example: JsonValue,
487 pub items: JsonValue,
488}
489
490impl Object {
491 pub fn new(require: bool, field: &str, title: &str, def: JsonValue) -> Self {
492 Self {
493 require,
494 field: field.to_string(),
495 mode: "object".to_string(),
496 title: title.to_string(),
497 def,
498 length: 0,
499 show: true,
500 describe: "".to_string(),
501 example: JsonValue::Null,
502 items: JsonValue::Null,
503 }
504 }
505 pub fn length(mut self, length: i32) -> Self {
509 self.length = length;
510 self
511 }
512 pub fn items(mut self, item: JsonValue) -> Self {
513 self.items = item;
514 self
515 }
516}
517
518impl Field for Object {
519 fn sql(&mut self, model: &str) -> String {
520 match model {
521 "sqlite" => {
522 format!("`{}` TEXT default '{}' ", self.field, self.def)
523 }
524 "pgsql" => {
525 let sql = format!("{} TEXT ", self.field);
526 format!("{} comment '{}|{}|{}'", sql.clone(), self.mode, self.title, self.require)
527 }
528 _ => {
529 let sql = match self.length {
530 1 => format!("`{}` MEDIUMTEXT ", self.field),
531 2 => format!("`{}` LONGTEXT ", self.field),
532 _ => format!("`{}` text ", self.field),
533 };
534 format!("{} comment '{}|{}|{}'", sql.clone(), self.mode, self.title, self.length)
535 }
536 }
537 }
538 fn hide(&mut self) -> &mut Self {
539 self.show = false;
540 self
541 }
542
543 fn describe(&mut self, text: &str) -> &mut Self {
544 self.describe = text.to_string();
545 self
546 }
547 fn field(&mut self) -> JsonValue {
548 let mut field = object! {};
549 field.insert("require", JsonValue::from(self.require)).unwrap();
550 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
551 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
552 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
553 field.insert("length", JsonValue::from(self.length)).unwrap();
554
555 field.insert("def", self.def.clone()).unwrap();
556
557 field.insert("show", JsonValue::from(self.show)).unwrap();
558 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
559 field.insert("example", self.example.clone()).unwrap();
560 field.insert("items", self.items.clone()).unwrap();
561
562 field
563 }
564
565 fn swagger(&mut self) -> JsonValue {
566 object! {
567 "type": self.mode.clone(),
568 "example": self.example.clone(),
569 }
570 }
571 fn example(&mut self, data: JsonValue) -> &mut Self {
572 self.example = data.clone();
573 self
574 }
575}
576
577pub struct Url {
586 pub require: bool,
587 pub field: String,
588 pub mode: String,
589 pub title: String,
590 pub def: String,
591 pub length: i32,
592 pub show: bool,
593 pub describe: String,
594 pub example: JsonValue,
595}
596
597impl Url {
598 pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
599 Self {
600 require,
601 field: field.to_string(),
602 mode: "url".to_string(),
603 title: title.to_string(),
604 def: default.to_string(),
605 length: 0,
606 show: true,
607 describe: "".to_string(),
608 example: JsonValue::Null,
609 }
610 }
611}
612
613impl Field for Url {
614 fn sql(&mut self, model: &str) -> String {
615 match model {
616 "sqlite" => {
617 format!("`{}` TEXT default '{}' ", self.field, self.def)
618 }
619 _ => {
620 let sql = format!("`{}` text ", self.field);
621 format!("{} comment '{}|{}'", sql.clone(), self.mode, self.title)
622 }
623 }
624 }
625 fn hide(&mut self) -> &mut Self {
626 self.show = false;
627 self
628 }
629 fn describe(&mut self, text: &str) -> &mut Self {
630 self.describe = text.to_string();
631 self
632 }
633
634 fn field(&mut self) -> JsonValue {
635 let mut field = object! {};
636 field.insert("require", JsonValue::from(self.require)).unwrap();
637 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
638 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
639 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
640 field.insert("length", JsonValue::from(self.length)).unwrap();
641 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
642
643 field.insert("show", JsonValue::from(self.show)).unwrap();
644 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
645 field.insert("example", self.example.clone()).unwrap();
646 field
647 }
648
649 fn swagger(&mut self) -> JsonValue {
650 object! {
651 "type": self.mode.clone(),
652 "example": self.example.clone(),
653 }
654 }
655
656 fn example(&mut self, data: JsonValue) -> &mut Self {
657 self.example = data.clone();
658 self
659 }
660}