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.insert("require", JsonValue::from(self.require)).unwrap();
231 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
232 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
233 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
234 field.insert("length", JsonValue::from(self.length)).unwrap();
235 field.insert("show", JsonValue::from(self.show)).unwrap();
236 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
237 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
238 field.insert("example", self.example.clone()).unwrap();
239 field
240 }
241
242 fn swagger(&mut self) -> JsonValue {
243 object! {
244 "type": self.mode.clone(),
245 "example": self.example.clone(),
246 }
247 }
248
249 fn example(&mut self, data: JsonValue) -> &mut Self {
250 self.example = data.clone();
251 self
252 }
253}
254
255pub struct Polygon {
264 pub require: bool,
265 pub field: String,
266 pub mode: String,
267 pub title: String,
268 pub def: JsonValue,
269 pub length: i32,
270
271 pub show: bool,
272 pub describe: String,
273 pub items: JsonValue,
274 pub example: JsonValue,
275 pub max_items: i32,
276}
277
278impl Polygon {
279 pub fn new(require: bool, field: &str, title: &str, def: JsonValue) -> Self {
280 Self {
281 require,
282 field: field.to_string(),
283 mode: "polygon".to_string(),
284 title: title.to_string(),
285 def,
286 length: 0,
287 show: true,
288 describe: "".to_string(),
289 items: JsonValue::Null,
290 example: JsonValue::Null,
291 max_items: 0,
292 }
293 }
294 pub fn length(mut self, length: i32) -> Self {
298 self.length = length;
299 self
300 }
301 pub fn items(mut self, item: JsonValue) -> Self {
302 self.items = item;
303 self
304 }
305 pub fn max_items(mut self, mun: i32) -> Self {
306 self.max_items = mun;
307 self
308 }
309}
310
311impl Field for Polygon {
312 fn sql(&mut self, model: &str) -> String {
313 let not_null = if self.require { " not null" } else { "" };
314 match model {
315 "sqlite" => {
316 format!("`{}` TEXT{} default '{}'", self.field, not_null, self.def)
317 }
318 "pgsql" => {
319 let sql = format!(r#""{}" TEXT default '{}'"#, self.field, self.def);
320 format!("{} --{}|{}|{}", sql, self.mode, self.title, self.length)
321 }
322 _ => {
323 let sql = match self.length {
324 1 => format!(
325 "`{}` MEDIUMTEXT{} default '{}'",
326 self.field, not_null, self.def
327 ),
328 2 => format!(
329 "`{}` LONGTEXT{} default '{}'",
330 self.field, not_null, self.def
331 ),
332 _ => format!("`{}` TEXT{} default '{}'", self.field, not_null, self.def),
333 };
334 format!(
335 "{} comment '{}|{}|{}'",
336 sql, self.mode, self.title, self.length
337 )
338 }
339 }
340 }
341 fn hide(&mut self) -> &mut Self {
342 self.show = false;
343 self
344 }
345 fn describe(&mut self, text: &str) -> &mut Self {
346 self.describe = text.to_string();
347 self
348 }
349
350 fn field(&mut self) -> JsonValue {
351 let mut field = object! {};
352 field
353 .insert("require", JsonValue::from(self.require))
354 .unwrap();
355 field
356 .insert("field", JsonValue::from(self.field.clone()))
357 .unwrap();
358 field
359 .insert("mode", JsonValue::from(self.mode.clone()))
360 .unwrap();
361 field
362 .insert("title", JsonValue::from(self.title.clone()))
363 .unwrap();
364 field
365 .insert("length", JsonValue::from(self.length))
366 .unwrap();
367
368 field.insert("def", self.def.clone()).unwrap();
369
370 field.insert("show", JsonValue::from(self.show)).unwrap();
371 field
372 .insert("describe", JsonValue::from(self.describe.clone()))
373 .unwrap();
374 field.insert("items", self.items.clone()).unwrap();
375 field.insert("example", self.example.clone()).unwrap();
376 field
377 .insert("max_items", JsonValue::from(self.max_items))
378 .unwrap();
379 field
380 }
381
382 fn swagger(&mut self) -> JsonValue {
383 object! {
384 "type": self.mode.clone(),
385 "example": self.example.clone(),
386 }
387 }
388
389 fn example(&mut self, data: JsonValue) -> &mut Self {
390 self.example = data.clone();
391 self
392 }
393}
394
395#[cfg(test)]
396mod test {
397 use crate::Field;
398
399 #[test]
400 fn test() {
401 let res = crate::location::Location::new(true, "str", "地理定位")
402 .describe("1313")
403 .length(0)
404 .field();
405 println!("{res:#}");
406 }
407}