df_fields/
str.rs

1use json::{JsonValue, object};
2use crate::{Field};
3
4/// 字符串
5///
6/// * require 是否必填
7/// * field 字段名
8/// * mode 模式 string
9/// * title 字段描述
10/// * length 字段总长度(含小数位)
11/// * def 默认值
12/// * dec 后缀内容
13pub struct Str {
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 dec: String,
21}
22
23impl Str {
24    pub fn new(require: bool, field: &str, title: &str, length: i32, default: &str) -> Str {
25        Self {
26            require,
27            field: field.to_string(),
28            mode: "string".to_string(),
29            title: title.to_string(),
30            def: default.to_string(),
31            length,
32            dec: "".to_string(),
33        }
34    }
35}
36
37impl Field for Str {
38    fn sql(&mut self, model: &str) -> String {
39        let mut sql = format!("{} varchar({})", self.field, self.length);
40        if self.require {
41            sql = format!("{} not null", sql.clone())
42        };
43        sql = format!("{} default '{}'", sql.clone(), self.def);
44        match model {
45            "sqlite" => sql,
46            _ => format!("{} comment '{}|{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def, self.dec)
47        }
48    }
49    fn field(&mut self) -> JsonValue {
50        let mut field = object! {};
51        field.insert("require", JsonValue::from(self.require.clone())).unwrap();
52        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
53        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
54        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
55        field.insert("length", JsonValue::from(self.length.clone())).unwrap();
56        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
57        field.insert("dec", JsonValue::from(self.dec.clone())).unwrap();
58        field
59    }
60}
61
62
63/// 密码
64///
65/// * field 字段名
66/// * mode 模式 pass
67/// * title 字段描述
68/// * length 字段总长度(含小数位)
69/// * default 默认值
70/// * empty 是否可空
71pub struct Pass {
72    pub require: bool,
73    pub field: String,
74    pub mode: String,
75    pub title: String,
76    pub def: String,
77    pub length: i32,
78    pub dec: String,
79}
80
81impl Pass {
82    pub fn new(require: bool, field: &str, title: &str, length: i32, default: &str) -> Self {
83        Self {
84            require,
85            field: field.to_string(),
86            mode: "pass".to_string(),
87            title: title.to_string(),
88            def: default.to_string(),
89            length,
90            dec: "".to_string(),
91        }
92    }
93}
94
95impl Field for Pass {
96    fn sql(&mut self, model: &str) -> String {
97        let mut sql = format!("{} varchar({})", self.field, self.length);
98        if self.require {
99            sql = format!("{} not null", sql.clone())
100        };
101        sql = format!("{} default '{}'", sql.clone(), self.def);
102        match model {
103            "sqlite" => sql,
104            _ => format!("{} comment '{}|{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def, self.dec)
105        }
106    }
107    fn field(&mut self) -> JsonValue {
108        let mut field = object! {};
109        field.insert("require", JsonValue::from(self.require.clone())).unwrap();
110        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
111        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
112        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
113        field.insert("length", JsonValue::from(self.length.clone())).unwrap();
114        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
115        field.insert("dec", JsonValue::from(self.dec.clone())).unwrap();
116        field
117    }
118}
119
120/// 自定义主键
121///
122/// * field 字段名
123/// * mode 模式
124/// * title 字段描述
125/// * length 字段总长度(含小数位)
126/// * def 默认值
127pub struct Key {
128    pub require: bool,
129    pub field: String,
130    pub mode: String,
131    pub title: String,
132    pub def: String,
133    pub length: i32,
134    pub dec: String,
135}
136
137impl Key {
138    pub fn new(require: bool, field: &str, title: &str, length: i32) -> Self {
139        Self {
140            require,
141            field: field.to_string(),
142            mode: "key".to_string(),
143            title: title.to_string(),
144            def: "".to_string(),
145            length,
146            dec: "".to_string(),
147        }
148    }
149}
150
151impl Field for Key {
152    fn sql(&mut self, model: &str) -> String {
153        let mut sql = "".to_string();
154        sql = format!("{}{}  varchar({})", sql.clone(), self.field, self.length);
155        sql = format!("{}", sql.clone());
156        //PRIMARY KEY
157        match model {
158            "sqlite" => sql,
159            _ => format!("{} comment '{}|{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def, self.dec)
160        }
161    }
162
163    fn field(&mut self) -> JsonValue {
164        let mut field = object! {};
165        field.insert("require", JsonValue::from(self.require.clone())).unwrap();
166        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
167        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
168        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
169        field.insert("length", JsonValue::from(self.length.clone())).unwrap();
170        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
171        field.insert("dec", JsonValue::from(self.dec.clone())).unwrap();
172        field
173    }
174}
175
176
177/// 电话
178///
179/// * field 字段名
180/// * mode 模式 pass
181/// * title 字段描述
182/// * length 字段总长度(含小数位)
183/// * default 默认值
184/// * empty 是否可空
185pub struct Tel {
186    pub require: bool,
187    pub field: String,
188    pub mode: String,
189    pub title: String,
190    pub def: String,
191    pub length: i32,
192}
193
194impl Tel {
195    pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
196        Self {
197            require,
198            field: field.to_string(),
199            mode: "tel".to_string(),
200            title: title.to_string(),
201            def: default.to_string(),
202            length: 15,
203        }
204    }
205}
206
207impl Field for Tel {
208    fn sql(&mut self, model: &str) -> String {
209        let mut sql = format!("{} varchar({})", self.field, self.length);
210        if self.require {
211            sql = format!("{} not null", sql.clone())
212        };
213        sql = format!("{} default '{}'", sql.clone(), self.def);
214        match model {
215            "sqlite" => sql,
216            _ => format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def)
217        }
218    }
219    fn field(&mut self) -> JsonValue {
220        let mut field = object! {};
221        field.insert("require", JsonValue::from(self.require.clone())).unwrap();
222        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
223        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
224        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
225        field.insert("length", JsonValue::from(self.length.clone())).unwrap();
226        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
227        field
228    }
229}
230
231
232/// 电子邮件
233///
234/// * field 字段名
235/// * mode 模式 pass
236/// * title 字段描述
237/// * length 字段总长度(含小数位)
238/// * default 默认值
239/// * empty 是否可空
240pub struct Email {
241    pub require: bool,
242    pub field: String,
243    pub mode: String,
244    pub title: String,
245    pub def: String,
246    pub length: i32,
247}
248
249impl Email {
250    pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
251        Self {
252            require,
253            field: field.to_string(),
254            mode: "email".to_string(),
255            title: title.to_string(),
256            def: default.to_string(),
257            length: 50,
258        }
259    }
260}
261
262impl Field for Email {
263    fn sql(&mut self, model: &str) -> String {
264        let mut sql = format!("{} varchar({})", self.field, self.length);
265        if self.require {
266            sql = format!("{} not null", sql.clone())
267        };
268        sql = format!("{} default '{}'", sql.clone(), self.def);
269        match model {
270            "sqlite" => sql,
271            _ => format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def)
272        }
273    }
274    fn field(&mut self) -> JsonValue {
275        let mut field = object! {};
276        field.insert("require", JsonValue::from(self.require.clone())).unwrap();
277        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
278        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
279        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
280        field.insert("length", JsonValue::from(self.length.clone())).unwrap();
281        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
282        field
283    }
284    fn verify(&mut self, data: JsonValue) -> JsonValue {
285        data
286    }
287}
288
289
290/// 颜色
291///
292/// * field 字段名
293/// * mode 模式 pass
294/// * title 字段描述
295/// * length 字段总长度(含小数位)
296/// * default 默认值
297/// * empty 是否可空
298
299pub struct Color {
300    pub require: bool,
301    pub field: String,
302    pub mode: String,
303    pub title: String,
304    pub def: String,
305    pub length: i32,
306}
307
308impl Color {
309    pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
310        Self {
311            require,
312            field: field.to_string(),
313            mode: "color".to_string(),
314            title: title.to_string(),
315            def: default.to_string(),
316            length: 9,
317        }
318    }
319}
320
321impl Field for Color {
322    fn sql(&mut self, model: &str) -> String {
323        let mut sql = format!("{} varchar({})", self.field, self.length);
324        if self.require {
325            sql = format!("{} not null", sql.clone())
326        };
327        sql = format!("{} default '{}'", sql.clone(), self.def);
328        match model {
329            "sqlite" => sql,
330            _ => format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def)
331        }
332    }
333    fn field(&mut self) -> JsonValue {
334        let mut field = object! {};
335        field.insert("require", JsonValue::from(self.require.clone())).unwrap();
336        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
337        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
338        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
339        field.insert("length", JsonValue::from(self.length.clone())).unwrap();
340        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
341        field
342    }
343    fn verify(&mut self, data: JsonValue) -> JsonValue {
344        data
345    }
346}