1use crate::datetime::Datetime;
2use crate::Field;
3use chrono::Local;
4use json::{object, JsonValue};
5use lazy_static::lazy_static;
6use rand::Rng;
7use regex::Regex;
8use std::sync::atomic::{AtomicU16, Ordering};
9
10static SEQ: AtomicU16 = AtomicU16::new(0);
12#[derive(Debug, Clone)]
22pub struct Str {
23 pub require: bool,
24 pub field: String,
25 pub mode: String,
26 pub title: String,
27 pub def: String,
28 pub length: usize,
29 pub show: bool,
30 pub describe: String,
31 pub example: JsonValue,
32}
33
34impl Str {
35 pub fn new(require: bool, field: &str, title: &str, length: usize, default: &str) -> Str {
36 Self {
37 require,
38 field: field.to_string(),
39 mode: "string".to_string(),
40 title: title.to_string(),
41 def: default.to_string(),
42 length,
43 show: true,
44 describe: "".to_string(),
45 example: JsonValue::Null,
46 }
47 }
48}
49
50impl Field for Str {
51 fn require(&mut self, require: bool) -> &mut Self {
52 self.require = require;
53 self
54 }
55
56 fn sql(&mut self, model: &str) -> String {
57 let not_null = if self.require { " not null" } else { "" };
58 match model {
59 "sqlite" => format!(
60 "{} varchar({}){} default '{}'",
61 self.field, self.length, not_null, self.def
62 ),
63 "pgsql" => {
64 format!(
65 r#""{}" varchar({}){} default '{}'"#,
66 self.field, self.length, not_null, self.def
67 )
68 }
69 _ => {
70 let sql = format!(
71 "`{}` varchar({}){} default '{}'",
72 self.field, self.length, not_null, self.def
73 );
74 format!(
75 "{} comment '{}|{}|{}|{}|{}'",
76 sql.clone(),
77 self.mode,
78 self.require,
79 self.title,
80 self.length,
81 self.def
82 )
83 }
84 }
85 }
86 fn hide(&mut self) -> &mut Self {
87 self.show = false;
88 self
89 }
90 fn describe(&mut self, text: &str) -> &mut Self {
91 self.describe = text.to_string();
92 self
93 }
94 fn field(&mut self) -> JsonValue {
95 let mut field = object! {};
96 field
97 .insert("require", JsonValue::from(self.require))
98 .unwrap();
99 field
100 .insert("field", JsonValue::from(self.field.clone()))
101 .unwrap();
102 field
103 .insert("mode", JsonValue::from(self.mode.clone()))
104 .unwrap();
105 field
106 .insert("title", JsonValue::from(self.title.clone()))
107 .unwrap();
108 field
109 .insert("length", JsonValue::from(self.length))
110 .unwrap();
111 field
112 .insert("def", JsonValue::from(self.def.clone()))
113 .unwrap();
114
115 field.insert("show", JsonValue::from(self.show)).unwrap();
116 field
117 .insert("describe", JsonValue::from(self.describe.clone()))
118 .unwrap();
119 field.insert("example", self.example.clone()).unwrap();
120
121 field
122 }
123
124 fn swagger(&mut self) -> JsonValue {
125 object! {
126 "type": self.mode.clone(),
127 "example": self.example.clone(),
128 }
129 }
130 fn example(&mut self, data: JsonValue) -> &mut Self {
131 self.example = data;
132 self
133 }
134}
135
136pub struct Pass {
145 pub require: bool,
146 pub field: String,
147 pub mode: String,
148 pub title: String,
149 pub def: String,
150 pub length: usize,
151 pub show: bool,
152 pub describe: String,
153 pub example: JsonValue,
154}
155
156impl Pass {
157 pub fn new(require: bool, field: &str, title: &str, length: usize, default: &str) -> Self {
158 Self {
159 require,
160 field: field.to_string(),
161 mode: "pass".to_string(),
162 title: title.to_string(),
163 def: default.to_string(),
164 length,
165 show: true,
166 describe: "".to_string(),
167 example: json::Null,
168 }
169 }
170 pub fn random_pass(len: usize) -> String {
172 let text = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789";
173 let mut code = "".to_string();
174 for _ in 0..len {
175 let index = rand::rng().random_range(0..=text.len() - 1);
176 let t = &text[index..=index];
177 code = format!("{}{}", code, t);
178 }
179 code
180 }
181}
182
183impl Field for Pass {
184 fn require(&mut self, require: bool) -> &mut Self {
185 self.require = require;
186 self
187 }
188
189 fn sql(&mut self, model: &str) -> String {
190 let not_null = if self.require { " not null" } else { "" };
191 match model {
192 "sqlite" => format!(
193 "{} varchar({}){} default '{}'",
194 self.field, self.length, not_null, self.def
195 ),
196 "pgsql" => {
197 format!(
198 r#""{}" varchar({}){} default '{}'"#,
199 self.field, self.length, not_null, self.def
200 )
201 }
202 _ => {
203 let sql = format!(
204 "`{}` varchar({}){} default '{}'",
205 self.field, self.length, not_null, self.def
206 );
207 format!(
208 "{} comment '{}|{}|{}|{}|{}'",
209 sql.clone(),
210 self.mode,
211 self.require,
212 self.title,
213 self.length,
214 self.def
215 )
216 }
217 }
218 }
219 fn hide(&mut self) -> &mut Self {
220 self.show = false;
221 self
222 }
223 fn describe(&mut self, text: &str) -> &mut Self {
224 self.describe = text.to_string();
225 self
226 }
227 fn field(&mut self) -> JsonValue {
228 let mut field = object! {};
229 field
230 .insert("require", JsonValue::from(self.require))
231 .unwrap();
232 field
233 .insert("field", JsonValue::from(self.field.clone()))
234 .unwrap();
235 field
236 .insert("mode", JsonValue::from(self.mode.clone()))
237 .unwrap();
238 field
239 .insert("title", JsonValue::from(self.title.clone()))
240 .unwrap();
241 field
242 .insert("length", JsonValue::from(self.length))
243 .unwrap();
244 field
245 .insert("def", JsonValue::from(self.def.clone()))
246 .unwrap();
247
248 field.insert("show", JsonValue::from(self.show)).unwrap();
249 field
250 .insert("describe", JsonValue::from(self.describe.clone()))
251 .unwrap();
252 field.insert("example", self.example.clone()).unwrap();
253
254 field
255 }
256
257 fn swagger(&mut self) -> JsonValue {
258 object! {
259 "type": self.mode.clone(),
260 "example": self.example.clone(),
261 }
262 }
263
264 fn example(&mut self, data: JsonValue) -> &mut Self {
265 self.example = data;
266 self
267 }
268}
269
270#[derive(Clone, Debug)]
278pub struct Key {
279 pub require: bool,
280 pub field: String,
281 pub mode: String,
282 pub title: String,
283 pub length: usize,
284
285 pub def: String,
286
287 pub auto: bool,
288 pub show: bool,
289 pub describe: String,
290 pub example: JsonValue,
291}
292
293impl Key {
294 pub fn new(require: bool, field: &str, title: &str, length: usize) -> Self {
295 Self {
296 require,
297 field: field.to_string(),
298 mode: "key".to_string(),
299 title: title.to_string(),
300 length,
301 auto: false,
302 show: true,
303 def: "".to_string(),
304 describe: "".to_string(),
305 example: JsonValue::Null,
306 }
307 }
308 pub fn auto(mut self, auto: bool) -> Self {
309 if auto {
310 self.length = 0;
311 }
312 self.auto = auto;
313 self
314 }
315}
316
317impl Field for Key {
318 fn require(&mut self, require: bool) -> &mut Self {
319 self.require = require;
320 self
321 }
322 fn sql(&mut self, model: &str) -> String {
323 let not_null = if self.require { " not null" } else { "" };
324 match model {
325 "sqlite" => match self.auto {
326 true => {
327 format!("{} INTEGER PRIMARY KEY AUTOINCREMENT", self.field)
328 }
329 false => {
330 format!(
331 "{} varchar({}){} default ''",
332 self.field, self.length, not_null
333 )
334 }
335 },
336 "pgsql" => match self.auto {
337 true => {
338 format!(r#""{}" SERIAL"#, self.field)
339 }
340 false => {
341 format!(
342 r#""{}" varchar({}){} default ''"#,
343 self.field, self.length, not_null
344 )
345 }
346 },
347 _ => match self.auto {
348 true => {
349 let sql = format!("`{}` int{} AUTO_INCREMENT", self.field, not_null);
350 format!(
351 "{} comment '{}|{}|{}'",
352 sql.clone(),
353 self.mode,
354 self.title,
355 self.length
356 )
357 }
358 false => {
359 let sql = format!(
360 "`{}` varchar({}){} default ''",
361 self.field, self.length, not_null
362 );
363 format!(
364 "{} comment '{}|{}|{}'",
365 sql.clone(),
366 self.mode,
367 self.title,
368 self.length
369 )
370 }
371 },
372 }
373 }
374 fn hide(&mut self) -> &mut Self {
375 self.show = false;
376 self
377 }
378
379 fn describe(&mut self, text: &str) -> &mut Self {
380 self.describe = text.to_string();
381 self
382 }
383 fn field(&mut self) -> JsonValue {
384 let mut field = object! {};
385 field
386 .insert("require", JsonValue::from(self.require))
387 .unwrap();
388 field
389 .insert("field", JsonValue::from(self.field.clone()))
390 .unwrap();
391 field
392 .insert("mode", JsonValue::from(self.mode.clone()))
393 .unwrap();
394 field
395 .insert("title", JsonValue::from(self.title.clone()))
396 .unwrap();
397 field
398 .insert("length", JsonValue::from(self.length))
399 .unwrap();
400 field
401 .insert("def", JsonValue::from(self.def.clone()))
402 .unwrap();
403 field.insert("auto", JsonValue::from(self.auto)).unwrap();
404
405 field.insert("show", JsonValue::from(self.show)).unwrap();
406 field
407 .insert("describe", JsonValue::from(self.describe.clone()))
408 .unwrap();
409 field.insert("example", self.example.clone()).unwrap();
410
411 field
412 }
413 fn swagger(&mut self) -> JsonValue {
414 object! {
415 "type": self.mode.clone(),
416 "example": self.example.clone(),
417 }
418 }
419
420 fn example(&mut self, data: JsonValue) -> &mut Self {
421 self.example = data;
422 self
423 }
424}
425
426pub struct Tel {
435 pub require: bool,
436 pub field: String,
437 pub mode: String,
438 pub title: String,
439 pub def: String,
440 pub length: i32,
441
442 pub show: bool,
443 pub describe: String,
444 pub example: JsonValue,
445}
446
447impl Tel {
448 pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
449 Self {
450 require,
451 field: field.to_string(),
452 mode: "tel".to_string(),
453 title: title.to_string(),
454 def: default.to_string(),
455 length: 15,
456
457 show: true,
458 describe: "".to_string(),
459 example: JsonValue::Null,
460 }
461 }
462 pub fn is_phone(input: &str) -> bool {
463 lazy_static! {
465 static ref CN_RE: Regex = Regex::new(r"^1[3-9]\d{9}$").unwrap();
467 static ref INTL_RE: Regex = Regex::new(r"^\+?[1-9]\d{1,14}$").unwrap();
469 static ref INTL_00_RE: Regex = Regex::new(r"^00[1-9]\d{1,14}$").unwrap();
471 }
472 CN_RE.is_match(input) || INTL_RE.is_match(input) || INTL_00_RE.is_match(input)
473 }
474}
475
476impl Field for Tel {
477 fn require(&mut self, require: bool) -> &mut Self {
478 self.require = require;
479 self
480 }
481 fn sql(&mut self, model: &str) -> String {
482 let not_null = if self.require { " not null" } else { "" };
483 match model {
484 "sqlite" => format!(
485 "{} varchar({}){} default '{}'",
486 self.field, self.length, not_null, self.def
487 ),
488 "pgsql" => {
489 format!(
490 r#""{}" varchar({}){} default '{}'"#,
491 self.field, self.length, not_null, self.def
492 )
493 }
494 _ => {
495 let sql = format!(
496 "`{}` varchar({}){} default '{}'",
497 self.field, self.length, not_null, self.def
498 );
499 format!(
500 "{} comment '{}|{}|{}|{}|{}'",
501 sql.clone(),
502 self.mode,
503 self.require,
504 self.title,
505 self.length,
506 self.def
507 )
508 }
509 }
510 }
511 fn hide(&mut self) -> &mut Self {
512 self.show = false;
513 self
514 }
515 fn describe(&mut self, text: &str) -> &mut Self {
516 self.describe = text.to_string();
517 self
518 }
519
520 fn field(&mut self) -> JsonValue {
521 let mut field = object! {};
522 field
523 .insert("require", JsonValue::from(self.require))
524 .unwrap();
525 field
526 .insert("field", JsonValue::from(self.field.clone()))
527 .unwrap();
528 field
529 .insert("mode", JsonValue::from(self.mode.clone()))
530 .unwrap();
531 field
532 .insert("title", JsonValue::from(self.title.clone()))
533 .unwrap();
534 field
535 .insert("length", JsonValue::from(self.length))
536 .unwrap();
537 field
538 .insert("def", JsonValue::from(self.def.clone()))
539 .unwrap();
540
541 field.insert("show", JsonValue::from(self.show)).unwrap();
542 field
543 .insert("describe", JsonValue::from(self.describe.clone()))
544 .unwrap();
545 field.insert("example", self.example.clone()).unwrap();
546
547 field
548 }
549
550 fn swagger(&mut self) -> JsonValue {
551 object! {
552 "type": self.mode.clone(),
553 "example": self.example.clone(),
554 }
555 }
556 fn example(&mut self, data: JsonValue) -> &mut Self {
557 self.example = data;
558 self
559 }
560}
561
562pub struct Ident {
571 pub require: bool,
572 pub field: String,
573 pub mode: String,
574 pub title: String,
575 pub def: String,
576 pub length: i32,
577
578 pub show: bool,
579 pub describe: String,
580 pub example: JsonValue,
581}
582
583impl Ident {
584 pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
585 Self {
586 require,
587 field: field.to_string(),
588 mode: "ident".to_string(),
589 title: title.to_string(),
590 def: default.to_string(),
591 length: 18,
592
593 show: true,
594 describe: "".to_string(),
595 example: JsonValue::Null,
596 }
597 }
598}
599
600impl Field for Ident {
601 fn require(&mut self, require: bool) -> &mut Self {
602 self.require = require;
603 self
604 }
605 fn sql(&mut self, model: &str) -> String {
606 let not_null = if self.require { " not null" } else { "" };
607 match model {
608 "sqlite" => format!(
609 "{} varchar({}){} default '{}'",
610 self.field, self.length, not_null, self.def
611 ),
612 "pgsql" => {
613 format!(
614 r#""{}" varchar({}){} default '{}'"#,
615 self.field, self.length, not_null, self.def
616 )
617 }
618 _ => {
619 let sql = format!(
620 "`{}` varchar({}){} default '{}'",
621 self.field, self.length, not_null, self.def
622 );
623 format!(
624 "{} comment '{}|{}|{}|{}|{}'",
625 sql.clone(),
626 self.mode,
627 self.require,
628 self.title,
629 self.length,
630 self.def
631 )
632 }
633 }
634 }
635 fn hide(&mut self) -> &mut Self {
636 self.show = false;
637 self
638 }
639 fn describe(&mut self, text: &str) -> &mut Self {
640 self.describe = text.to_string();
641 self
642 }
643
644 fn field(&mut self) -> JsonValue {
645 let mut field = object! {};
646 field
647 .insert("require", JsonValue::from(self.require))
648 .unwrap();
649 field
650 .insert("field", JsonValue::from(self.field.clone()))
651 .unwrap();
652 field
653 .insert("mode", JsonValue::from(self.mode.clone()))
654 .unwrap();
655 field
656 .insert("title", JsonValue::from(self.title.clone()))
657 .unwrap();
658 field
659 .insert("length", JsonValue::from(self.length))
660 .unwrap();
661 field
662 .insert("def", JsonValue::from(self.def.clone()))
663 .unwrap();
664
665 field.insert("show", JsonValue::from(self.show)).unwrap();
666 field
667 .insert("describe", JsonValue::from(self.describe.clone()))
668 .unwrap();
669 field.insert("example", self.example.clone()).unwrap();
670
671 field
672 }
673
674 fn swagger(&mut self) -> JsonValue {
675 object! {
676 "type": self.mode.clone(),
677 "example": self.example.clone(),
678 }
679 }
680 fn example(&mut self, data: JsonValue) -> &mut Self {
681 self.example = data;
682 self
683 }
684}
685pub struct Email {
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 Email {
707 pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
708 Self {
709 require,
710 field: field.to_string(),
711 mode: "email".to_string(),
712 title: title.to_string(),
713 def: default.to_string(),
714 length: 100,
715
716 show: true,
717 describe: "".to_string(),
718 example: JsonValue::Null,
719 }
720 }
721 pub fn is_email(email: &str) -> bool {
722 lazy_static! {
724 static ref EMAIL_RE: Regex =
725 Regex::new(r"(?i)^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$").unwrap();
726 }
727 EMAIL_RE.is_match(email)
728 }
729}
730
731impl Field for Email {
732 fn require(&mut self, require: bool) -> &mut Self {
733 self.require = require;
734 self
735 }
736 fn sql(&mut self, model: &str) -> String {
737 let not_null = if self.require { " not null" } else { "" };
738 match model {
739 "sqlite" => format!(
740 "{} varchar({}){} default '{}'",
741 self.field, self.length, not_null, self.def
742 ),
743 "pgsql" => {
744 format!(
745 r#""{}" varchar({}){} default '{}'"#,
746 self.field, self.length, not_null, self.def
747 )
748 }
749 _ => {
750 let sql = format!(
751 "`{}` varchar({}){} default '{}'",
752 self.field, self.length, not_null, self.def
753 );
754 format!(
755 "{} comment '{}|{}|{}|{}|{}'",
756 sql.clone(),
757 self.mode,
758 self.require,
759 self.title,
760 self.length,
761 self.def
762 )
763 }
764 }
765 }
766 fn hide(&mut self) -> &mut Self {
767 self.show = false;
768 self
769 }
770 fn describe(&mut self, text: &str) -> &mut Self {
771 self.describe = text.to_string();
772 self
773 }
774 fn field(&mut self) -> JsonValue {
775 let mut field = object! {};
776 field
777 .insert("require", JsonValue::from(self.require))
778 .unwrap();
779 field
780 .insert("field", JsonValue::from(self.field.clone()))
781 .unwrap();
782 field
783 .insert("mode", JsonValue::from(self.mode.clone()))
784 .unwrap();
785 field
786 .insert("title", JsonValue::from(self.title.clone()))
787 .unwrap();
788 field
789 .insert("length", JsonValue::from(self.length))
790 .unwrap();
791 field
792 .insert("def", JsonValue::from(self.def.clone()))
793 .unwrap();
794
795 field.insert("show", JsonValue::from(self.show)).unwrap();
796 field
797 .insert("describe", JsonValue::from(self.describe.clone()))
798 .unwrap();
799 field.insert("example", self.example.clone()).unwrap();
800 field
801 }
802
803 fn verify(&mut self, data: JsonValue) -> JsonValue {
804 data
805 }
806
807 fn swagger(&mut self) -> JsonValue {
808 object! {
809 "type": self.mode.clone(),
810 "example": self.example.clone(),
811 }
812 }
813 fn example(&mut self, data: JsonValue) -> &mut Self {
814 self.example = data.clone();
815 self
816 }
817}
818
819pub struct Color {
826 pub require: bool,
827 pub field: String,
828 pub mode: String,
829 pub title: String,
830 pub def: String,
831 pub length: i32,
832
833 pub show: bool,
834 pub describe: String,
835 pub example: JsonValue,
836}
837
838impl Color {
839 pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
840 Self {
841 require,
842 field: field.to_string(),
843 mode: "color".to_string(),
844 title: title.to_string(),
845 def: default.to_string(),
846 length: 9,
847
848 show: true,
849 describe: "".to_string(),
850 example: JsonValue::Null,
851 }
852 }
853}
854
855impl Field for Color {
856 fn require(&mut self, require: bool) -> &mut Self {
857 self.require = require;
858 self
859 }
860 fn sql(&mut self, model: &str) -> String {
861 let not_null = if self.require { " not null" } else { "" };
862 match model {
863 "sqlite" => format!(
864 "{} varchar({}){} default '{}'",
865 self.field, self.length, not_null, self.def
866 ),
867 "pgsql" => {
868 format!(
869 r#""{}" varchar({}){} default '{}'"#,
870 self.field, self.length, not_null, self.def
871 )
872 }
873 _ => {
874 let sql = format!(
875 "`{}` varchar({}){} default '{}'",
876 self.field, self.length, not_null, self.def
877 );
878 format!(
879 "{} comment '{}|{}|{}|{}|{}'",
880 sql.clone(),
881 self.mode,
882 self.require,
883 self.title,
884 self.length,
885 self.def
886 )
887 }
888 }
889 }
890 fn hide(&mut self) -> &mut Self {
891 self.show = false;
892 self
893 }
894 fn describe(&mut self, text: &str) -> &mut Self {
895 self.describe = text.to_string();
896 self
897 }
898 fn field(&mut self) -> JsonValue {
899 let mut field = object! {};
900 field
901 .insert("require", JsonValue::from(self.require))
902 .unwrap();
903 field
904 .insert("field", JsonValue::from(self.field.clone()))
905 .unwrap();
906 field
907 .insert("mode", JsonValue::from(self.mode.clone()))
908 .unwrap();
909 field
910 .insert("title", JsonValue::from(self.title.clone()))
911 .unwrap();
912 field
913 .insert("length", JsonValue::from(self.length))
914 .unwrap();
915 field
916 .insert("def", JsonValue::from(self.def.clone()))
917 .unwrap();
918
919 field.insert("show", JsonValue::from(self.show)).unwrap();
920 field
921 .insert("describe", JsonValue::from(self.describe.clone()))
922 .unwrap();
923 field.insert("example", self.example.clone()).unwrap();
924
925 field
926 }
927
928 fn verify(&mut self, data: JsonValue) -> JsonValue {
929 data
930 }
931
932 fn swagger(&mut self) -> JsonValue {
933 object! {
934 "type": self.mode.clone(),
935 "example": self.example.clone(),
936 }
937 }
938 fn example(&mut self, data: JsonValue) -> &mut Self {
939 self.example = data.clone();
940 self
941 }
942}
943
944pub struct Code {
954 pub require: bool,
955 pub field: String,
956 pub mode: String,
957 pub title: String,
958 pub def: String,
959 pub length: usize,
960
961 pub show: bool,
962 pub describe: String,
963
964 pub dec: String,
966 pub example: JsonValue,
967}
968
969impl Code {
970 pub fn new(
971 require: bool,
972 field: &str,
973 title: &str,
974 length: usize,
975 dec: &str,
976 default: &str,
977 ) -> Self {
978 Self {
979 require,
980 field: field.to_string(),
981 mode: "code".to_string(),
982 title: title.to_string(),
983 def: default.to_string(),
984 length: length + dec.len(),
985 show: true,
986 describe: "".to_string(),
987 dec: dec.to_string(),
988 example: JsonValue::Null,
989 }
990 }
991
992 pub fn order_code(prefix: &str, machine_id: u16) -> String {
994 let now = Local::now();
995 let time_str = now.format("%Y%m%d%H%M%S");
996 let millis = now.timestamp_subsec_millis();
997 let seq = SEQ.fetch_add(1, Ordering::SeqCst) % 1000;
999 let rand_part: u16 = rand::rng().random_range(0..1000);
1001 format!(
1002 "{}{}{:02}{:03}{:03}{:03}",
1003 prefix,
1004 time_str,
1005 machine_id % 100,
1006 millis,
1007 rand_part,
1008 seq
1009 )
1010 }
1011 pub fn code_no_13(prefix: &str) -> String {
1013 let data = Datetime::datetime_format("%Y%m%d%H%f");
1014 let data = data.trim_end_matches("00");
1015 format!("{prefix}{data}")
1016 }
1017 pub fn batch_no(prefix: &str) -> String {
1019 let data = Datetime::datetime_format("%Y%m%d%H");
1020 format!("{prefix}{data}")
1021 }
1022 pub fn random_number_f64(begin: f64, finish: f64) -> f64 {
1024 rand::rng().random_range(begin..=finish)
1025 }
1026 pub fn verification_code(len: usize) -> String {
1028 let text = "0123456789";
1029 let mut code = "".to_string();
1030 for _ in 0..len {
1031 let index = rand::rng().random_range(0..=text.len() - 1);
1032 let t = &text[index..=index];
1033 code = format!("{}{}", code, t);
1034 }
1035 code
1036 }
1037}
1038
1039impl Field for Code {
1040 fn require(&mut self, require: bool) -> &mut Self {
1041 self.require = require;
1042 self
1043 }
1044 fn sql(&mut self, model: &str) -> String {
1045 let not_null = if self.require { " not null" } else { "" };
1046 match model {
1047 "sqlite" => format!(
1048 "{} varchar({}){} default '{}'",
1049 self.field, self.length, not_null, self.def
1050 ),
1051 "pgsql" => {
1052 format!(
1053 r#""{}" varchar({}){} default '{}'"#,
1054 self.field, self.length, not_null, self.def
1055 )
1056 }
1057 _ => {
1058 let sql = format!(
1059 "`{}` varchar({}){} default '{}'",
1060 self.field, self.length, not_null, self.def
1061 );
1062 format!(
1063 "{} comment '{}|{}|{}|{}|{}|{}'",
1064 sql.clone(),
1065 self.mode,
1066 self.require,
1067 self.title,
1068 self.length,
1069 self.dec,
1070 self.def
1071 )
1072 }
1073 }
1074 }
1075 fn hide(&mut self) -> &mut Self {
1076 self.show = false;
1077 self
1078 }
1079 fn describe(&mut self, text: &str) -> &mut Self {
1080 self.describe = text.to_string();
1081 self
1082 }
1083
1084 fn field(&mut self) -> JsonValue {
1085 let mut field = object! {};
1086 field
1087 .insert("require", JsonValue::from(self.require))
1088 .unwrap();
1089 field
1090 .insert("field", JsonValue::from(self.field.clone()))
1091 .unwrap();
1092 field
1093 .insert("mode", JsonValue::from(self.mode.clone()))
1094 .unwrap();
1095 field
1096 .insert("title", JsonValue::from(self.title.clone()))
1097 .unwrap();
1098 field
1099 .insert("length", JsonValue::from(self.length))
1100 .unwrap();
1101 field
1102 .insert("def", JsonValue::from(self.def.clone()))
1103 .unwrap();
1104
1105 field.insert("show", JsonValue::from(self.show)).unwrap();
1106 field
1107 .insert("describe", JsonValue::from(self.describe.clone()))
1108 .unwrap();
1109 field
1110 .insert("dec", JsonValue::from(self.dec.clone()))
1111 .unwrap();
1112 field.insert("example", self.example.clone()).unwrap();
1113
1114 field
1115 }
1116
1117 fn swagger(&mut self) -> JsonValue {
1118 object! {
1119 "type": self.mode.clone(),
1120 "example": self.example.clone(),
1121 }
1122 }
1123 fn example(&mut self, data: JsonValue) -> &mut Self {
1124 self.example = data.clone();
1125 self
1126 }
1127}
1128
1129pub struct BarCode {
1131 pub require: bool,
1133 pub field: String,
1135 pub mode: String,
1137 pub title: String,
1139 pub def: String,
1141 pub length: usize,
1143 pub show: bool,
1145 pub describe: String,
1147 pub example: JsonValue,
1148}
1149
1150impl BarCode {
1151 pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
1152 Self {
1153 require,
1154 field: field.to_string(),
1155 mode: "barcode".to_string(),
1156 title: title.to_string(),
1157 def: default.to_string(),
1158 length: 20,
1159 show: true,
1160 describe: "".to_string(),
1161 example: JsonValue::Null,
1162 }
1163 }
1164 pub fn create_barcode(prefix: &str) -> String {
1166 let data = Datetime::datetime_format("%Y%m%d%H");
1167 format!("{prefix}{data}")
1168 }
1169}
1170
1171impl Field for BarCode {
1172 fn require(&mut self, require: bool) -> &mut Self {
1173 self.require = require;
1174 self
1175 }
1176 fn sql(&mut self, model: &str) -> String {
1177 let not_null = if self.require { " not null" } else { "" };
1178 match model {
1179 "sqlite" => format!(
1180 "{} varchar({}){} default '{}'",
1181 self.field, self.length, not_null, self.def
1182 ),
1183 "pgsql" => {
1184 format!(
1185 r#""{}" varchar({}){} default '{}'"#,
1186 self.field, self.length, not_null, self.def
1187 )
1188 }
1189 _ => {
1190 let sql = format!(
1191 "`{}` varchar({}){} default '{}'",
1192 self.field, self.length, not_null, self.def
1193 );
1194 format!(
1195 "{} comment '{}|{}|{}|{}|{}'",
1196 sql.clone(),
1197 self.mode,
1198 self.require,
1199 self.title,
1200 self.length,
1201 self.def
1202 )
1203 }
1204 }
1205 }
1206 fn hide(&mut self) -> &mut Self {
1207 self.show = false;
1208 self
1209 }
1210 fn describe(&mut self, text: &str) -> &mut Self {
1211 self.describe = text.to_string();
1212 self
1213 }
1214
1215 fn field(&mut self) -> JsonValue {
1216 let mut field = object! {};
1217 field
1218 .insert("require", JsonValue::from(self.require))
1219 .unwrap();
1220 field
1221 .insert("field", JsonValue::from(self.field.clone()))
1222 .unwrap();
1223 field
1224 .insert("mode", JsonValue::from(self.mode.clone()))
1225 .unwrap();
1226 field
1227 .insert("title", JsonValue::from(self.title.clone()))
1228 .unwrap();
1229 field
1230 .insert("length", JsonValue::from(self.length))
1231 .unwrap();
1232 field
1233 .insert("def", JsonValue::from(self.def.clone()))
1234 .unwrap();
1235
1236 field.insert("show", JsonValue::from(self.show)).unwrap();
1237 field
1238 .insert("describe", JsonValue::from(self.describe.clone()))
1239 .unwrap();
1240 field.insert("example", self.example.clone()).unwrap();
1241 field
1242 }
1243
1244 fn swagger(&mut self) -> JsonValue {
1245 object! {
1246 "type": self.mode.clone(),
1247 "example": self.example.clone(),
1248 }
1249 }
1250
1251 fn example(&mut self, data: JsonValue) -> &mut Self {
1252 self.example = data.clone();
1253 self
1254 }
1255}
1256
1257pub struct QrCode {
1259 pub require: bool,
1261 pub field: String,
1263 pub mode: String,
1265 pub title: String,
1267 pub def: String,
1269 pub length: usize,
1271 pub show: bool,
1273 pub describe: String,
1275 pub example: JsonValue,
1276}
1277
1278impl QrCode {
1279 pub fn new(require: bool, field: &str, title: &str, length: usize, default: &str) -> Self {
1280 Self {
1281 require,
1282 field: field.to_string(),
1283 mode: "qrcode".to_string(),
1284 title: title.to_string(),
1285 def: default.to_string(),
1286 length,
1287 show: true,
1288 describe: "".to_string(),
1289 example: JsonValue::Null,
1290 }
1291 }
1292}
1293
1294impl Field for QrCode {
1295 fn require(&mut self, require: bool) -> &mut Self {
1296 self.require = require;
1297 self
1298 }
1299 fn sql(&mut self, model: &str) -> String {
1300 let not_null = if self.require { " not null" } else { "" };
1301 match model {
1302 "sqlite" => format!(
1303 "{} varchar({}){} default '{}'",
1304 self.field, self.length, not_null, self.def
1305 ),
1306 "pgsql" => {
1307 format!(
1308 r#""{}" varchar({}){} default '{}'"#,
1309 self.field, self.length, not_null, self.def
1310 )
1311 }
1312 _ => {
1313 let sql = format!(
1314 "`{}` varchar({}){} default '{}'",
1315 self.field, self.length, not_null, self.def
1316 );
1317 format!(
1318 "{} comment '{}|{}|{}|{}|{}'",
1319 sql.clone(),
1320 self.mode,
1321 self.require,
1322 self.title,
1323 self.length,
1324 self.def
1325 )
1326 }
1327 }
1328 }
1329 fn hide(&mut self) -> &mut Self {
1330 self.show = false;
1331 self
1332 }
1333 fn describe(&mut self, text: &str) -> &mut Self {
1334 self.describe = text.to_string();
1335 self
1336 }
1337
1338 fn field(&mut self) -> JsonValue {
1339 let mut field = object! {};
1340 field
1341 .insert("require", JsonValue::from(self.require))
1342 .unwrap();
1343 field
1344 .insert("field", JsonValue::from(self.field.clone()))
1345 .unwrap();
1346 field
1347 .insert("mode", JsonValue::from(self.mode.clone()))
1348 .unwrap();
1349 field
1350 .insert("title", JsonValue::from(self.title.clone()))
1351 .unwrap();
1352 field
1353 .insert("length", JsonValue::from(self.length))
1354 .unwrap();
1355 field
1356 .insert("def", JsonValue::from(self.def.clone()))
1357 .unwrap();
1358
1359 field.insert("show", JsonValue::from(self.show)).unwrap();
1360 field
1361 .insert("describe", JsonValue::from(self.describe.clone()))
1362 .unwrap();
1363 field.insert("example", self.example.clone()).unwrap();
1364
1365 field
1366 }
1367
1368 fn swagger(&mut self) -> JsonValue {
1369 object! {
1370 "type": self.mode.clone(),
1371 "example": self.example.clone(),
1372 }
1373 }
1374
1375 fn example(&mut self, data: JsonValue) -> &mut Self {
1376 self.example = data.clone();
1377 self
1378 }
1379}
1380#[cfg(test)]
1381mod tests {
1382 #[test]
1383 fn code() {
1384 let data = crate::str::Code::order_code("Y", 1);
1385 println!("code: {} {}", data, data.len());
1386 let data = crate::str::Code::order_code("Y", 1);
1387 println!("code: {} {}", data, data.len());
1388 let data = crate::str::Code::order_code("Y", 2);
1389 println!("code: {} {}", data, data.len());
1390 let data = crate::str::Code::order_code("Y", 2);
1391 println!("code: {} {}", data, data.len());
1392 let data = crate::str::Code::order_code("Y", 3);
1393 println!("code: {} {}", data, data.len());
1394 let data = crate::str::Code::order_code("Y", 3);
1395 println!("code: {} {}", data, data.len());
1396 }
1397}