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