1use std::sync::atomic::{AtomicU16, Ordering};
2use chrono::Local;
3use crate::datetime::Datetime;
4use crate::Field;
5use json::{object, JsonValue};
6use rand::{Rng};
7use lazy_static::lazy_static;
8use regex::Regex;
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 match model {
58 "sqlite" => format!(
59 "{} varchar({}) default '{}'",
60 self.field, self.length, self.def
61 ),
62 "pgsql" => {
63 let sql = format!(
64 r#""{}" varchar({}) default '{}'"#,
65 self.field, self.length, self.def
66 );
67 format!(
68 "{} comment '{}|{}|{}|{}|{}'",
69 sql.clone(),
70 self.mode,
71 self.require,
72 self.title,
73 self.length,
74 self.def
75 )
76 }
77 _ => {
78 let sql = format!(
79 "`{}` varchar({}) default '{}'",
80 self.field, self.length, self.def
81 );
82 format!(
83 "{} comment '{}|{}|{}|{}|{}'",
84 sql.clone(),
85 self.mode,
86 self.require,
87 self.title,
88 self.length,
89 self.def
90 )
91 }
92 }
93 }
94 fn hide(&mut self) -> &mut Self {
95 self.show = false;
96 self
97 }
98 fn describe(&mut self, text: &str) -> &mut Self {
99 self.describe = text.to_string();
100 self
101 }
102 fn field(&mut self) -> JsonValue {
103 let mut field = object! {};
104 field.insert("require", JsonValue::from(self.require)).unwrap();
105 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
106 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
107 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
108 field.insert("length", JsonValue::from(self.length)).unwrap();
109 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
110
111 field.insert("show", JsonValue::from(self.show)).unwrap();
112 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
113 field.insert("example", self.example.clone()).unwrap();
114
115 field
116 }
117
118 fn swagger(&mut self) -> JsonValue {
119 object! {
120 "type": self.mode.clone(),
121 "example": self.example.clone(),
122 }
123 }
124 fn example(&mut self, data: JsonValue) -> &mut Self {
125 self.example = data;
126 self
127 }
128}
129
130pub struct Pass {
139 pub require: bool,
140 pub field: String,
141 pub mode: String,
142 pub title: String,
143 pub def: String,
144 pub length: usize,
145 pub show: bool,
146 pub describe: String,
147 pub example: JsonValue,
148}
149
150impl Pass {
151 pub fn new(require: bool, field: &str, title: &str, length: usize, default: &str) -> Self {
152 Self {
153 require,
154 field: field.to_string(),
155 mode: "pass".to_string(),
156 title: title.to_string(),
157 def: default.to_string(),
158 length,
159 show: true,
160 describe: "".to_string(),
161 example: json::Null,
162 }
163 }
164 pub fn random_pass(len: usize) -> String {
166 let text = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789";
167 let mut code = "".to_string();
168 for _ in 0..len {
169 let index = rand::rng().random_range(0..=text.len() - 1);
170 let t = &text[index..=index];
171 code = format!("{}{}", code, t);
172 }
173 code
174 }
175}
176
177impl Field for Pass {
178 fn require(&mut self, require: bool) -> &mut Self {
179 self.require = require;
180 self
181 }
182
183 fn sql(&mut self, model: &str) -> String {
184 match model {
185 "sqlite" => format!(
186 "`{}` varchar({}) default '{}'",
187 self.field, self.length, self.def
188 ),
189 "pgsql" => {
190 let sql = format!(
191 r#""{}" varchar({}) default '{}'"#,
192 self.field, self.length, self.def
193 );
194 format!(
195 "{} comment '{}|{}|{}|{}|{}'",
196 sql.clone(),
197 self.mode,
198 self.require,
199 self.title,
200 self.length,
201 self.def
202 )
203 }
204 _ => {
205 let sql = format!(
206 "`{}` varchar({}) default '{}'",
207 self.field, self.length, self.def
208 );
209 format!(
210 "{} comment '{}|{}|{}|{}|{}'",
211 sql.clone(),
212 self.mode,
213 self.require,
214 self.title,
215 self.length,
216 self.def
217 )
218 }
219 }
220 }
221 fn hide(&mut self) -> &mut Self {
222 self.show = false;
223 self
224 }
225 fn describe(&mut self, text: &str) -> &mut Self {
226 self.describe = text.to_string();
227 self
228 }
229 fn field(&mut self) -> JsonValue {
230 let mut field = object! {};
231 field.insert("require", JsonValue::from(self.require)).unwrap();
232 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
233 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
234 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
235 field.insert("length", JsonValue::from(self.length)).unwrap();
236 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
237
238 field.insert("show", JsonValue::from(self.show)).unwrap();
239 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
240 field.insert("example", self.example.clone()).unwrap();
241
242 field
243 }
244
245 fn swagger(&mut self) -> JsonValue {
246 object! {
247 "type": self.mode.clone(),
248 "example": self.example.clone(),
249 }
250 }
251
252 fn example(&mut self, data: JsonValue) -> &mut Self {
253 self.example = data;
254 self
255 }
256}
257
258#[derive(Clone, Debug)]
266pub struct Key {
267 pub require: bool,
268 pub field: String,
269 pub mode: String,
270 pub title: String,
271 pub length: usize,
272
273 pub def: String,
274
275 pub auto: bool,
276 pub show: bool,
277 pub describe: String,
278 pub example: JsonValue,
279}
280
281impl Key {
282 pub fn new(require: bool, field: &str, title: &str, length: usize) -> Self {
283 Self {
284 require,
285 field: field.to_string(),
286 mode: "key".to_string(),
287 title: title.to_string(),
288 length,
289 auto: false,
290 show: true,
291 def: "".to_string(),
292 describe: "".to_string(),
293 example: JsonValue::Null,
294 }
295 }
296 pub fn auto(mut self, auto: bool) -> Self {
297 if auto {
298 self.length = 0;
299 }
300 self.auto = auto;
301 self
302 }
303}
304
305impl Field for Key {
306 fn require(&mut self, require: bool) -> &mut Self {
307 self.require = require;
308 self
309 }
310 fn sql(&mut self, model: &str) -> String {
311 match model {
312 "sqlite" => match self.auto {
313 true => {
314 let sql = format!("`{}` INTEGER PRIMARY KEY", self.field);
315 format!("{} AUTOINCREMENT", sql.clone())
316 }
317 false => {
318 format!(
319 "`{}` varchar({}) default '' constraint {}_key primary key",
320 self.field, self.length, self.field
321 )
322 }
323 },
324 "pgsql" => match self.auto {
325 true => {
326 let sql = format!(r#""{}" int AUTO_INCREMENT"#, self.field);
327 format!(
328 "{} comment '{}|{}|{}'",
329 sql.clone(),
330 self.mode,
331 self.title,
332 self.length
333 )
334 }
335 false => {
336 let mut sql = format!(r#""{}" varchar({}) not null"#, self.field, self.length);
337 sql = sql.clone().to_string();
338 format!(
339 "{} comment '{}|{}|{}'",
340 sql.clone(),
341 self.mode,
342 self.title,
343 self.length
344 )
345 }
346 },
347 _ => match self.auto {
348 true => {
349 let sql = format!("`{}` int AUTO_INCREMENT", self.field);
350 format!(
351 "{} comment '{}|{}|{}'",
352 sql.clone(),
353 self.mode,
354 self.title,
355 self.length
356 )
357 }
358 false => {
359 let mut sql = format!("`{}` varchar({}) ", self.field, self.length);
360 sql = sql.clone().to_string();
361 format!(
362 "{} comment '{}|{}|{}'",
363 sql.clone(),
364 self.mode,
365 self.title,
366 self.length
367 )
368 }
369 },
370 }
371 }
372 fn hide(&mut self) -> &mut Self {
373 self.show = false;
374 self
375 }
376
377 fn describe(&mut self, text: &str) -> &mut Self {
378 self.describe = text.to_string();
379 self
380 }
381 fn field(&mut self) -> JsonValue {
382 let mut field = object! {};
383 field.insert("require", JsonValue::from(self.require)).unwrap();
384 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
385 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
386 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
387 field.insert("length", JsonValue::from(self.length)).unwrap();
388 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
389 field.insert("auto", JsonValue::from(self.auto)).unwrap();
390
391 field.insert("show", JsonValue::from(self.show)).unwrap();
392 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
393 field.insert("example", self.example.clone()).unwrap();
394
395 field
396 }
397 fn swagger(&mut self) -> JsonValue {
398 object! {
399 "type": self.mode.clone(),
400 "example": self.example.clone(),
401 }
402 }
403
404 fn example(&mut self, data: JsonValue) -> &mut Self {
405 self.example = data;
406 self
407 }
408}
409
410pub struct Tel {
419 pub require: bool,
420 pub field: String,
421 pub mode: String,
422 pub title: String,
423 pub def: String,
424 pub length: i32,
425
426 pub show: bool,
427 pub describe: String,
428 pub example: JsonValue,
429}
430
431impl Tel {
432 pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
433 Self {
434 require,
435 field: field.to_string(),
436 mode: "tel".to_string(),
437 title: title.to_string(),
438 def: default.to_string(),
439 length: 15,
440
441 show: true,
442 describe: "".to_string(),
443 example: JsonValue::Null,
444 }
445 }
446 pub fn is_phone(input: &str) -> bool {
447 lazy_static! {
449 static ref CN_RE: Regex = Regex::new(r"^1[3-9]\d{9}$").unwrap();
451 static ref INTL_RE: Regex = Regex::new(r"^\+?[1-9]\d{1,14}$").unwrap();
453 static ref INTL_00_RE: Regex = Regex::new(r"^00[1-9]\d{1,14}$").unwrap();
455 }
456 CN_RE.is_match(input) || INTL_RE.is_match(input) || INTL_00_RE.is_match(input)
457 }
458}
459
460impl Field for Tel {
461 fn require(&mut self, require: bool) -> &mut Self {
462 self.require = require;
463 self
464 }
465 fn sql(&mut self, model: &str) -> String {
466 let dot = if model == "pgsql" { "" } else { "`" };
467 let sql = format!(
468 "{dot}{}{dot} varchar({}) default '{}'",
469 self.field, self.length, self.def
470 );
471 match model {
472 "sqlite" => sql,
473 _ => format!(
474 "{} comment '{}|{}|{}|{}|{}'",
475 sql.clone(),
476 self.mode,
477 self.require,
478 self.title,
479 self.length,
480 self.def
481 ),
482 }
483 }
484 fn hide(&mut self) -> &mut Self {
485 self.show = false;
486 self
487 }
488 fn describe(&mut self, text: &str) -> &mut Self {
489 self.describe = text.to_string();
490 self
491 }
492
493 fn field(&mut self) -> JsonValue {
494 let mut field = object! {};
495 field.insert("require", JsonValue::from(self.require)).unwrap();
496 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
497 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
498 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
499 field.insert("length", JsonValue::from(self.length)).unwrap();
500 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
501
502 field.insert("show", JsonValue::from(self.show)).unwrap();
503 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
504 field.insert("example", self.example.clone()).unwrap();
505
506 field
507 }
508
509 fn swagger(&mut self) -> JsonValue {
510 object! {
511 "type": self.mode.clone(),
512 "example": self.example.clone(),
513 }
514 }
515 fn example(&mut self, data: JsonValue) -> &mut Self {
516 self.example = data;
517 self
518 }
519}
520
521pub struct Ident {
530 pub require: bool,
531 pub field: String,
532 pub mode: String,
533 pub title: String,
534 pub def: String,
535 pub length: i32,
536
537 pub show: bool,
538 pub describe: String,
539 pub example: JsonValue,
540}
541
542impl Ident {
543 pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
544 Self {
545 require,
546 field: field.to_string(),
547 mode: "ident".to_string(),
548 title: title.to_string(),
549 def: default.to_string(),
550 length: 18,
551
552 show: true,
553 describe: "".to_string(),
554 example: JsonValue::Null,
555 }
556 }
557}
558
559impl Field for Ident {
560 fn require(&mut self, require: bool) -> &mut Self {
561 self.require = require;
562 self
563 }
564 fn sql(&mut self, model: &str) -> String {
565 let dot = if model == "pgsql" { "" } else { "`" };
566 let sql = format!(
567 "{dot}{}{dot} varchar({}) default '{}'",
568 self.field, self.length, self.def
569 );
570 match model {
571 "sqlite" => sql,
572 _ => format!(
573 "{} comment '{}|{}|{}|{}|{}'",
574 sql.clone(),
575 self.mode,
576 self.require,
577 self.title,
578 self.length,
579 self.def
580 ),
581 }
582 }
583 fn hide(&mut self) -> &mut Self {
584 self.show = false;
585 self
586 }
587 fn describe(&mut self, text: &str) -> &mut Self {
588 self.describe = text.to_string();
589 self
590 }
591
592 fn field(&mut self) -> JsonValue {
593 let mut field = object! {};
594 field.insert("require", JsonValue::from(self.require)).unwrap();
595 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
596 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
597 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
598 field.insert("length", JsonValue::from(self.length)).unwrap();
599 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
600
601 field.insert("show", JsonValue::from(self.show)).unwrap();
602 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
603 field.insert("example", self.example.clone()).unwrap();
604
605 field
606 }
607
608 fn swagger(&mut self) -> JsonValue {
609 object! {
610 "type": self.mode.clone(),
611 "example": self.example.clone(),
612 }
613 }
614 fn example(&mut self, data: JsonValue) -> &mut Self {
615 self.example = data;
616 self
617 }
618}
619pub struct Email {
628 pub require: bool,
629 pub field: String,
630 pub mode: String,
631 pub title: String,
632 pub def: String,
633 pub length: i32,
634
635 pub show: bool,
636 pub describe: String,
637 pub example: JsonValue,
638}
639
640impl Email {
641 pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
642 Self {
643 require,
644 field: field.to_string(),
645 mode: "email".to_string(),
646 title: title.to_string(),
647 def: default.to_string(),
648 length: 100,
649
650 show: true,
651 describe: "".to_string(),
652 example: JsonValue::Null,
653 }
654 }
655 pub fn is_email(email: &str) -> bool {
656 lazy_static! {
658 static ref EMAIL_RE: Regex =
659 Regex::new(r"(?i)^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$").unwrap();
660 }
661 EMAIL_RE.is_match(email)
662 }
663}
664
665impl Field for Email {
666 fn require(&mut self, require: bool) -> &mut Self {
667 self.require = require;
668 self
669 }
670 fn sql(&mut self, model: &str) -> String {
671 let sql = format!("`{}` varchar({}) default '{}'", self.field, self.length, self.def);
672 match model {
673 "sqlite" => sql,
674 "pgsql" => {
675 let sql = format!("{} varchar({}) default '{}'", self.field, self.length, self.def);
676 format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def)
677 }
678 _ => format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def),
679 }
680 }
681 fn hide(&mut self) -> &mut Self {
682 self.show = false;
683 self
684 }
685 fn describe(&mut self, text: &str) -> &mut Self {
686 self.describe = text.to_string();
687 self
688 }
689 fn field(&mut self) -> JsonValue {
690 let mut field = object! {};
691 field.insert("require", JsonValue::from(self.require)).unwrap();
692 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
693 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
694 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
695 field.insert("length", JsonValue::from(self.length)).unwrap();
696 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
697
698 field.insert("show", JsonValue::from(self.show)).unwrap();
699 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
700 field.insert("example", self.example.clone()).unwrap();
701 field
702 }
703
704 fn verify(&mut self, data: JsonValue) -> JsonValue {
705 data
706 }
707
708 fn swagger(&mut self) -> JsonValue {
709 object! {
710 "type": self.mode.clone(),
711 "example": self.example.clone(),
712 }
713 }
714 fn example(&mut self, data: JsonValue) -> &mut Self {
715 self.example = data.clone();
716 self
717 }
718}
719
720pub struct Color {
727 pub require: bool,
728 pub field: String,
729 pub mode: String,
730 pub title: String,
731 pub def: String,
732 pub length: i32,
733
734 pub show: bool,
735 pub describe: String,
736 pub example: JsonValue,
737}
738
739impl Color {
740 pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
741 Self {
742 require,
743 field: field.to_string(),
744 mode: "color".to_string(),
745 title: title.to_string(),
746 def: default.to_string(),
747 length: 9,
748
749 show: true,
750 describe: "".to_string(),
751 example: JsonValue::Null,
752 }
753 }
754}
755
756impl Field for Color {
757 fn require(&mut self, require: bool) -> &mut Self {
758 self.require = require;
759 self
760 }
761 fn sql(&mut self, model: &str) -> String {
762 let dot = if model == "pgsql" { "" } else { "`" };
763 let sql = format!(
764 "{dot}{}{dot} varchar({}) default '{}'",
765 self.field, self.length, self.def
766 );
767 match model {
768 "sqlite" => sql,
769 _ => format!(
770 "{} comment '{}|{}|{}|{}|{}'",
771 sql.clone(),
772 self.mode,
773 self.require,
774 self.title,
775 self.length,
776 self.def
777 ),
778 }
779 }
780 fn hide(&mut self) -> &mut Self {
781 self.show = false;
782 self
783 }
784 fn describe(&mut self, text: &str) -> &mut Self {
785 self.describe = text.to_string();
786 self
787 }
788 fn field(&mut self) -> JsonValue {
789 let mut field = object! {};
790 field.insert("require", JsonValue::from(self.require)).unwrap();
791 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
792 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
793 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
794 field.insert("length", JsonValue::from(self.length)).unwrap();
795 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
796
797 field.insert("show", JsonValue::from(self.show)).unwrap();
798 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
799 field.insert("example", self.example.clone()).unwrap();
800
801 field
802 }
803
804 fn verify(&mut self, data: JsonValue) -> JsonValue {
805 data
806 }
807
808 fn swagger(&mut self) -> JsonValue {
809 object! {
810 "type": self.mode.clone(),
811 "example": self.example.clone(),
812 }
813 }
814 fn example(&mut self, data: JsonValue) -> &mut Self {
815 self.example = data.clone();
816 self
817 }
818}
819
820pub struct Code {
830 pub require: bool,
831 pub field: String,
832 pub mode: String,
833 pub title: String,
834 pub def: String,
835 pub length: usize,
836
837 pub show: bool,
838 pub describe: String,
839
840 pub dec: String,
842 pub example: JsonValue,
843}
844
845impl Code {
846 pub fn new(
847 require: bool,
848 field: &str,
849 title: &str,
850 length: usize,
851 dec: &str,
852 default: &str,
853 ) -> Self {
854 Self {
855 require,
856 field: field.to_string(),
857 mode: "code".to_string(),
858 title: title.to_string(),
859 def: default.to_string(),
860 length: length + dec.len(),
861 show: true,
862 describe: "".to_string(),
863 dec: dec.to_string(),
864 example: JsonValue::Null,
865 }
866 }
867
868 pub fn order_code(prefix: &str, machine_id: u16) -> String {
870 let now = Local::now();
871 let time_str = now.format("%Y%m%d%H%M%S");
872 let millis = now.timestamp_subsec_millis();
873 let seq = SEQ.fetch_add(1, Ordering::SeqCst) % 1000;
875 let rand_part: u16 = rand::rng().random_range(0..1000);
877 format!(
878 "{}{}{:02}{:03}{:03}{:03}",
879 prefix,
880 time_str,
881 machine_id % 100,
882 millis,
883 rand_part,
884 seq
885 )
886 }
887 pub fn code_no_13(prefix: &str) -> String {
889 let data = Datetime::datetime_format("%Y%m%d%H%f");
890 let data = data.trim_end_matches("00");
891 format!("{prefix}{data}")
892 }
893 pub fn batch_no(prefix: &str) -> String {
895 let data = Datetime::datetime_format("%Y%m%d%H");
896 format!("{prefix}{data}")
897 }
898 pub fn random_number_f64(begin: f64, finish: f64) -> f64 {
900 rand::rng().random_range(begin..=finish)
901 }
902 pub fn verification_code(len: usize) -> String {
904 let text = "0123456789";
905 let mut code = "".to_string();
906 for _ in 0..len {
907 let index = rand::rng().random_range(0..=text.len() - 1);
908 let t = &text[index..=index];
909 code = format!("{}{}", code, t);
910 }
911 code
912 }
913
914}
915
916impl Field for Code {
917 fn require(&mut self, require: bool) -> &mut Self {
918 self.require = require;
919 self
920 }
921 fn sql(&mut self, model: &str) -> String {
922 let dot = if model == "pgsql" { "" } else { "`" };
923 let sql = format!(
924 "{dot}{}{dot} varchar({}) default '{}'",
925 self.field, self.length, self.def
926 );
927 match model {
928 "sqlite" => sql,
929 _ => format!(
930 "{} comment '{}|{}|{}|{}|{}|{}'",
931 sql.clone(),
932 self.mode,
933 self.require,
934 self.title,
935 self.length,
936 self.dec,
937 self.def
938 ),
939 }
940 }
941 fn hide(&mut self) -> &mut Self {
942 self.show = false;
943 self
944 }
945 fn describe(&mut self, text: &str) -> &mut Self {
946 self.describe = text.to_string();
947 self
948 }
949
950 fn field(&mut self) -> JsonValue {
951 let mut field = object! {};
952 field.insert("require", JsonValue::from(self.require)).unwrap();
953 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
954 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
955 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
956 field.insert("length", JsonValue::from(self.length)).unwrap();
957 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
958
959 field.insert("show", JsonValue::from(self.show)).unwrap();
960 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
961 field.insert("dec", JsonValue::from(self.dec.clone())).unwrap();
962 field.insert("example", self.example.clone()).unwrap();
963
964 field
965 }
966
967 fn swagger(&mut self) -> JsonValue {
968 object! {
969 "type": self.mode.clone(),
970 "example": self.example.clone(),
971 }
972 }
973 fn example(&mut self, data: JsonValue) -> &mut Self {
974 self.example = data.clone();
975 self
976 }
977}
978
979pub struct BarCode {
981 pub require: bool,
983 pub field: String,
985 pub mode: String,
987 pub title: String,
989 pub def: String,
991 pub length: usize,
993 pub show: bool,
995 pub describe: String,
997 pub example: JsonValue,
998}
999
1000impl BarCode {
1001 pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
1002 Self {
1003 require,
1004 field: field.to_string(),
1005 mode: "barcode".to_string(),
1006 title: title.to_string(),
1007 def: default.to_string(),
1008 length: 20,
1009 show: true,
1010 describe: "".to_string(),
1011 example: JsonValue::Null,
1012 }
1013 }
1014 pub fn create_barcode(prefix: &str) -> String {
1016 let data = Datetime::datetime_format("%Y%m%d%H");
1017 format!("{prefix}{data}")
1018 }
1019}
1020
1021impl Field for BarCode {
1022 fn require(&mut self, require: bool) -> &mut Self {
1023 self.require = require;
1024 self
1025 }
1026 fn sql(&mut self, model: &str) -> String {
1027 let dot = if model == "pgsql" { "" } else { "`" };
1028 let sql = format!(
1029 "{dot}{}{dot} varchar({}) default '{}'",
1030 self.field, self.length, self.def
1031 );
1032 match model {
1033 "sqlite" => sql,
1034 _ => format!(
1035 "{} comment '{}|{}|{}|{}|{}'",
1036 sql.clone(),
1037 self.mode,
1038 self.require,
1039 self.title,
1040 self.length,
1041 self.def
1042 ),
1043 }
1044 }
1045 fn hide(&mut self) -> &mut Self {
1046 self.show = false;
1047 self
1048 }
1049 fn describe(&mut self, text: &str) -> &mut Self {
1050 self.describe = text.to_string();
1051 self
1052 }
1053
1054 fn field(&mut self) -> JsonValue {
1055 let mut field = object! {};
1056 field.insert("require", JsonValue::from(self.require)).unwrap();
1057 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
1058 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
1059 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
1060 field.insert("length", JsonValue::from(self.length)).unwrap();
1061 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
1062
1063 field.insert("show", JsonValue::from(self.show)).unwrap();
1064 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
1065 field.insert("example", self.example.clone()).unwrap();
1066 field
1067 }
1068
1069 fn swagger(&mut self) -> JsonValue {
1070 object! {
1071 "type": self.mode.clone(),
1072 "example": self.example.clone(),
1073 }
1074 }
1075
1076 fn example(&mut self, data: JsonValue) -> &mut Self {
1077 self.example = data.clone();
1078 self
1079 }
1080}
1081
1082pub struct QrCode {
1084 pub require: bool,
1086 pub field: String,
1088 pub mode: String,
1090 pub title: String,
1092 pub def: String,
1094 pub length: usize,
1096 pub show: bool,
1098 pub describe: String,
1100 pub example: JsonValue,
1101}
1102
1103impl QrCode {
1104 pub fn new(require: bool, field: &str, title: &str, length: usize, default: &str) -> Self {
1105 Self {
1106 require,
1107 field: field.to_string(),
1108 mode: "qrcode".to_string(),
1109 title: title.to_string(),
1110 def: default.to_string(),
1111 length,
1112 show: true,
1113 describe: "".to_string(),
1114 example: JsonValue::Null,
1115 }
1116 }
1117}
1118
1119impl Field for QrCode {
1120 fn require(&mut self, require: bool) -> &mut Self {
1121 self.require = require;
1122 self
1123 }
1124 fn sql(&mut self, model: &str) -> String {
1125 let dot = if model == "pgsql" { "" } else { "`" };
1126 let sql = format!(
1127 "{dot}{}{dot} varchar({}) default '{}'",
1128 self.field, self.length, self.def
1129 );
1130 match model {
1131 "sqlite" => sql,
1132 _ => format!(
1133 "{} comment '{}|{}|{}|{}|{}'",
1134 sql.clone(),
1135 self.mode,
1136 self.require,
1137 self.title,
1138 self.length,
1139 self.def
1140 ),
1141 }
1142 }
1143 fn hide(&mut self) -> &mut Self {
1144 self.show = false;
1145 self
1146 }
1147 fn describe(&mut self, text: &str) -> &mut Self {
1148 self.describe = text.to_string();
1149 self
1150 }
1151
1152 fn field(&mut self) -> JsonValue {
1153 let mut field = object! {};
1154 field.insert("require", JsonValue::from(self.require)).unwrap();
1155 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
1156 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
1157 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
1158 field.insert("length", JsonValue::from(self.length)).unwrap();
1159 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
1160
1161 field.insert("show", JsonValue::from(self.show)).unwrap();
1162 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
1163 field.insert("example", self.example.clone()).unwrap();
1164
1165 field
1166 }
1167
1168 fn swagger(&mut self) -> JsonValue {
1169 object! {
1170 "type": self.mode.clone(),
1171 "example": self.example.clone(),
1172 }
1173 }
1174
1175 fn example(&mut self, data: JsonValue) -> &mut Self {
1176 self.example = data.clone();
1177 self
1178 }
1179}
1180#[cfg(test)]
1181mod tests {
1182 #[test]
1183 fn code() {
1184 let data = crate::str::Code::order_code("Y", 1);
1185 println!("code: {} {}", data, data.len());
1186 let data = crate::str::Code::order_code("Y", 1);
1187 println!("code: {} {}", data, data.len());
1188 let data = crate::str::Code::order_code("Y", 2);
1189 println!("code: {} {}", data, data.len());
1190 let data = crate::str::Code::order_code("Y", 2);
1191 println!("code: {} {}", data, data.len());
1192 let data = crate::str::Code::order_code("Y", 3);
1193 println!("code: {} {}", data, data.len());
1194 let data = crate::str::Code::order_code("Y", 3);
1195 println!("code: {} {}", data, data.len());
1196 }
1197}