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