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