1use std::sync::atomic::{AtomicU16, Ordering};
2use chrono::Local;
3use crate::datetime::Datetime;
4use crate::Field;
5use json::{object, JsonValue};
6use rand::Rng;
7
8static SEQ: AtomicU16 = AtomicU16::new(0);
10#[derive(Debug, Clone)]
20pub struct Str {
21 pub require: bool,
22 pub field: String,
23 pub mode: String,
24 pub title: String,
25 pub def: String,
26 pub length: usize,
27 pub show: bool,
28 pub describe: String,
29 pub example: JsonValue,
30}
31
32impl Str {
33 pub fn new(require: bool, field: &str, title: &str, length: usize, default: &str) -> Str {
34 Self {
35 require,
36 field: field.to_string(),
37 mode: "string".to_string(),
38 title: title.to_string(),
39 def: default.to_string(),
40 length,
41 show: true,
42 describe: "".to_string(),
43 example: JsonValue::Null,
44 }
45 }
46}
47
48impl Field for Str {
49 fn require(&mut self, require: bool) -> &mut Self {
50 self.require = require;
51 self
52 }
53
54 fn sql(&mut self, model: &str) -> String {
55 match model {
56 "sqlite" => format!(
57 "{} varchar({}) default '{}'",
58 self.field, self.length, self.def
59 ),
60 "pgsql" => {
61 let sql = format!(
62 r#""{}" varchar({}) default '{}'"#,
63 self.field, self.length, self.def
64 );
65 format!(
66 "{} comment '{}|{}|{}|{}|{}'",
67 sql.clone(),
68 self.mode,
69 self.require,
70 self.title,
71 self.length,
72 self.def
73 )
74 }
75 _ => {
76 let sql = format!(
77 "`{}` varchar({}) default '{}'",
78 self.field, self.length, self.def
79 );
80 format!(
81 "{} comment '{}|{}|{}|{}|{}'",
82 sql.clone(),
83 self.mode,
84 self.require,
85 self.title,
86 self.length,
87 self.def
88 )
89 }
90 }
91 }
92 fn hide(&mut self) -> &mut Self {
93 self.show = false;
94 self
95 }
96 fn describe(&mut self, text: &str) -> &mut Self {
97 self.describe = text.to_string();
98 self
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("length", JsonValue::from(self.length)).unwrap();
107 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
108
109 field.insert("show", JsonValue::from(self.show)).unwrap();
110 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
111 field.insert("example", self.example.clone()).unwrap();
112
113 field
114 }
115
116 fn swagger(&mut self) -> JsonValue {
117 object! {
118 "type": self.mode.clone(),
119 "example": self.example.clone(),
120 }
121 }
122 fn example(&mut self, data: JsonValue) -> &mut Self {
123 self.example = data;
124 self
125 }
126}
127
128pub struct Pass {
137 pub require: bool,
138 pub field: String,
139 pub mode: String,
140 pub title: String,
141 pub def: String,
142 pub length: usize,
143 pub show: bool,
144 pub describe: String,
145 pub example: JsonValue,
146}
147
148impl Pass {
149 pub fn new(require: bool, field: &str, title: &str, length: usize, default: &str) -> Self {
150 Self {
151 require,
152 field: field.to_string(),
153 mode: "pass".to_string(),
154 title: title.to_string(),
155 def: default.to_string(),
156 length,
157 show: true,
158 describe: "".to_string(),
159 example: json::Null,
160 }
161 }
162}
163
164impl Field for Pass {
165 fn require(&mut self, require: bool) -> &mut Self {
166 self.require = require;
167 self
168 }
169
170 fn sql(&mut self, model: &str) -> String {
171 match model {
172 "sqlite" => format!(
173 "`{}` varchar({}) default '{}'",
174 self.field, self.length, self.def
175 ),
176 "pgsql" => {
177 let sql = format!(
178 r#""{}" varchar({}) default '{}'"#,
179 self.field, self.length, self.def
180 );
181 format!(
182 "{} comment '{}|{}|{}|{}|{}'",
183 sql.clone(),
184 self.mode,
185 self.require,
186 self.title,
187 self.length,
188 self.def
189 )
190 }
191 _ => {
192 let sql = format!(
193 "`{}` varchar({}) default '{}'",
194 self.field, self.length, self.def
195 );
196 format!(
197 "{} comment '{}|{}|{}|{}|{}'",
198 sql.clone(),
199 self.mode,
200 self.require,
201 self.title,
202 self.length,
203 self.def
204 )
205 }
206 }
207 }
208 fn hide(&mut self) -> &mut Self {
209 self.show = false;
210 self
211 }
212 fn describe(&mut self, text: &str) -> &mut Self {
213 self.describe = text.to_string();
214 self
215 }
216 fn field(&mut self) -> JsonValue {
217 let mut field = object! {};
218 field.insert("require", JsonValue::from(self.require)).unwrap();
219 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
220 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
221 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
222 field.insert("length", JsonValue::from(self.length)).unwrap();
223 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
224
225 field.insert("show", JsonValue::from(self.show)).unwrap();
226 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
227 field.insert("example", self.example.clone()).unwrap();
228
229 field
230 }
231
232 fn swagger(&mut self) -> JsonValue {
233 object! {
234 "type": self.mode.clone(),
235 "example": self.example.clone(),
236 }
237 }
238
239 fn example(&mut self, data: JsonValue) -> &mut Self {
240 self.example = data;
241 self
242 }
243}
244
245#[derive(Clone, Debug)]
253pub struct Key {
254 pub require: bool,
255 pub field: String,
256 pub mode: String,
257 pub title: String,
258 pub length: usize,
259
260 pub def: String,
261
262 pub auto: bool,
263 pub show: bool,
264 pub describe: String,
265 pub example: JsonValue,
266}
267
268impl Key {
269 pub fn new(require: bool, field: &str, title: &str, length: usize) -> Self {
270 Self {
271 require,
272 field: field.to_string(),
273 mode: "key".to_string(),
274 title: title.to_string(),
275 length,
276 auto: false,
277 show: true,
278 def: "".to_string(),
279 describe: "".to_string(),
280 example: JsonValue::Null,
281 }
282 }
283 pub fn auto(mut self, auto: bool) -> Self {
284 if auto {
285 self.length = 0;
286 }
287 self.auto = auto;
288 self
289 }
290}
291
292impl Field for Key {
293 fn require(&mut self, require: bool) -> &mut Self {
294 self.require = require;
295 self
296 }
297 fn sql(&mut self, model: &str) -> String {
298 match model {
299 "sqlite" => match self.auto {
300 true => {
301 let sql = format!("`{}` INTEGER PRIMARY KEY", self.field);
302 format!("{} AUTOINCREMENT", sql.clone())
303 }
304 false => {
305 format!(
306 "`{}` varchar({}) default '' constraint {}_key primary key",
307 self.field, self.length, self.field
308 )
309 }
310 },
311 "pgsql" => match self.auto {
312 true => {
313 let sql = format!(r#""{}" int AUTO_INCREMENT"#, self.field);
314 format!(
315 "{} comment '{}|{}|{}'",
316 sql.clone(),
317 self.mode,
318 self.title,
319 self.length
320 )
321 }
322 false => {
323 let mut sql = format!(r#""{}" varchar({}) not null"#, self.field, self.length);
324 sql = sql.clone().to_string();
325 format!(
326 "{} comment '{}|{}|{}'",
327 sql.clone(),
328 self.mode,
329 self.title,
330 self.length
331 )
332 }
333 },
334 _ => match self.auto {
335 true => {
336 let sql = format!("`{}` int AUTO_INCREMENT", self.field);
337 format!(
338 "{} comment '{}|{}|{}'",
339 sql.clone(),
340 self.mode,
341 self.title,
342 self.length
343 )
344 }
345 false => {
346 let mut sql = format!("`{}` varchar({}) ", self.field, self.length);
347 sql = sql.clone().to_string();
348 format!(
349 "{} comment '{}|{}|{}'",
350 sql.clone(),
351 self.mode,
352 self.title,
353 self.length
354 )
355 }
356 },
357 }
358 }
359 fn hide(&mut self) -> &mut Self {
360 self.show = false;
361 self
362 }
363
364 fn describe(&mut self, text: &str) -> &mut Self {
365 self.describe = text.to_string();
366 self
367 }
368 fn field(&mut self) -> JsonValue {
369 let mut field = object! {};
370 field.insert("require", JsonValue::from(self.require)).unwrap();
371 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
372 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
373 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
374 field.insert("length", JsonValue::from(self.length)).unwrap();
375 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
376 field.insert("auto", JsonValue::from(self.auto)).unwrap();
377
378 field.insert("show", JsonValue::from(self.show)).unwrap();
379 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
380 field.insert("example", self.example.clone()).unwrap();
381
382 field
383 }
384 fn example(&mut self, data: JsonValue) -> &mut Self {
385 self.example = data;
386 self
387 }
388
389 fn swagger(&mut self) -> JsonValue {
390 object! {
391 "type": self.mode.clone(),
392 "example": self.example.clone(),
393 }
394 }
395}
396
397pub struct Tel {
406 pub require: bool,
407 pub field: String,
408 pub mode: String,
409 pub title: String,
410 pub def: String,
411 pub length: i32,
412
413 pub show: bool,
414 pub describe: String,
415 pub example: JsonValue,
416}
417
418impl Tel {
419 pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
420 Self {
421 require,
422 field: field.to_string(),
423 mode: "tel".to_string(),
424 title: title.to_string(),
425 def: default.to_string(),
426 length: 15,
427
428 show: true,
429 describe: "".to_string(),
430 example: JsonValue::Null,
431 }
432 }
433}
434
435impl Field for Tel {
436 fn require(&mut self, require: bool) -> &mut Self {
437 self.require = require;
438 self
439 }
440 fn sql(&mut self, model: &str) -> String {
441 let dot = if model == "pgsql" { "" } else { "`" };
442 let sql = format!(
443 "{dot}{}{dot} varchar({}) default '{}'",
444 self.field, self.length, self.def
445 );
446 match model {
447 "sqlite" => sql,
448 _ => format!(
449 "{} comment '{}|{}|{}|{}|{}'",
450 sql.clone(),
451 self.mode,
452 self.require,
453 self.title,
454 self.length,
455 self.def
456 ),
457 }
458 }
459 fn hide(&mut self) -> &mut Self {
460 self.show = false;
461 self
462 }
463 fn describe(&mut self, text: &str) -> &mut Self {
464 self.describe = text.to_string();
465 self
466 }
467
468 fn field(&mut self) -> JsonValue {
469 let mut field = object! {};
470 field.insert("require", JsonValue::from(self.require)).unwrap();
471 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
472 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
473 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
474 field.insert("length", JsonValue::from(self.length)).unwrap();
475 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
476
477 field.insert("show", JsonValue::from(self.show)).unwrap();
478 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
479 field.insert("example", self.example.clone()).unwrap();
480
481 field
482 }
483
484 fn swagger(&mut self) -> JsonValue {
485 object! {
486 "type": self.mode.clone(),
487 "example": self.example.clone(),
488 }
489 }
490 fn example(&mut self, data: JsonValue) -> &mut Self {
491 self.example = data;
492 self
493 }
494}
495
496pub struct Ident {
505 pub require: bool,
506 pub field: String,
507 pub mode: String,
508 pub title: String,
509 pub def: String,
510 pub length: i32,
511
512 pub show: bool,
513 pub describe: String,
514 pub example: JsonValue,
515}
516
517impl Ident {
518 pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
519 Self {
520 require,
521 field: field.to_string(),
522 mode: "ident".to_string(),
523 title: title.to_string(),
524 def: default.to_string(),
525 length: 18,
526
527 show: true,
528 describe: "".to_string(),
529 example: JsonValue::Null,
530 }
531 }
532}
533
534impl Field for Ident {
535 fn require(&mut self, require: bool) -> &mut Self {
536 self.require = require;
537 self
538 }
539 fn sql(&mut self, model: &str) -> String {
540 let dot = if model == "pgsql" { "" } else { "`" };
541 let sql = format!(
542 "{dot}{}{dot} varchar({}) default '{}'",
543 self.field, self.length, self.def
544 );
545 match model {
546 "sqlite" => sql,
547 _ => format!(
548 "{} comment '{}|{}|{}|{}|{}'",
549 sql.clone(),
550 self.mode,
551 self.require,
552 self.title,
553 self.length,
554 self.def
555 ),
556 }
557 }
558 fn hide(&mut self) -> &mut Self {
559 self.show = false;
560 self
561 }
562 fn describe(&mut self, text: &str) -> &mut Self {
563 self.describe = text.to_string();
564 self
565 }
566
567 fn field(&mut self) -> JsonValue {
568 let mut field = object! {};
569 field.insert("require", JsonValue::from(self.require)).unwrap();
570 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
571 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
572 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
573 field.insert("length", JsonValue::from(self.length)).unwrap();
574 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
575
576 field.insert("show", JsonValue::from(self.show)).unwrap();
577 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
578 field.insert("example", self.example.clone()).unwrap();
579
580 field
581 }
582
583 fn swagger(&mut self) -> JsonValue {
584 object! {
585 "type": self.mode.clone(),
586 "example": self.example.clone(),
587 }
588 }
589 fn example(&mut self, data: JsonValue) -> &mut Self {
590 self.example = data;
591 self
592 }
593}
594pub struct Email {
603 pub require: bool,
604 pub field: String,
605 pub mode: String,
606 pub title: String,
607 pub def: String,
608 pub length: i32,
609
610 pub show: bool,
611 pub describe: String,
612 pub example: JsonValue,
613}
614
615impl Email {
616 pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
617 Self {
618 require,
619 field: field.to_string(),
620 mode: "email".to_string(),
621 title: title.to_string(),
622 def: default.to_string(),
623 length: 100,
624
625 show: true,
626 describe: "".to_string(),
627 example: JsonValue::Null,
628 }
629 }
630}
631
632impl Field for Email {
633 fn require(&mut self, require: bool) -> &mut Self {
634 self.require = require;
635 self
636 }
637 fn sql(&mut self, model: &str) -> String {
638 let sql = format!("`{}` varchar({}) default '{}'", self.field, self.length, self.def);
639 match model {
640 "sqlite" => sql,
641 "pgsql" => {
642 let sql = format!("{} varchar({}) default '{}'", self.field, self.length, self.def);
643 format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def)
644 }
645 _ => format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def),
646 }
647 }
648 fn hide(&mut self) -> &mut Self {
649 self.show = false;
650 self
651 }
652 fn describe(&mut self, text: &str) -> &mut Self {
653 self.describe = text.to_string();
654 self
655 }
656 fn field(&mut self) -> JsonValue {
657 let mut field = object! {};
658 field.insert("require", JsonValue::from(self.require)).unwrap();
659 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
660 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
661 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
662 field.insert("length", JsonValue::from(self.length)).unwrap();
663 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
664
665 field.insert("show", JsonValue::from(self.show)).unwrap();
666 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
667 field.insert("example", self.example.clone()).unwrap();
668 field
669 }
670
671 fn verify(&mut self, data: JsonValue) -> JsonValue {
672 data
673 }
674
675 fn swagger(&mut self) -> JsonValue {
676 object! {
677 "type": self.mode.clone(),
678 "example": self.example.clone(),
679 }
680 }
681 fn example(&mut self, data: JsonValue) -> &mut Self {
682 self.example = data.clone();
683 self
684 }
685}
686
687pub struct Color {
694 pub require: bool,
695 pub field: String,
696 pub mode: String,
697 pub title: String,
698 pub def: String,
699 pub length: i32,
700
701 pub show: bool,
702 pub describe: String,
703 pub example: JsonValue,
704}
705
706impl Color {
707 pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
708 Self {
709 require,
710 field: field.to_string(),
711 mode: "color".to_string(),
712 title: title.to_string(),
713 def: default.to_string(),
714 length: 9,
715
716 show: true,
717 describe: "".to_string(),
718 example: JsonValue::Null,
719 }
720 }
721}
722
723impl Field for Color {
724 fn require(&mut self, require: bool) -> &mut Self {
725 self.require = require;
726 self
727 }
728 fn sql(&mut self, model: &str) -> String {
729 let dot = if model == "pgsql" { "" } else { "`" };
730 let sql = format!(
731 "{dot}{}{dot} varchar({}) default '{}'",
732 self.field, self.length, self.def
733 );
734 match model {
735 "sqlite" => sql,
736 _ => format!(
737 "{} comment '{}|{}|{}|{}|{}'",
738 sql.clone(),
739 self.mode,
740 self.require,
741 self.title,
742 self.length,
743 self.def
744 ),
745 }
746 }
747 fn hide(&mut self) -> &mut Self {
748 self.show = false;
749 self
750 }
751 fn describe(&mut self, text: &str) -> &mut Self {
752 self.describe = text.to_string();
753 self
754 }
755 fn field(&mut self) -> JsonValue {
756 let mut field = object! {};
757 field.insert("require", JsonValue::from(self.require)).unwrap();
758 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
759 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
760 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
761 field.insert("length", JsonValue::from(self.length)).unwrap();
762 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
763
764 field.insert("show", JsonValue::from(self.show)).unwrap();
765 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
766 field.insert("example", self.example.clone()).unwrap();
767
768 field
769 }
770
771 fn verify(&mut self, data: JsonValue) -> JsonValue {
772 data
773 }
774
775 fn swagger(&mut self) -> JsonValue {
776 object! {
777 "type": self.mode.clone(),
778 "example": self.example.clone(),
779 }
780 }
781 fn example(&mut self, data: JsonValue) -> &mut Self {
782 self.example = data.clone();
783 self
784 }
785}
786
787pub struct Code {
797 pub require: bool,
798 pub field: String,
799 pub mode: String,
800 pub title: String,
801 pub def: String,
802 pub length: usize,
803
804 pub show: bool,
805 pub describe: String,
806
807 pub dec: String,
809 pub example: JsonValue,
810}
811
812impl Code {
813 pub fn new(
814 require: bool,
815 field: &str,
816 title: &str,
817 length: usize,
818 dec: &str,
819 default: &str,
820 ) -> Self {
821 Self {
822 require,
823 field: field.to_string(),
824 mode: "code".to_string(),
825 title: title.to_string(),
826 def: default.to_string(),
827 length: length + dec.len(),
828 show: true,
829 describe: "".to_string(),
830 dec: dec.to_string(),
831 example: JsonValue::Null,
832 }
833 }
834
835 pub fn order_code(prefix: &str, machine_id: u16) -> String {
837 let now = Local::now();
838 let time_str = now.format("%Y%m%d%H%M%S");
839 let millis = now.timestamp_subsec_millis();
840 let seq = SEQ.fetch_add(1, Ordering::SeqCst) % 1000;
842 let rand_part: u16 = rand::rng().random_range(0..1000);
844 format!(
845 "{}{}{:02}{:03}{:03}{:03}",
846 prefix,
847 time_str,
848 machine_id % 100,
849 millis,
850 rand_part,
851 seq
852 )
853 }
854 pub fn code_no_13(prefix: &str) -> String {
856 let data = Datetime::datetime_format("%Y%m%d%H%f");
857 let data = data.trim_end_matches("00");
858 format!("{prefix}{data}")
859 }
860 pub fn batch_no(prefix: &str) -> String {
862 let data = Datetime::datetime_format("%Y%m%d%H");
863 format!("{prefix}{data}")
864 }
865}
866
867impl Field for Code {
868 fn require(&mut self, require: bool) -> &mut Self {
869 self.require = require;
870 self
871 }
872 fn sql(&mut self, model: &str) -> String {
873 let dot = if model == "pgsql" { "" } else { "`" };
874 let sql = format!(
875 "{dot}{}{dot} varchar({}) default '{}'",
876 self.field, self.length, self.def
877 );
878 match model {
879 "sqlite" => sql,
880 _ => format!(
881 "{} comment '{}|{}|{}|{}|{}|{}'",
882 sql.clone(),
883 self.mode,
884 self.require,
885 self.title,
886 self.length,
887 self.dec,
888 self.def
889 ),
890 }
891 }
892 fn hide(&mut self) -> &mut Self {
893 self.show = false;
894 self
895 }
896 fn describe(&mut self, text: &str) -> &mut Self {
897 self.describe = text.to_string();
898 self
899 }
900
901 fn field(&mut self) -> JsonValue {
902 let mut field = object! {};
903 field.insert("require", JsonValue::from(self.require)).unwrap();
904 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
905 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
906 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
907 field.insert("length", JsonValue::from(self.length)).unwrap();
908 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
909
910 field.insert("show", JsonValue::from(self.show)).unwrap();
911 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
912 field.insert("dec", JsonValue::from(self.dec.clone())).unwrap();
913 field.insert("example", self.example.clone()).unwrap();
914
915 field
916 }
917
918 fn swagger(&mut self) -> JsonValue {
919 object! {
920 "type": self.mode.clone(),
921 "example": self.example.clone(),
922 }
923 }
924 fn example(&mut self, data: JsonValue) -> &mut Self {
925 self.example = data.clone();
926 self
927 }
928}
929
930pub struct BarCode {
932 pub require: bool,
934 pub field: String,
936 pub mode: String,
938 pub title: String,
940 pub def: String,
942 pub length: usize,
944 pub show: bool,
946 pub describe: String,
948 pub example: JsonValue,
949}
950
951impl BarCode {
952 pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
953 Self {
954 require,
955 field: field.to_string(),
956 mode: "barcode".to_string(),
957 title: title.to_string(),
958 def: default.to_string(),
959 length: 20,
960 show: true,
961 describe: "".to_string(),
962 example: JsonValue::Null,
963 }
964 }
965 pub fn create_barcode(prefix: &str) -> String {
967 let data = Datetime::datetime_format("%Y%m%d%H");
968 format!("{prefix}{data}")
969 }
970}
971
972impl Field for BarCode {
973 fn require(&mut self, require: bool) -> &mut Self {
974 self.require = require;
975 self
976 }
977 fn sql(&mut self, model: &str) -> String {
978 let dot = if model == "pgsql" { "" } else { "`" };
979 let sql = format!(
980 "{dot}{}{dot} varchar({}) default '{}'",
981 self.field, self.length, self.def
982 );
983 match model {
984 "sqlite" => sql,
985 _ => format!(
986 "{} comment '{}|{}|{}|{}|{}'",
987 sql.clone(),
988 self.mode,
989 self.require,
990 self.title,
991 self.length,
992 self.def
993 ),
994 }
995 }
996 fn hide(&mut self) -> &mut Self {
997 self.show = false;
998 self
999 }
1000 fn describe(&mut self, text: &str) -> &mut Self {
1001 self.describe = text.to_string();
1002 self
1003 }
1004
1005 fn field(&mut self) -> JsonValue {
1006 let mut field = object! {};
1007 field.insert("require", JsonValue::from(self.require)).unwrap();
1008 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
1009 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
1010 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
1011 field.insert("length", JsonValue::from(self.length)).unwrap();
1012 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
1013
1014 field.insert("show", JsonValue::from(self.show)).unwrap();
1015 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
1016 field.insert("example", self.example.clone()).unwrap();
1017 field
1018 }
1019
1020 fn swagger(&mut self) -> JsonValue {
1021 object! {
1022 "type": self.mode.clone(),
1023 "example": self.example.clone(),
1024 }
1025 }
1026
1027 fn example(&mut self, data: JsonValue) -> &mut Self {
1028 self.example = data.clone();
1029 self
1030 }
1031}
1032
1033pub struct QrCode {
1035 pub require: bool,
1037 pub field: String,
1039 pub mode: String,
1041 pub title: String,
1043 pub def: String,
1045 pub length: usize,
1047 pub show: bool,
1049 pub describe: String,
1051 pub example: JsonValue,
1052}
1053
1054impl QrCode {
1055 pub fn new(require: bool, field: &str, title: &str, length: usize, default: &str) -> Self {
1056 Self {
1057 require,
1058 field: field.to_string(),
1059 mode: "qrcode".to_string(),
1060 title: title.to_string(),
1061 def: default.to_string(),
1062 length,
1063 show: true,
1064 describe: "".to_string(),
1065 example: JsonValue::Null,
1066 }
1067 }
1068}
1069
1070impl Field for QrCode {
1071 fn require(&mut self, require: bool) -> &mut Self {
1072 self.require = require;
1073 self
1074 }
1075 fn sql(&mut self, model: &str) -> String {
1076 let dot = if model == "pgsql" { "" } else { "`" };
1077 let sql = format!(
1078 "{dot}{}{dot} varchar({}) default '{}'",
1079 self.field, self.length, self.def
1080 );
1081 match model {
1082 "sqlite" => sql,
1083 _ => format!(
1084 "{} comment '{}|{}|{}|{}|{}'",
1085 sql.clone(),
1086 self.mode,
1087 self.require,
1088 self.title,
1089 self.length,
1090 self.def
1091 ),
1092 }
1093 }
1094 fn hide(&mut self) -> &mut Self {
1095 self.show = false;
1096 self
1097 }
1098 fn describe(&mut self, text: &str) -> &mut Self {
1099 self.describe = text.to_string();
1100 self
1101 }
1102
1103 fn field(&mut self) -> JsonValue {
1104 let mut field = object! {};
1105 field.insert("require", JsonValue::from(self.require)).unwrap();
1106 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
1107 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
1108 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
1109 field.insert("length", JsonValue::from(self.length)).unwrap();
1110 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
1111
1112 field.insert("show", JsonValue::from(self.show)).unwrap();
1113 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
1114 field.insert("example", self.example.clone()).unwrap();
1115
1116 field
1117 }
1118
1119 fn swagger(&mut self) -> JsonValue {
1120 object! {
1121 "type": self.mode.clone(),
1122 "example": self.example.clone(),
1123 }
1124 }
1125
1126 fn example(&mut self, data: JsonValue) -> &mut Self {
1127 self.example = data.clone();
1128 self
1129 }
1130}
1131#[cfg(test)]
1132mod tests {
1133 #[test]
1134 fn code() {
1135 let data = crate::str::Code::order_code("Y", 1);
1136 println!("code: {} {}", data, data.len());
1137 let data = crate::str::Code::order_code("Y", 1);
1138 println!("code: {} {}", data, data.len());
1139 let data = crate::str::Code::order_code("Y", 2);
1140 println!("code: {} {}", data, data.len());
1141 let data = crate::str::Code::order_code("Y", 2);
1142 println!("code: {} {}", data, data.len());
1143 let data = crate::str::Code::order_code("Y", 3);
1144 println!("code: {} {}", data, data.len());
1145 let data = crate::str::Code::order_code("Y", 3);
1146 println!("code: {} {}", data, data.len());
1147 }
1148}