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