1use crate::Field;
2use json::{object, JsonValue};
3
4#[derive(Debug, Clone)]
13pub struct Location {
14 pub require: bool,
15 pub field: String,
16 pub mode: String,
17 pub title: String,
18 pub length: i32,
19 pub show: bool,
20 pub describe: String,
21 pub def: String,
22 pub example: JsonValue,
23}
24
25impl Location {
26 pub fn new(require: bool, field: &str, title: &str) -> Self {
27 Self {
28 require,
29 field: field.to_string(),
30 mode: "location".to_string(),
31 title: title.to_string(),
32 length: 0,
33 def: "".to_string(),
34 show: true,
35 describe: "".to_string(),
36 example: JsonValue::Null,
37 }
38 }
39 pub fn def(&mut self, def: &str) -> &mut Self {
40 self.def = def.to_string();
41 self
42 }
43 #[allow(dead_code)]
44 fn length(&mut self, code: i32) -> &mut Self {
54 self.length = code;
55 self
56 }
57}
58
59impl Field for Location {
60 fn sql(&mut self, model: &str) -> String {
61 let not_null = if self.require { " not null" } else { "" };
62 match model {
63 "sqlite" => {
64 let length = 39;
65 format!(
66 "`{}` varchar({}){} default '{}'",
67 self.field, length, not_null, self.def
68 )
69 }
70 "pgsql" => {
71 let length = 39;
72 let sql = format!(
73 r#""{}" varchar({}) default '{}'"#,
74 self.field, length, self.def
75 );
76 format!(
77 "{} --{}|{}|{}|{}|{}",
78 sql, self.title, self.mode, self.require, self.def, self.length
79 )
80 }
81 "mysql" => {
82 let sql = format!("`{}` POINT SRID {}{}", self.field, self.length, not_null);
83 format!(
84 "{} comment '{}|{}|{}|{}|{}'",
85 sql, self.title, self.mode, self.require, self.def, self.length
86 )
87 }
88 _ => {
89 let sql = format!(
90 "`{}` varchar({}){} default '{}'",
91 self.field, self.length, not_null, self.def
92 );
93 format!(
94 "{} comment '{}|{}|{}|{}|{}'",
95 sql, self.title, self.mode, self.require, self.def, self.length
96 )
97 }
98 }
99 }
100 fn hide(&mut self) -> &mut Self {
101 self.show = false;
102 self
103 }
104 fn describe(&mut self, text: &str) -> &mut Self {
105 self.describe = text.to_string();
106 self
107 }
108
109 fn field(&mut self) -> JsonValue {
110 let mut field = object! {};
111 field
112 .insert("require", JsonValue::from(self.require))
113 .unwrap();
114 field
115 .insert("field", JsonValue::from(self.field.clone()))
116 .unwrap();
117 field
118 .insert("mode", JsonValue::from(self.mode.clone()))
119 .unwrap();
120 field
121 .insert("title", JsonValue::from(self.title.clone()))
122 .unwrap();
123 field
124 .insert("length", JsonValue::from(self.length))
125 .unwrap();
126 field.insert("show", JsonValue::from(self.show)).unwrap();
127 field
128 .insert("describe", JsonValue::from(self.describe.clone()))
129 .unwrap();
130 field
131 .insert("def", JsonValue::from(self.def.clone()))
132 .unwrap();
133 field.insert("example", self.example.clone()).unwrap();
134 field
135 }
136
137 fn swagger(&mut self) -> JsonValue {
138 object! {
139 "type": self.mode.clone(),
140 "example": self.example.clone(),
141 }
142 }
143
144 fn example(&mut self, data: JsonValue) -> &mut Self {
145 self.example = data.clone();
146 self
147 }
148}
149
150#[derive(Debug, Clone)]
159pub struct Address {
160 pub require: bool,
161 pub field: String,
162 pub mode: String,
163 pub title: String,
164 pub length: usize,
165 pub show: bool,
166 pub describe: String,
167 pub def: String,
168 pub example: JsonValue,
169}
170
171impl Address {
172 pub fn new(require: bool, field: &str, title: &str, length: usize, def: &str) -> Self {
173 Self {
174 require,
175 field: field.to_string(),
176 mode: "address".to_string(),
177 title: title.to_string(),
178 length,
179 def: def.to_string(),
180 show: true,
181 describe: "".to_string(),
182 example: JsonValue::Null,
183 }
184 }
185}
186
187impl Field for Address {
188 fn sql(&mut self, model: &str) -> String {
189 let not_null = if self.require { " not null" } else { "" };
190 match model {
191 "sqlite" => {
192 format!(
193 "`{}` varchar({}){} default '{}'",
194 self.field, self.length, not_null, self.def
195 )
196 }
197 "pgsql" => {
198 let sql = format!(
199 r#""{}" varchar({}) default '{}' "#,
200 self.field, self.length, self.def
201 );
202 format!(
203 "{} --{}|{}|{}|{}|{}",
204 sql, self.title, self.mode, self.require, self.def, self.length
205 )
206 }
207 _ => {
208 let sql = format!(
209 "`{}` varchar({}){} default '{}'",
210 self.field, self.length, not_null, self.def
211 );
212 format!(
213 "{} comment '{}|{}|{}|{}|{}'",
214 sql, self.title, self.mode, self.require, self.def, self.length
215 )
216 }
217 }
218 }
219 fn hide(&mut self) -> &mut Self {
220 self.show = false;
221 self
222 }
223 fn describe(&mut self, text: &str) -> &mut Self {
224 self.describe = text.to_string();
225 self
226 }
227
228 fn field(&mut self) -> JsonValue {
229 let mut field = object! {};
230 field
231 .insert("require", JsonValue::from(self.require))
232 .unwrap();
233 field
234 .insert("field", JsonValue::from(self.field.clone()))
235 .unwrap();
236 field
237 .insert("mode", JsonValue::from(self.mode.clone()))
238 .unwrap();
239 field
240 .insert("title", JsonValue::from(self.title.clone()))
241 .unwrap();
242 field
243 .insert("length", JsonValue::from(self.length))
244 .unwrap();
245 field.insert("show", JsonValue::from(self.show)).unwrap();
246 field
247 .insert("describe", JsonValue::from(self.describe.clone()))
248 .unwrap();
249 field
250 .insert("def", JsonValue::from(self.def.clone()))
251 .unwrap();
252 field.insert("example", self.example.clone()).unwrap();
253 field
254 }
255
256 fn swagger(&mut self) -> JsonValue {
257 object! {
258 "type": self.mode.clone(),
259 "example": self.example.clone(),
260 }
261 }
262
263 fn example(&mut self, data: JsonValue) -> &mut Self {
264 self.example = data.clone();
265 self
266 }
267}
268
269#[derive(Debug, Clone)]
277pub struct LocationAddress {
278 pub require: bool,
279 pub field: String,
280 pub mode: String,
281 pub title: String,
282 pub length: i32,
283 pub show: bool,
284 pub describe: String,
285 pub def: JsonValue,
286 pub example: JsonValue,
287}
288
289impl LocationAddress {
290 pub fn new(require: bool, field: &str, title: &str, def: JsonValue) -> Self {
291 Self {
292 require,
293 field: field.to_string(),
294 mode: "location_address".to_string(),
295 title: title.to_string(),
296 length: 0,
297 def,
298 show: true,
299 describe: "".to_string(),
300 example: JsonValue::Null,
301 }
302 }
303 pub fn length(mut self, length: i32) -> Self {
304 self.length = length;
305 self
306 }
307}
308
309impl Field for LocationAddress {
310 fn sql(&mut self, model: &str) -> String {
311 let not_null = if self.require { " not null" } else { "" };
312 match model {
313 "sqlite" => {
314 format!("`{}` TEXT{} default '{}'", self.field, not_null, self.def)
315 }
316 "pgsql" => {
317 let sql = format!(r#""{}" TEXT default '{}' "#, self.field, self.def);
318 format!("{} --{}|{}|{}", sql, self.mode, self.title, self.length)
319 }
320 _ => {
321 let sql = format!("`{}` TEXT{} default '{}'", self.field, not_null, self.def);
322 format!(
323 "{} comment '{}|{}|{}'",
324 sql, self.mode, self.title, self.length
325 )
326 }
327 }
328 }
329 fn hide(&mut self) -> &mut Self {
330 self.show = false;
331 self
332 }
333 fn describe(&mut self, text: &str) -> &mut Self {
334 self.describe = text.to_string();
335 self
336 }
337
338 fn field(&mut self) -> JsonValue {
339 let mut field = object! {};
340 field
341 .insert("require", JsonValue::from(self.require))
342 .unwrap();
343 field
344 .insert("field", JsonValue::from(self.field.clone()))
345 .unwrap();
346 field
347 .insert("mode", JsonValue::from(self.mode.clone()))
348 .unwrap();
349 field
350 .insert("title", JsonValue::from(self.title.clone()))
351 .unwrap();
352 field
353 .insert("length", JsonValue::from(self.length))
354 .unwrap();
355 field.insert("show", JsonValue::from(self.show)).unwrap();
356 field
357 .insert("describe", JsonValue::from(self.describe.clone()))
358 .unwrap();
359 field.insert("def", self.def.clone()).unwrap();
360 field.insert("example", self.example.clone()).unwrap();
361 field
362 }
363
364 fn swagger(&mut self) -> JsonValue {
365 object! {
366 "type": self.mode.clone(),
367 "example": self.example.clone(),
368 }
369 }
370
371 fn example(&mut self, data: JsonValue) -> &mut Self {
372 self.example = data.clone();
373 self
374 }
375}
376
377pub struct Polygon {
386 pub require: bool,
387 pub field: String,
388 pub mode: String,
389 pub title: String,
390 pub def: JsonValue,
391 pub length: i32,
392
393 pub show: bool,
394 pub describe: String,
395 pub items: JsonValue,
396 pub example: JsonValue,
397 pub max_items: i32,
398}
399
400impl Polygon {
401 pub fn new(require: bool, field: &str, title: &str, def: JsonValue) -> Self {
402 Self {
403 require,
404 field: field.to_string(),
405 mode: "polygon".to_string(),
406 title: title.to_string(),
407 def,
408 length: 0,
409 show: true,
410 describe: "".to_string(),
411 items: JsonValue::Null,
412 example: JsonValue::Null,
413 max_items: 0,
414 }
415 }
416 pub fn length(mut self, length: i32) -> Self {
420 self.length = length;
421 self
422 }
423 pub fn items(mut self, item: JsonValue) -> Self {
424 self.items = item;
425 self
426 }
427 pub fn max_items(mut self, mun: i32) -> Self {
428 self.max_items = mun;
429 self
430 }
431}
432
433impl Field for Polygon {
434 fn sql(&mut self, model: &str) -> String {
435 let not_null = if self.require { " not null" } else { "" };
436 match model {
437 "sqlite" => {
438 format!("`{}` TEXT{} default '{}'", self.field, not_null, self.def)
439 }
440 "pgsql" => {
441 let sql = format!(r#""{}" TEXT default '{}'"#, self.field, self.def);
442 format!("{} --{}|{}|{}", sql, self.mode, self.title, self.length)
443 }
444 _ => {
445 let sql = match self.length {
446 1 => format!(
447 "`{}` MEDIUMTEXT{} default '{}'",
448 self.field, not_null, self.def
449 ),
450 2 => format!(
451 "`{}` LONGTEXT{} default '{}'",
452 self.field, not_null, self.def
453 ),
454 _ => format!("`{}` TEXT{} default '{}'", self.field, not_null, self.def),
455 };
456 format!(
457 "{} comment '{}|{}|{}'",
458 sql, self.mode, self.title, self.length
459 )
460 }
461 }
462 }
463 fn hide(&mut self) -> &mut Self {
464 self.show = false;
465 self
466 }
467 fn describe(&mut self, text: &str) -> &mut Self {
468 self.describe = text.to_string();
469 self
470 }
471
472 fn field(&mut self) -> JsonValue {
473 let mut field = object! {};
474 field
475 .insert("require", JsonValue::from(self.require))
476 .unwrap();
477 field
478 .insert("field", JsonValue::from(self.field.clone()))
479 .unwrap();
480 field
481 .insert("mode", JsonValue::from(self.mode.clone()))
482 .unwrap();
483 field
484 .insert("title", JsonValue::from(self.title.clone()))
485 .unwrap();
486 field
487 .insert("length", JsonValue::from(self.length))
488 .unwrap();
489
490 field.insert("def", self.def.clone()).unwrap();
491
492 field.insert("show", JsonValue::from(self.show)).unwrap();
493 field
494 .insert("describe", JsonValue::from(self.describe.clone()))
495 .unwrap();
496 field.insert("items", self.items.clone()).unwrap();
497 field.insert("example", self.example.clone()).unwrap();
498 field
499 .insert("max_items", JsonValue::from(self.max_items))
500 .unwrap();
501 field
502 }
503
504 fn swagger(&mut self) -> JsonValue {
505 object! {
506 "type": self.mode.clone(),
507 "example": self.example.clone(),
508 }
509 }
510
511 fn example(&mut self, data: JsonValue) -> &mut Self {
512 self.example = data.clone();
513 self
514 }
515}
516
517#[cfg(test)]
518mod test {
519 use crate::Field;
520
521 #[test]
522 fn test() {
523 let res = crate::location::Location::new(true, "str", "地理定位")
524 .describe("1313")
525 .length(0)
526 .field();
527 println!("{res:#}");
528 }
529}