Skip to main content

br_fields/
text.rs

1use crate::Field;
2use json::{object, JsonValue};
3
4/// 长文本
5///
6/// * require 是否必填
7/// * field 字段名
8/// * mode 模式 string
9/// * title 字段描述
10/// * length 附件数量
11/// * def 默认值
12/// * dec 后缀
13pub struct Text {
14    pub require: bool,
15    pub field: String,
16    pub mode: String,
17    pub title: String,
18    pub def: String,
19    pub length: i32,
20    pub show: bool,
21    pub describe: String,
22    pub example: JsonValue,
23}
24
25impl Text {
26    pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
27        Self {
28            require,
29            field: field.to_string(),
30            mode: "text".to_string(),
31            title: title.to_string(),
32            def: default.to_string(),
33            length: 0,
34            show: true,
35            describe: "".to_string(),
36            example: JsonValue::Null,
37        }
38    }
39    /// 最小长度 TEXT 最多 65535 个字符
40    /// 中等长度 MEDIUMTEXT 最多 16777215 个字符
41    /// 最长长度 LONGTEXT 最多 4294967295 个字符
42    pub fn length(mut self, length: i32) -> Self {
43        self.length = length;
44        self
45    }
46}
47
48impl Field for Text {
49    fn sql(&mut self, model: &str) -> String {
50        let not_null = if self.require { " not null" } else { "" };
51        match model {
52            "sqlite" => {
53                format!("{} TEXT{} default '{}'", self.field, not_null, self.def)
54            }
55            "pgsql" => {
56                format!(
57                    r#""{}" TEXT{} default '{}'"#,
58                    self.field, not_null, self.def
59                )
60            }
61            _ => {
62                let sql = match self.length {
63                    1 => format!(
64                        "`{}` MEDIUMTEXT{}",
65                        self.field, not_null
66                    ),
67                    2 => format!(
68                        "`{}` LONGTEXT{}",
69                        self.field, not_null
70                    ),
71                    _ => format!("`{}` text{}", self.field, not_null),
72                };
73                format!(
74                    "{} comment '{}|{}|{}|{}|{}'",
75                    sql.clone(),
76                    self.mode,
77                    self.require,
78                    self.title,
79                    self.length,
80                    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
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
131    fn example(&mut self, data: JsonValue) -> &mut Self {
132        self.example = data.clone();
133        self
134    }
135}
136
137/// 富文本
138///
139/// * require 是否必填
140/// * field 字段名
141/// * mode 模式 string
142/// * title 字段描述
143/// * length 附件数量
144/// * def 默认值
145pub struct Editor {
146    pub require: bool,
147    pub field: String,
148    pub mode: String,
149    pub title: String,
150    pub def: String,
151    pub length: i32,
152    pub show: bool,
153    pub describe: String,
154    pub example: JsonValue,
155}
156
157impl Editor {
158    pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
159        Self {
160            require,
161            field: field.to_string(),
162            mode: "editor".to_string(),
163            title: title.to_string(),
164            def: default.to_string(),
165            length: 0,
166            show: true,
167            describe: "".to_string(),
168            example: JsonValue::Null,
169        }
170    }
171    /// 最小长度 TEXT 最多 65535 个字符
172    /// 中等长度 MEDIUMTEXT 最多 16777215 个字符
173    /// 最长长度 LONGTEXT 最多 4294967295 个字符
174    pub fn length(mut self, length: i32) -> Self {
175        self.length = length;
176        self
177    }
178}
179
180impl Field for Editor {
181    fn sql(&mut self, model: &str) -> String {
182        let not_null = if self.require { " not null" } else { "" };
183        match model {
184            "sqlite" => {
185                format!("{} TEXT{} default '{}'", self.field, not_null, self.def)
186            }
187            "pgsql" => {
188                format!(
189                    r#""{}" TEXT{} default '{}'"#,
190                    self.field, not_null, self.def
191                )
192            }
193            _ => {
194                let sql = match self.length {
195                    1 => format!(
196                        "`{}` MEDIUMTEXT{}",
197                        self.field, not_null
198                    ),
199                    2 => format!(
200                        "`{}` LONGTEXT{}",
201                        self.field, not_null
202                    ),
203                    _ => format!("`{}` text{}", self.field, not_null),
204                };
205                format!(
206                    "{} comment '{}|{}|{}|{}'",
207                    sql.clone(),
208                    self.mode,
209                    self.require,
210                    self.title,
211                    self.length
212                )
213            }
214        }
215    }
216    fn field(&mut self) -> JsonValue {
217        let mut field = object! {};
218        field
219            .insert("require", JsonValue::from(self.require))
220            .unwrap();
221        field
222            .insert("field", JsonValue::from(self.field.clone()))
223            .unwrap();
224        field
225            .insert("mode", JsonValue::from(self.mode.clone()))
226            .unwrap();
227        field
228            .insert("title", JsonValue::from(self.title.clone()))
229            .unwrap();
230        field
231            .insert("length", JsonValue::from(self.length))
232            .unwrap();
233        field
234            .insert("def", JsonValue::from(self.def.clone()))
235            .unwrap();
236
237        field.insert("show", JsonValue::from(self.show)).unwrap();
238        field
239            .insert("describe", JsonValue::from(self.describe.clone()))
240            .unwrap();
241        field.insert("example", self.example.clone()).unwrap();
242        field
243    }
244    fn hide(&mut self) -> &mut Self {
245        self.show = false;
246        self
247    }
248
249    fn describe(&mut self, text: &str) -> &mut Self {
250        self.describe = text.to_string();
251        self
252    }
253    fn swagger(&mut self) -> JsonValue {
254        object! {
255            "type": self.mode.clone(),
256            "example": self.example.clone(),
257        }
258    }
259    fn example(&mut self, data: JsonValue) -> &mut Self {
260        self.example = data.clone();
261        self
262    }
263}
264
265/// JSON
266///
267/// * field 字段名
268/// * mode 模式 json
269/// * title 字段描述
270/// * default 默认值
271pub struct Json {
272    pub require: bool,
273    pub field: String,
274    pub mode: String,
275    pub title: String,
276    pub def: JsonValue,
277    pub length: i32,
278
279    pub show: bool,
280    pub describe: String,
281    pub example: JsonValue,
282}
283
284impl Json {
285    pub fn new(require: bool, field: &str, title: &str, def: JsonValue) -> Self {
286        Self {
287            require,
288            field: field.to_string(),
289            mode: "json".to_string(),
290            title: title.to_string(),
291            def,
292            length: 0,
293            show: true,
294            describe: "".to_string(),
295            example: JsonValue::Null,
296        }
297    }
298    /// 最小长度 TEXT 最多 65535 个字符
299    /// 中等长度 MEDIUMTEXT 最多 16777215 个字符
300    /// 最长长度 LONGTEXT 最多 4294967295 个字符
301    pub fn length(mut self, length: i32) -> Self {
302        self.length = length;
303        self
304    }
305}
306
307impl Field for Json {
308    fn sql(&mut self, model: &str) -> String {
309        let not_null = if self.require { " not null" } else { "" };
310        let def_str = self.def.to_string();
311        match model {
312            "sqlite" => {
313                format!("{} TEXT{} default '{}'", self.field, not_null, def_str)
314            }
315            "pgsql" => {
316                format!(r#""{}" TEXT{} default '{}'"#, self.field, not_null, def_str)
317            }
318            _ => {
319                let sql = match self.length {
320                    1 => format!(
321                        "`{}` MEDIUMTEXT{}",
322                        self.field, not_null
323                    ),
324                    2 => format!(
325                        "`{}` LONGTEXT{}",
326                        self.field, not_null
327                    ),
328                    _ => format!("`{}` text{}", self.field, not_null),
329                };
330                format!(
331                    "{} comment '{}|{}|{}'",
332                    sql.clone(),
333                    self.mode,
334                    self.title,
335                    self.length
336                )
337            }
338        }
339    }
340    fn field(&mut self) -> JsonValue {
341        let mut field = object! {};
342        field
343            .insert("require", JsonValue::from(self.require))
344            .unwrap();
345        field
346            .insert("field", JsonValue::from(self.field.clone()))
347            .unwrap();
348        field
349            .insert("mode", JsonValue::from(self.mode.clone()))
350            .unwrap();
351        field
352            .insert("title", JsonValue::from(self.title.clone()))
353            .unwrap();
354
355        field
356            .insert("length", JsonValue::from(self.length))
357            .unwrap();
358
359        field.insert("def", self.def.clone()).unwrap();
360
361        field.insert("show", JsonValue::from(self.show)).unwrap();
362        field
363            .insert("describe", JsonValue::from(self.describe.clone()))
364            .unwrap();
365        field.insert("example", self.example.clone()).unwrap();
366        field
367    }
368    fn hide(&mut self) -> &mut Self {
369        self.show = false;
370        self
371    }
372
373    fn describe(&mut self, text: &str) -> &mut Self {
374        self.describe = text.to_string();
375        self
376    }
377    fn swagger(&mut self) -> JsonValue {
378        object! {
379            "type": self.mode.clone(),
380            "example": self.example.clone(),
381        }
382    }
383    fn example(&mut self, data: JsonValue) -> &mut Self {
384        self.example = data.clone();
385        self
386    }
387}
388
389/// Array
390///
391/// * field 字段名
392/// * mode 模式 json
393/// * title 字段描述
394/// * default 默认值
395pub struct Array {
396    pub require: bool,
397    pub field: String,
398    pub mode: String,
399    pub title: String,
400    pub def: JsonValue,
401    pub length: i32,
402
403    pub show: bool,
404    pub describe: String,
405    pub items: JsonValue,
406    pub example: JsonValue,
407    pub max_items: i32,
408}
409
410impl Array {
411    pub fn new(require: bool, field: &str, title: &str, def: JsonValue) -> Self {
412        Self {
413            require,
414            field: field.to_string(),
415            mode: "array".to_string(),
416            title: title.to_string(),
417            def,
418            length: 0,
419            show: true,
420            describe: "".to_string(),
421            items: JsonValue::Null,
422            example: JsonValue::Null,
423            max_items: 0,
424        }
425    }
426    /// 最小长度 TEXT 最多 65535 个字符
427    /// 中等长度 MEDIUMTEXT 最多 16777215 个字符
428    /// 最长长度 LONGTEXT 最多 4294967295 个字符
429    pub fn length(mut self, length: i32) -> Self {
430        self.length = length;
431        self
432    }
433    pub fn items(mut self, item: JsonValue) -> Self {
434        self.items = item;
435        self
436    }
437    pub fn max_items(mut self, mun: i32) -> Self {
438        self.max_items = mun;
439        self
440    }
441}
442
443impl Field for Array {
444    fn sql(&mut self, model: &str) -> String {
445        let not_null = if self.require { " not null" } else { "" };
446        let def_str = self.def.to_string();
447        match model {
448            "sqlite" => {
449                format!("{} TEXT{} default '{}'", self.field, not_null, def_str)
450            }
451            "pgsql" => {
452                format!(r#""{}" TEXT{} default '{}'"#, self.field, not_null, def_str)
453            }
454            _ => {
455                let sql = match self.length {
456                    1 => format!(
457                        "`{}` MEDIUMTEXT{}",
458                        self.field, not_null
459                    ),
460                    2 => format!(
461                        "`{}` LONGTEXT{}",
462                        self.field, not_null
463                    ),
464                    _ => format!("`{}` text{}", self.field, not_null),
465                };
466                format!(
467                    "{} comment '{}|{}|{}'",
468                    sql.clone(),
469                    self.mode,
470                    self.title,
471                    self.length
472                )
473            }
474        }
475    }
476    fn hide(&mut self) -> &mut Self {
477        self.show = false;
478        self
479    }
480    fn describe(&mut self, text: &str) -> &mut Self {
481        self.describe = text.to_string();
482        self
483    }
484    fn field(&mut self) -> JsonValue {
485        let mut field = object! {};
486        field
487            .insert("require", JsonValue::from(self.require))
488            .unwrap();
489        field
490            .insert("field", JsonValue::from(self.field.clone()))
491            .unwrap();
492        field
493            .insert("mode", JsonValue::from(self.mode.clone()))
494            .unwrap();
495        field
496            .insert("title", JsonValue::from(self.title.clone()))
497            .unwrap();
498        field
499            .insert("length", JsonValue::from(self.length))
500            .unwrap();
501
502        field.insert("def", self.def.clone()).unwrap();
503
504        field.insert("show", JsonValue::from(self.show)).unwrap();
505        field
506            .insert("describe", JsonValue::from(self.describe.clone()))
507            .unwrap();
508        field.insert("items", self.items.clone()).unwrap();
509        field.insert("example", self.example.clone()).unwrap();
510        field
511            .insert("max_items", JsonValue::from(self.max_items))
512            .unwrap();
513        field
514    }
515
516    fn swagger(&mut self) -> JsonValue {
517        object! {
518            "type": self.mode.clone(),
519            "example": self.example.clone(),
520        }
521    }
522    fn example(&mut self, data: JsonValue) -> &mut Self {
523        self.example = data;
524        self
525    }
526}
527
528/// Object
529#[derive(Debug, Clone)]
530pub struct Object {
531    /// 是否必填
532    pub require: bool,
533    /// 字段名
534    pub field: String,
535    pub mode: String,
536    /// 字段描述
537    pub title: String,
538    /// 默认值
539    pub def: JsonValue,
540    pub length: i32,
541
542    pub show: bool,
543    /// 功能描述
544    pub describe: String,
545    pub example: JsonValue,
546    pub items: JsonValue,
547}
548
549impl Object {
550    pub fn new(require: bool, field: &str, title: &str, def: JsonValue) -> Self {
551        Self {
552            require,
553            field: field.to_string(),
554            mode: "object".to_string(),
555            title: title.to_string(),
556            def,
557            length: 0,
558            show: true,
559            describe: "".to_string(),
560            example: JsonValue::Null,
561            items: JsonValue::Null,
562        }
563    }
564    /// 最小长度 TEXT 最多 65535 个字符
565    /// 中等长度 MEDIUMTEXT 最多 16777215 个字符
566    /// 最长长度 LONGTEXT 最多 4294967295 个字符
567    pub fn length(mut self, length: i32) -> Self {
568        self.length = length;
569        self
570    }
571    pub fn items(mut self, item: JsonValue) -> Self {
572        self.items = item;
573        self
574    }
575}
576
577impl Field for Object {
578    fn sql(&mut self, model: &str) -> String {
579        let not_null = if self.require { " not null" } else { "" };
580        let def_str = self.def.to_string();
581        match model {
582            "sqlite" => {
583                format!("{} TEXT{} default '{}'", self.field, not_null, def_str)
584            }
585            "pgsql" => {
586                format!(r#""{}" TEXT{} default '{}'"#, self.field, not_null, def_str)
587            }
588            _ => {
589                let sql = match self.length {
590                    1 => format!(
591                        "`{}` MEDIUMTEXT{}",
592                        self.field, not_null
593                    ),
594                    2 => format!(
595                        "`{}` LONGTEXT{}",
596                        self.field, not_null
597                    ),
598                    _ => format!("`{}` text{}", self.field, not_null),
599                };
600                format!(
601                    "{} comment '{}|{}|{}'",
602                    sql.clone(),
603                    self.mode,
604                    self.title,
605                    self.length
606                )
607            }
608        }
609    }
610    fn hide(&mut self) -> &mut Self {
611        self.show = false;
612        self
613    }
614
615    fn describe(&mut self, text: &str) -> &mut Self {
616        self.describe = text.to_string();
617        self
618    }
619    fn field(&mut self) -> JsonValue {
620        let mut field = object! {};
621        field
622            .insert("require", JsonValue::from(self.require))
623            .unwrap();
624        field
625            .insert("field", JsonValue::from(self.field.clone()))
626            .unwrap();
627        field
628            .insert("mode", JsonValue::from(self.mode.clone()))
629            .unwrap();
630        field
631            .insert("title", JsonValue::from(self.title.clone()))
632            .unwrap();
633        field
634            .insert("length", JsonValue::from(self.length))
635            .unwrap();
636
637        field.insert("def", self.def.clone()).unwrap();
638
639        field.insert("show", JsonValue::from(self.show)).unwrap();
640        field
641            .insert("describe", JsonValue::from(self.describe.clone()))
642            .unwrap();
643        field.insert("example", self.example.clone()).unwrap();
644        field.insert("items", self.items.clone()).unwrap();
645
646        field
647    }
648
649    fn swagger(&mut self) -> JsonValue {
650        object! {
651            "type": self.mode.clone(),
652            "example": self.example.clone(),
653        }
654    }
655    fn example(&mut self, data: JsonValue) -> &mut Self {
656        self.example = data.clone();
657        self
658    }
659}
660
661/// 网址
662///
663/// * require 是否必填
664/// * field 字段名
665/// * mode 模式 string
666/// * title 字段描述
667/// * length 附件数量
668/// * def 默认值
669pub struct Url {
670    pub require: bool,
671    pub field: String,
672    pub mode: String,
673    pub title: String,
674    pub def: String,
675    pub length: i32,
676    pub show: bool,
677    pub describe: String,
678    pub example: JsonValue,
679}
680
681impl Url {
682    pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
683        Self {
684            require,
685            field: field.to_string(),
686            mode: "url".to_string(),
687            title: title.to_string(),
688            def: default.to_string(),
689            length: 0,
690            show: true,
691            describe: "".to_string(),
692            example: JsonValue::Null,
693        }
694    }
695}
696
697impl Field for Url {
698    fn sql(&mut self, model: &str) -> String {
699        let not_null = if self.require { " not null" } else { "" };
700        match model {
701            "sqlite" => {
702                format!("{} TEXT{} default '{}'", self.field, not_null, self.def)
703            }
704            "pgsql" => {
705                format!(
706                    r#""{}" TEXT{} default '{}'"#,
707                    self.field, not_null, self.def
708                )
709            }
710            _ => {
711                let sql = format!("`{}` text{}", self.field, not_null);
712                format!("{} comment '{}|{}'", sql.clone(), self.mode, self.title)
713            }
714        }
715    }
716    fn hide(&mut self) -> &mut Self {
717        self.show = false;
718        self
719    }
720    fn describe(&mut self, text: &str) -> &mut Self {
721        self.describe = text.to_string();
722        self
723    }
724
725    fn field(&mut self) -> JsonValue {
726        let mut field = object! {};
727        field
728            .insert("require", JsonValue::from(self.require))
729            .unwrap();
730        field
731            .insert("field", JsonValue::from(self.field.clone()))
732            .unwrap();
733        field
734            .insert("mode", JsonValue::from(self.mode.clone()))
735            .unwrap();
736        field
737            .insert("title", JsonValue::from(self.title.clone()))
738            .unwrap();
739        field
740            .insert("length", JsonValue::from(self.length))
741            .unwrap();
742        field
743            .insert("def", JsonValue::from(self.def.clone()))
744            .unwrap();
745
746        field.insert("show", JsonValue::from(self.show)).unwrap();
747        field
748            .insert("describe", JsonValue::from(self.describe.clone()))
749            .unwrap();
750        field.insert("example", self.example.clone()).unwrap();
751        field
752    }
753
754    fn swagger(&mut self) -> JsonValue {
755        object! {
756            "type": self.mode.clone(),
757            "example": self.example.clone(),
758        }
759    }
760
761    fn example(&mut self, data: JsonValue) -> &mut Self {
762        self.example = data.clone();
763        self
764    }
765}