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