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 let not_null = if self.require { " not null" } else { "" };
51 match model {
52 "sqlite" => {
53 format!("{} TEXT{} default '{}'", self.field, not_null, self.def)
54 }
55 "pgsql" => {
56 format!(
57 r#""{}" TEXT{} default '{}'"#,
58 self.field, not_null, self.def
59 )
60 }
61 _ => {
62 let sql = match self.length {
63 1 => format!("`{}` MEDIUMTEXT{}", self.field, not_null),
64 2 => format!("`{}` LONGTEXT{}", self.field, not_null),
65 _ => format!("`{}` text{}", self.field, not_null),
66 };
67 format!(
68 "{} comment '{}|{}|{}|{}|{}'",
69 sql.clone(),
70 self.mode,
71 self.require,
72 self.title,
73 self.length,
74 self.def
75 )
76 }
77 }
78 }
79 fn hide(&mut self) -> &mut Self {
80 self.show = false;
81 self
82 }
83 fn describe(&mut self, text: &str) -> &mut Self {
84 self.describe = text.to_string();
85 self
86 }
87
88 fn field(&mut self) -> JsonValue {
89 let mut field = object! {};
90 field
91 .insert("require", JsonValue::from(self.require))
92 .unwrap();
93 field
94 .insert("field", JsonValue::from(self.field.clone()))
95 .unwrap();
96 field
97 .insert("mode", JsonValue::from(self.mode.clone()))
98 .unwrap();
99 field
100 .insert("title", JsonValue::from(self.title.clone()))
101 .unwrap();
102 field
103 .insert("length", JsonValue::from(self.length))
104 .unwrap();
105 field
106 .insert("def", JsonValue::from(self.def.clone()))
107 .unwrap();
108
109 field.insert("show", JsonValue::from(self.show)).unwrap();
110 field
111 .insert("describe", JsonValue::from(self.describe.clone()))
112 .unwrap();
113 field.insert("example", self.example.clone()).unwrap();
114
115 field
116 }
117
118 fn swagger(&mut self) -> JsonValue {
119 object! {
120 "type": self.mode.clone(),
121 "example": self.example.clone(),
122 }
123 }
124
125 fn example(&mut self, data: JsonValue) -> &mut Self {
126 self.example = data.clone();
127 self
128 }
129}
130
131pub struct Editor {
140 pub require: bool,
141 pub field: String,
142 pub mode: String,
143 pub title: String,
144 pub def: String,
145 pub length: i32,
146 pub show: bool,
147 pub describe: String,
148 pub example: JsonValue,
149}
150
151impl Editor {
152 pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
153 Self {
154 require,
155 field: field.to_string(),
156 mode: "editor".to_string(),
157 title: title.to_string(),
158 def: default.to_string(),
159 length: 0,
160 show: true,
161 describe: "".to_string(),
162 example: JsonValue::Null,
163 }
164 }
165 pub fn length(mut self, length: i32) -> Self {
169 self.length = length;
170 self
171 }
172}
173
174impl Field for Editor {
175 fn sql(&mut self, model: &str) -> String {
176 let not_null = if self.require { " not null" } else { "" };
177 match model {
178 "sqlite" => {
179 format!("{} TEXT{} default '{}'", self.field, not_null, self.def)
180 }
181 "pgsql" => {
182 format!(
183 r#""{}" TEXT{} default '{}'"#,
184 self.field, not_null, self.def
185 )
186 }
187 _ => {
188 let sql = match self.length {
189 1 => format!("`{}` MEDIUMTEXT{}", self.field, not_null),
190 2 => format!("`{}` LONGTEXT{}", self.field, not_null),
191 _ => format!("`{}` text{}", self.field, not_null),
192 };
193 format!(
194 "{} comment '{}|{}|{}|{}'",
195 sql.clone(),
196 self.mode,
197 self.require,
198 self.title,
199 self.length
200 )
201 }
202 }
203 }
204 fn field(&mut self) -> JsonValue {
205 let mut field = object! {};
206 field
207 .insert("require", JsonValue::from(self.require))
208 .unwrap();
209 field
210 .insert("field", JsonValue::from(self.field.clone()))
211 .unwrap();
212 field
213 .insert("mode", JsonValue::from(self.mode.clone()))
214 .unwrap();
215 field
216 .insert("title", JsonValue::from(self.title.clone()))
217 .unwrap();
218 field
219 .insert("length", JsonValue::from(self.length))
220 .unwrap();
221 field
222 .insert("def", JsonValue::from(self.def.clone()))
223 .unwrap();
224
225 field.insert("show", JsonValue::from(self.show)).unwrap();
226 field
227 .insert("describe", JsonValue::from(self.describe.clone()))
228 .unwrap();
229 field.insert("example", self.example.clone()).unwrap();
230 field
231 }
232 fn hide(&mut self) -> &mut Self {
233 self.show = false;
234 self
235 }
236
237 fn describe(&mut self, text: &str) -> &mut Self {
238 self.describe = text.to_string();
239 self
240 }
241 fn swagger(&mut self) -> JsonValue {
242 object! {
243 "type": self.mode.clone(),
244 "example": self.example.clone(),
245 }
246 }
247 fn example(&mut self, data: JsonValue) -> &mut Self {
248 self.example = data.clone();
249 self
250 }
251}
252
253pub struct Json {
260 pub require: bool,
261 pub field: String,
262 pub mode: String,
263 pub title: String,
264 pub def: JsonValue,
265 pub length: i32,
266
267 pub show: bool,
268 pub describe: String,
269 pub example: JsonValue,
270}
271
272impl Json {
273 pub fn new(require: bool, field: &str, title: &str, def: JsonValue) -> Self {
274 Self {
275 require,
276 field: field.to_string(),
277 mode: "json".to_string(),
278 title: title.to_string(),
279 def,
280 length: 0,
281 show: true,
282 describe: "".to_string(),
283 example: JsonValue::Null,
284 }
285 }
286 pub fn length(mut self, length: i32) -> Self {
290 self.length = length;
291 self
292 }
293}
294
295impl Field for Json {
296 fn sql(&mut self, model: &str) -> String {
297 let not_null = if self.require { " not null" } else { "" };
298 let def_str = self.def.to_string();
299 match model {
300 "sqlite" => {
301 format!("{} TEXT{} default '{}'", self.field, not_null, def_str)
302 }
303 "pgsql" => {
304 format!(r#""{}" TEXT{} default '{}'"#, self.field, not_null, def_str)
305 }
306 _ => {
307 let sql = match self.length {
308 1 => format!("`{}` MEDIUMTEXT{}", self.field, not_null),
309 2 => format!("`{}` LONGTEXT{}", self.field, not_null),
310 _ => format!("`{}` text{}", self.field, not_null),
311 };
312 format!(
313 "{} comment '{}|{}|{}'",
314 sql.clone(),
315 self.mode,
316 self.title,
317 self.length
318 )
319 }
320 }
321 }
322 fn field(&mut self) -> JsonValue {
323 let mut field = object! {};
324 field
325 .insert("require", JsonValue::from(self.require))
326 .unwrap();
327 field
328 .insert("field", JsonValue::from(self.field.clone()))
329 .unwrap();
330 field
331 .insert("mode", JsonValue::from(self.mode.clone()))
332 .unwrap();
333 field
334 .insert("title", JsonValue::from(self.title.clone()))
335 .unwrap();
336
337 field
338 .insert("length", JsonValue::from(self.length))
339 .unwrap();
340
341 field.insert("def", self.def.clone()).unwrap();
342
343 field.insert("show", JsonValue::from(self.show)).unwrap();
344 field
345 .insert("describe", JsonValue::from(self.describe.clone()))
346 .unwrap();
347 field.insert("example", self.example.clone()).unwrap();
348 field
349 }
350 fn hide(&mut self) -> &mut Self {
351 self.show = false;
352 self
353 }
354
355 fn describe(&mut self, text: &str) -> &mut Self {
356 self.describe = text.to_string();
357 self
358 }
359 fn swagger(&mut self) -> JsonValue {
360 object! {
361 "type": self.mode.clone(),
362 "example": self.example.clone(),
363 }
364 }
365 fn example(&mut self, data: JsonValue) -> &mut Self {
366 self.example = data.clone();
367 self
368 }
369}
370
371pub struct Array {
378 pub require: bool,
379 pub field: String,
380 pub mode: String,
381 pub title: String,
382 pub def: JsonValue,
383 pub length: i32,
384
385 pub show: bool,
386 pub describe: String,
387 pub items: JsonValue,
388 pub example: JsonValue,
389 pub max_items: i32,
390}
391
392impl Array {
393 pub fn new(require: bool, field: &str, title: &str, def: JsonValue) -> Self {
394 Self {
395 require,
396 field: field.to_string(),
397 mode: "array".to_string(),
398 title: title.to_string(),
399 def,
400 length: 0,
401 show: true,
402 describe: "".to_string(),
403 items: JsonValue::Null,
404 example: JsonValue::Null,
405 max_items: 0,
406 }
407 }
408 pub fn length(mut self, length: i32) -> Self {
412 self.length = length;
413 self
414 }
415 pub fn items(mut self, item: JsonValue) -> Self {
416 self.items = item;
417 self
418 }
419 pub fn max_items(mut self, mun: i32) -> Self {
420 self.max_items = mun;
421 self
422 }
423}
424
425impl Field for Array {
426 fn sql(&mut self, model: &str) -> String {
427 let not_null = if self.require { " not null" } else { "" };
428 let def_str = self.def.to_string();
429 match model {
430 "sqlite" => {
431 format!("{} TEXT{} default '{}'", self.field, not_null, def_str)
432 }
433 "pgsql" => {
434 format!(r#""{}" TEXT{} default '{}'"#, self.field, not_null, def_str)
435 }
436 _ => {
437 let sql = match self.length {
438 1 => format!("`{}` MEDIUMTEXT{}", self.field, not_null),
439 2 => format!("`{}` LONGTEXT{}", self.field, not_null),
440 _ => format!("`{}` text{}", self.field, not_null),
441 };
442 format!(
443 "{} comment '{}|{}|{}'",
444 sql.clone(),
445 self.mode,
446 self.title,
447 self.length
448 )
449 }
450 }
451 }
452 fn hide(&mut self) -> &mut Self {
453 self.show = false;
454 self
455 }
456 fn describe(&mut self, text: &str) -> &mut Self {
457 self.describe = text.to_string();
458 self
459 }
460 fn field(&mut self) -> JsonValue {
461 let mut field = object! {};
462 field
463 .insert("require", JsonValue::from(self.require))
464 .unwrap();
465 field
466 .insert("field", JsonValue::from(self.field.clone()))
467 .unwrap();
468 field
469 .insert("mode", JsonValue::from(self.mode.clone()))
470 .unwrap();
471 field
472 .insert("title", JsonValue::from(self.title.clone()))
473 .unwrap();
474 field
475 .insert("length", JsonValue::from(self.length))
476 .unwrap();
477
478 field.insert("def", self.def.clone()).unwrap();
479
480 field.insert("show", JsonValue::from(self.show)).unwrap();
481 field
482 .insert("describe", JsonValue::from(self.describe.clone()))
483 .unwrap();
484 field.insert("items", self.items.clone()).unwrap();
485 field.insert("example", self.example.clone()).unwrap();
486 field
487 .insert("max_items", JsonValue::from(self.max_items))
488 .unwrap();
489 field
490 }
491
492 fn swagger(&mut self) -> JsonValue {
493 object! {
494 "type": self.mode.clone(),
495 "example": self.example.clone(),
496 }
497 }
498 fn example(&mut self, data: JsonValue) -> &mut Self {
499 self.example = data;
500 self
501 }
502}
503
504#[derive(Debug, Clone)]
506pub struct Object {
507 pub require: bool,
509 pub field: String,
511 pub mode: String,
512 pub title: String,
514 pub def: JsonValue,
516 pub length: i32,
517
518 pub show: bool,
519 pub describe: String,
521 pub example: JsonValue,
522 pub items: JsonValue,
523}
524
525impl Object {
526 pub fn new(require: bool, field: &str, title: &str, def: JsonValue) -> Self {
527 Self {
528 require,
529 field: field.to_string(),
530 mode: "object".to_string(),
531 title: title.to_string(),
532 def,
533 length: 0,
534 show: true,
535 describe: "".to_string(),
536 example: JsonValue::Null,
537 items: JsonValue::Null,
538 }
539 }
540 pub fn length(mut self, length: i32) -> Self {
544 self.length = length;
545 self
546 }
547 pub fn items(mut self, item: JsonValue) -> Self {
548 self.items = item;
549 self
550 }
551}
552
553impl Field for Object {
554 fn sql(&mut self, model: &str) -> String {
555 let not_null = if self.require { " not null" } else { "" };
556 let def_str = self.def.to_string();
557 match model {
558 "sqlite" => {
559 format!("{} TEXT{} default '{}'", self.field, not_null, def_str)
560 }
561 "pgsql" => {
562 format!(r#""{}" TEXT{} default '{}'"#, self.field, not_null, def_str)
563 }
564 _ => {
565 let sql = match self.length {
566 1 => format!("`{}` MEDIUMTEXT{}", self.field, not_null),
567 2 => format!("`{}` LONGTEXT{}", self.field, not_null),
568 _ => format!("`{}` text{}", self.field, not_null),
569 };
570 format!(
571 "{} comment '{}|{}|{}'",
572 sql.clone(),
573 self.mode,
574 self.title,
575 self.length
576 )
577 }
578 }
579 }
580 fn hide(&mut self) -> &mut Self {
581 self.show = false;
582 self
583 }
584
585 fn describe(&mut self, text: &str) -> &mut Self {
586 self.describe = text.to_string();
587 self
588 }
589 fn field(&mut self) -> JsonValue {
590 let mut field = object! {};
591 field
592 .insert("require", JsonValue::from(self.require))
593 .unwrap();
594 field
595 .insert("field", JsonValue::from(self.field.clone()))
596 .unwrap();
597 field
598 .insert("mode", JsonValue::from(self.mode.clone()))
599 .unwrap();
600 field
601 .insert("title", JsonValue::from(self.title.clone()))
602 .unwrap();
603 field
604 .insert("length", JsonValue::from(self.length))
605 .unwrap();
606
607 field.insert("def", self.def.clone()).unwrap();
608
609 field.insert("show", JsonValue::from(self.show)).unwrap();
610 field
611 .insert("describe", JsonValue::from(self.describe.clone()))
612 .unwrap();
613 field.insert("example", self.example.clone()).unwrap();
614 field.insert("items", self.items.clone()).unwrap();
615
616 field
617 }
618
619 fn swagger(&mut self) -> JsonValue {
620 object! {
621 "type": self.mode.clone(),
622 "example": self.example.clone(),
623 }
624 }
625 fn example(&mut self, data: JsonValue) -> &mut Self {
626 self.example = data.clone();
627 self
628 }
629}
630
631pub struct Url {
640 pub require: bool,
641 pub field: String,
642 pub mode: String,
643 pub title: String,
644 pub def: String,
645 pub length: i32,
646 pub show: bool,
647 pub describe: String,
648 pub example: JsonValue,
649}
650
651impl Url {
652 pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
653 Self {
654 require,
655 field: field.to_string(),
656 mode: "url".to_string(),
657 title: title.to_string(),
658 def: default.to_string(),
659 length: 0,
660 show: true,
661 describe: "".to_string(),
662 example: JsonValue::Null,
663 }
664 }
665}
666
667impl Field for Url {
668 fn sql(&mut self, model: &str) -> String {
669 let not_null = if self.require { " not null" } else { "" };
670 match model {
671 "sqlite" => {
672 format!("{} TEXT{} default '{}'", self.field, not_null, self.def)
673 }
674 "pgsql" => {
675 format!(
676 r#""{}" TEXT{} default '{}'"#,
677 self.field, not_null, self.def
678 )
679 }
680 _ => {
681 let sql = format!("`{}` text{}", self.field, not_null);
682 format!("{} comment '{}|{}'", sql.clone(), self.mode, self.title)
683 }
684 }
685 }
686 fn hide(&mut self) -> &mut Self {
687 self.show = false;
688 self
689 }
690 fn describe(&mut self, text: &str) -> &mut Self {
691 self.describe = text.to_string();
692 self
693 }
694
695 fn field(&mut self) -> JsonValue {
696 let mut field = object! {};
697 field
698 .insert("require", JsonValue::from(self.require))
699 .unwrap();
700 field
701 .insert("field", JsonValue::from(self.field.clone()))
702 .unwrap();
703 field
704 .insert("mode", JsonValue::from(self.mode.clone()))
705 .unwrap();
706 field
707 .insert("title", JsonValue::from(self.title.clone()))
708 .unwrap();
709 field
710 .insert("length", JsonValue::from(self.length))
711 .unwrap();
712 field
713 .insert("def", JsonValue::from(self.def.clone()))
714 .unwrap();
715
716 field.insert("show", JsonValue::from(self.show)).unwrap();
717 field
718 .insert("describe", JsonValue::from(self.describe.clone()))
719 .unwrap();
720 field.insert("example", self.example.clone()).unwrap();
721 field
722 }
723
724 fn swagger(&mut self) -> JsonValue {
725 object! {
726 "type": self.mode.clone(),
727 "example": self.example.clone(),
728 }
729 }
730
731 fn example(&mut self, data: JsonValue) -> &mut Self {
732 self.example = data.clone();
733 self
734 }
735}