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        match model {
51            "sqlite" => {
52                format!("`{}` TEXT default '{}' ", self.field, self.def)
53            }
54            "pgsql" => {
55                let sql =  format!(r#""{}" TEXT"#, self.field);
56                format!(
57                    "{} comment '{}|{}|{}|{}|{}'",
58                    sql.clone(),
59                    self.mode,
60                    self.require,
61                    self.title,
62                    self.length,
63                    self.def
64                )
65            }
66            _ => {
67                let sql = match self.length {
68                    1 => format!("`{}` MEDIUMTEXT ", self.field),
69                    2 => format!("`{}` LONGTEXT ", self.field),
70                    _ => format!("`{}` text ", self.field),
71                };
72                format!(
73                    "{} comment '{}|{}|{}|{}|{}'",
74                    sql.clone(),
75                    self.mode,
76                    self.require,
77                    self.title,
78                    self.length,
79                    self.def
80                )
81            }
82        }
83    }
84    fn hide(&mut self) -> &mut Self {
85        self.show = false;
86        self
87    }
88    fn describe(&mut self, text: &str) -> &mut Self {
89        self.describe = text.to_string();
90        self
91    }
92
93    fn field(&mut self) -> JsonValue {
94        let mut field = object! {};
95        field.insert("require", JsonValue::from(self.require)).unwrap();
96        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
97        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
98        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
99        field.insert("length", JsonValue::from(self.length)).unwrap();
100        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
101
102        field.insert("show", JsonValue::from(self.show)).unwrap();
103        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
104        field.insert("example", self.example.clone()).unwrap();
105
106        field
107    }
108
109    fn swagger(&mut self) -> JsonValue {
110        object! {
111            "type": self.mode.clone(),
112            "example": self.example.clone(),
113        }
114    }
115
116    fn example(&mut self, data: JsonValue) -> &mut Self {
117        self.example = data.clone();
118        self
119    }
120}
121
122/// 富文本
123///
124/// * require 是否必填
125/// * field 字段名
126/// * mode 模式 string
127/// * title 字段描述
128/// * length 附件数量
129/// * def 默认值
130pub struct Editor {
131    pub require: bool,
132    pub field: String,
133    pub mode: String,
134    pub title: String,
135    pub def: String,
136    pub length: i32,
137    pub show: bool,
138    pub describe: String,
139    pub example: JsonValue,
140}
141
142impl Editor {
143    pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
144        Self {
145            require,
146            field: field.to_string(),
147            mode: "editor".to_string(),
148            title: title.to_string(),
149            def: default.to_string(),
150            length: 0,
151            show: true,
152            describe: "".to_string(),
153            example: JsonValue::Null,
154        }
155    }
156    /// 最小长度 TEXT 最多 65535 个字符
157    /// 中等长度 MEDIUMTEXT 最多 16777215 个字符
158    /// 最长长度 LONGTEXT 最多 4294967295 个字符
159    pub fn length(mut self, length: i32) -> Self {
160        self.length = length;
161        self
162    }
163}
164
165impl Field for Editor {
166    fn sql(&mut self, model: &str) -> String {
167        match model {
168            "sqlite" => {
169                format!("`{}` TEXT default '{}' ", self.field, self.def)
170            }
171            "pgsql" => {
172                let sql =  format!(r#""{}" TEXT"#, self.field);
173                format!(
174                    "{} comment '{}|{}|{}|{}'",
175                    sql.clone(),
176                    self.mode,
177                    self.require,
178                    self.title,
179                    self.length
180                )
181            }
182            _ => {
183                let sql = match self.length {
184                    1 => format!("`{}` MEDIUMTEXT ", self.field),
185                    2 => format!("`{}` LONGTEXT ", self.field),
186                    _ => format!("`{}` text ", self.field),
187                };
188                format!(
189                    "{} comment '{}|{}|{}|{}'",
190                    sql.clone(),
191                    self.mode,
192                    self.require,
193                    self.title,
194                    self.length
195                )
196            }
197        }
198    }
199    fn field(&mut self) -> JsonValue {
200        let mut field = object! {};
201        field.insert("require", JsonValue::from(self.require)).unwrap();
202        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
203        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
204        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
205        field.insert("length", JsonValue::from(self.length)).unwrap();
206        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
207
208        field.insert("show", JsonValue::from(self.show)).unwrap();
209        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
210        field.insert("example", self.example.clone()).unwrap();
211        field
212    }
213    fn hide(&mut self) -> &mut Self {
214        self.show = false;
215        self
216    }
217
218    fn describe(&mut self, text: &str) -> &mut Self {
219        self.describe = text.to_string();
220        self
221    }
222    fn swagger(&mut self) -> JsonValue {
223        object! {
224            "type": self.mode.clone(),
225            "example": self.example.clone(),
226        }
227    }
228    fn example(&mut self, data: JsonValue) -> &mut Self {
229        self.example = data.clone();
230        self
231    }
232}
233
234/// JSON
235///
236/// * field 字段名
237/// * mode 模式 json
238/// * title 字段描述
239/// * default 默认值
240pub struct Json {
241    pub require: bool,
242    pub field: String,
243    pub mode: String,
244    pub title: String,
245    pub def: JsonValue,
246    pub length: i32,
247
248    pub show: bool,
249    pub describe: String,
250    pub example: JsonValue,
251}
252
253impl Json {
254    pub fn new(require: bool, field: &str, title: &str, def: JsonValue) -> Self {
255        Self {
256            require,
257            field: field.to_string(),
258            mode: "json".to_string(),
259            title: title.to_string(),
260            def,
261            length: 0,
262            show: true,
263            describe: "".to_string(),
264            example: JsonValue::Null,
265        }
266    }
267    /// 最小长度 TEXT 最多 65535 个字符
268    /// 中等长度 MEDIUMTEXT 最多 16777215 个字符
269    /// 最长长度 LONGTEXT 最多 4294967295 个字符
270    pub fn length(mut self, length: i32) -> Self {
271        self.length = length;
272        self
273    }
274}
275
276impl Field for Json {
277    fn sql(&mut self, model: &str) -> String {
278        match model {
279            "sqlite" => {
280                format!("`{}` TEXT default '{}' ", self.field, self.def)
281            }
282            "pgsql" => {
283                let sql =  format!(r#""{}" TEXT"#, self.field);
284                format!(
285                    "{} comment '{}|{}|{}'",
286                    sql.clone(),
287                    self.mode,
288                    self.title,
289                    self.length
290                )
291            }
292            _ => {
293                let sql = match self.length {
294                    1 => format!("`{}` MEDIUMTEXT ", self.field),
295                    2 => format!("`{}` LONGTEXT ", self.field),
296                    _ => format!("`{}` text ", self.field),
297                };
298                format!(
299                    "{} comment '{}|{}|{}'",
300                    sql.clone(),
301                    self.mode,
302                    self.title,
303                    self.length
304                )
305            }
306        }
307    }
308    fn field(&mut self) -> JsonValue {
309        let mut field = object! {};
310        field.insert("require", JsonValue::from(self.require)).unwrap();
311        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
312        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
313        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
314
315        field.insert("length", JsonValue::from(self.length)).unwrap();
316
317        field.insert("def", self.def.clone()).unwrap();
318
319        field.insert("show", JsonValue::from(self.show)).unwrap();
320        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
321        field.insert("example", self.example.clone()).unwrap();
322        field
323    }
324    fn hide(&mut self) -> &mut Self {
325        self.show = false;
326        self
327    }
328
329    fn describe(&mut self, text: &str) -> &mut Self {
330        self.describe = text.to_string();
331        self
332    }
333    fn swagger(&mut self) -> JsonValue {
334        object! {
335            "type": self.mode.clone(),
336            "example": self.example.clone(),
337        }
338    }
339    fn example(&mut self, data: JsonValue) -> &mut Self {
340        self.example = data.clone();
341        self
342    }
343}
344
345/// Array
346///
347/// * field 字段名
348/// * mode 模式 json
349/// * title 字段描述
350/// * default 默认值
351pub struct Array {
352    pub require: bool,
353    pub field: String,
354    pub mode: String,
355    pub title: String,
356    pub def: JsonValue,
357    pub length: i32,
358
359    pub show: bool,
360    pub describe: String,
361    pub items: JsonValue,
362    pub example: JsonValue,
363    pub max_items: i32,
364}
365
366impl Array {
367    pub fn new(require: bool, field: &str, title: &str, def: JsonValue) -> Self {
368        Self {
369            require,
370            field: field.to_string(),
371            mode: "array".to_string(),
372            title: title.to_string(),
373            def,
374            length: 0,
375            show: true,
376            describe: "".to_string(),
377            items: JsonValue::Null,
378            example: JsonValue::Null,
379            max_items: 0,
380        }
381    }
382    /// 最小长度 TEXT 最多 65535 个字符
383    /// 中等长度 MEDIUMTEXT 最多 16777215 个字符
384    /// 最长长度 LONGTEXT 最多 4294967295 个字符
385    pub fn length(mut self, length: i32) -> Self {
386        self.length = length;
387        self
388    }
389    pub fn items(mut self, item: JsonValue) -> Self {
390        self.items = item;
391        self
392    }
393    pub fn max_items(mut self, mun: i32) -> Self {
394        self.max_items = mun;
395        self
396    }
397}
398
399impl Field for Array {
400    fn sql(&mut self, model: &str) -> String {
401        match model {
402            "sqlite" => {
403                format!("`{}` TEXT default '{}' ", self.field, self.def)
404            }
405            "pgsql" => {
406                let sql = format!("{} TEXT ", self.field);
407                format!(
408                    "{} comment '{}|{}|{}'",
409                    sql.clone(),
410                    self.mode,
411                    self.title,
412                    self.length
413                )
414            }
415            _ => {
416                let sql = match self.length {
417                    1 => format!("`{}` MEDIUMTEXT ", self.field),
418                    2 => format!("`{}` LONGTEXT ", self.field),
419                    _ => format!("`{}` text ", self.field),
420                };
421                format!(
422                    "{} comment '{}|{}|{}'",
423                    sql.clone(),
424                    self.mode,
425                    self.title,
426                    self.length
427                )
428            }
429        }
430    }
431    fn hide(&mut self) -> &mut Self {
432        self.show = false;
433        self
434    }
435    fn describe(&mut self, text: &str) -> &mut Self {
436        self.describe = text.to_string();
437        self
438    }
439    fn field(&mut self) -> JsonValue {
440        let mut field = object! {};
441        field.insert("require", JsonValue::from(self.require)).unwrap();
442        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
443        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
444        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
445        field.insert("length", JsonValue::from(self.length)).unwrap();
446
447        field.insert("def", self.def.clone()).unwrap();
448
449        field.insert("show", JsonValue::from(self.show)).unwrap();
450        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
451        field.insert("items", self.items.clone()).unwrap();
452        field.insert("example", self.example.clone()).unwrap();
453        field.insert("max_items", JsonValue::from(self.max_items)).unwrap();
454        field
455    }
456
457    fn swagger(&mut self) -> JsonValue {
458        object! {
459            "type": self.mode.clone(),
460            "example": self.example.clone(),
461        }
462    }
463    fn example(&mut self, data: JsonValue) -> &mut Self {
464        self.example = data;
465        self
466    }
467}
468
469/// Object
470pub struct Object {
471    /// 是否必填
472    pub require: bool,
473    /// 字段名
474    pub field: String,
475    pub mode: String,
476    /// 字段描述
477    pub title: String,
478    /// 默认值
479    pub def: JsonValue,
480    pub length: i32,
481
482    pub show: bool,
483    /// 功能描述
484    pub describe: String,
485    pub example: JsonValue,
486    pub items: JsonValue,
487}
488
489impl Object {
490    pub fn new(require: bool, field: &str, title: &str, def: JsonValue) -> Self {
491        Self {
492            require,
493            field: field.to_string(),
494            mode: "object".to_string(),
495            title: title.to_string(),
496            def,
497            length: 0,
498            show: true,
499            describe: "".to_string(),
500            example: JsonValue::Null,
501            items: JsonValue::Null,
502        }
503    }
504    /// 最小长度 TEXT 最多 65535 个字符
505    /// 中等长度 MEDIUMTEXT 最多 16777215 个字符
506    /// 最长长度 LONGTEXT 最多 4294967295 个字符
507    pub fn length(mut self, length: i32) -> Self {
508        self.length = length;
509        self
510    }
511    pub fn items(mut self, item: JsonValue) -> Self {
512        self.items = item;
513        self
514    }
515}
516
517impl Field for Object {
518    fn sql(&mut self, model: &str) -> String {
519        match model {
520            "sqlite" => {
521                format!("`{}` TEXT default '{}' ", self.field, self.def)
522            }
523            "pgsql" => {
524                let sql = format!("{} TEXT ", self.field);
525                format!("{} comment '{}|{}|{}'", sql.clone(), self.mode, self.title, self.require)
526            }
527            _ => {
528                let sql = match self.length {
529                    1 => format!("`{}` MEDIUMTEXT ", self.field),
530                    2 => format!("`{}` LONGTEXT ", self.field),
531                    _ => format!("`{}` text ", self.field),
532                };
533                format!("{} comment '{}|{}|{}'", sql.clone(), self.mode, self.title, self.length)
534            }
535        }
536    }
537    fn hide(&mut self) -> &mut Self {
538        self.show = false;
539        self
540    }
541
542    fn describe(&mut self, text: &str) -> &mut Self {
543        self.describe = text.to_string();
544        self
545    }
546    fn field(&mut self) -> JsonValue {
547        let mut field = object! {};
548        field.insert("require", JsonValue::from(self.require)).unwrap();
549        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
550        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
551        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
552        field.insert("length", JsonValue::from(self.length)).unwrap();
553
554        field.insert("def", self.def.clone()).unwrap();
555
556        field.insert("show", JsonValue::from(self.show)).unwrap();
557        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
558        field.insert("example", self.example.clone()).unwrap();
559        field.insert("items", self.items.clone()).unwrap();
560
561        field
562    }
563
564    fn swagger(&mut self) -> JsonValue {
565        object! {
566            "type": self.mode.clone(),
567            "example": self.example.clone(),
568        }
569    }
570    fn example(&mut self, data: JsonValue) -> &mut Self {
571        self.example = data.clone();
572        self
573    }
574}
575
576/// 网址
577///
578/// * require 是否必填
579/// * field 字段名
580/// * mode 模式 string
581/// * title 字段描述
582/// * length 附件数量
583/// * def 默认值
584pub struct Url {
585    pub require: bool,
586    pub field: String,
587    pub mode: String,
588    pub title: String,
589    pub def: String,
590    pub length: i32,
591    pub show: bool,
592    pub describe: String,
593    pub example: JsonValue,
594}
595
596impl Url {
597    pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
598        Self {
599            require,
600            field: field.to_string(),
601            mode: "url".to_string(),
602            title: title.to_string(),
603            def: default.to_string(),
604            length: 0,
605            show: true,
606            describe: "".to_string(),
607            example: JsonValue::Null,
608        }
609    }
610}
611
612impl Field for Url {
613    fn sql(&mut self, model: &str) -> String {
614        match model {
615            "sqlite" => {
616                format!("`{}` TEXT default '{}' ", self.field, self.def)
617            }
618            _ => {
619                let sql = format!("`{}` text ", self.field);
620                format!("{} comment '{}|{}'", sql.clone(), self.mode, self.title)
621            }
622        }
623    }
624    fn hide(&mut self) -> &mut Self {
625        self.show = false;
626        self
627    }
628    fn describe(&mut self, text: &str) -> &mut Self {
629        self.describe = text.to_string();
630        self
631    }
632
633    fn field(&mut self) -> JsonValue {
634        let mut field = object! {};
635        field.insert("require", JsonValue::from(self.require)).unwrap();
636        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
637        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
638        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
639        field.insert("length", JsonValue::from(self.length)).unwrap();
640        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
641
642        field.insert("show", JsonValue::from(self.show)).unwrap();
643        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
644        field.insert("example", self.example.clone()).unwrap();
645        field
646    }
647
648    fn swagger(&mut self) -> JsonValue {
649        object! {
650            "type": self.mode.clone(),
651            "example": self.example.clone(),
652        }
653    }
654
655    fn example(&mut self, data: JsonValue) -> &mut Self {
656        self.example = data.clone();
657        self
658    }
659}