1use json::{JsonValue, object};
2use crate::{Field};
3
4pub 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
63pub 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
120pub 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 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
177pub 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
232pub 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
290pub 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}