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 format!(
73 r#""{}" varchar({}){} default '{}'"#,
74 self.field, length, not_null, self.def
75 )
76 }
77 "mysql" => {
78 let sql = format!("`{}` POINT SRID {}{}", self.field, self.length, not_null);
79 format!(
80 "{} comment '{}|{}|{}|{}|{}'",
81 sql.clone(),
82 self.title,
83 self.mode,
84 self.require,
85 self.def,
86 self.length
87 )
88 }
89 _ => {
90 let sql = format!(
91 "`{}` varchar({}){} default '{}'",
92 self.field, self.length, not_null, self.def
93 );
94 format!(
95 "{} comment '{}|{}|{}|{}|{}'",
96 sql.clone(),
97 self.title,
98 self.mode,
99 self.require,
100 self.def,
101 self.length
102 )
103 }
104 }
105 }
106 fn hide(&mut self) -> &mut Self {
107 self.show = false;
108 self
109 }
110 fn describe(&mut self, text: &str) -> &mut Self {
111 self.describe = text.to_string();
112 self
113 }
114
115 fn field(&mut self) -> JsonValue {
116 let mut field = object! {};
117 field
118 .insert("require", JsonValue::from(self.require))
119 .unwrap();
120 field
121 .insert("field", JsonValue::from(self.field.clone()))
122 .unwrap();
123 field
124 .insert("mode", JsonValue::from(self.mode.clone()))
125 .unwrap();
126 field
127 .insert("title", JsonValue::from(self.title.clone()))
128 .unwrap();
129 field
130 .insert("length", JsonValue::from(self.length))
131 .unwrap();
132 field.insert("show", JsonValue::from(self.show)).unwrap();
133 field
134 .insert("describe", JsonValue::from(self.describe.clone()))
135 .unwrap();
136 field
137 .insert("def", JsonValue::from(self.def.clone()))
138 .unwrap();
139 field.insert("example", self.example.clone()).unwrap();
140 field
141 }
142
143 fn swagger(&mut self) -> JsonValue {
144 object! {
145 "type": self.mode.clone(),
146 "example": self.example.clone(),
147 }
148 }
149
150 fn example(&mut self, data: JsonValue) -> &mut Self {
151 self.example = data.clone();
152 self
153 }
154}
155
156pub struct Polygon {
165 pub require: bool,
166 pub field: String,
167 pub mode: String,
168 pub title: String,
169 pub def: JsonValue,
170 pub length: i32,
171
172 pub show: bool,
173 pub describe: String,
174 pub items: JsonValue,
175 pub example: JsonValue,
176 pub max_items: i32,
177}
178
179impl Polygon {
180 pub fn new(require: bool, field: &str, title: &str, def: JsonValue) -> Self {
181 Self {
182 require,
183 field: field.to_string(),
184 mode: "polygon".to_string(),
185 title: title.to_string(),
186 def,
187 length: 0,
188 show: true,
189 describe: "".to_string(),
190 items: JsonValue::Null,
191 example: JsonValue::Null,
192 max_items: 0,
193 }
194 }
195 pub fn length(mut self, length: i32) -> Self {
199 self.length = length;
200 self
201 }
202 pub fn items(mut self, item: JsonValue) -> Self {
203 self.items = item;
204 self
205 }
206 pub fn max_items(mut self, mun: i32) -> Self {
207 self.max_items = mun;
208 self
209 }
210}
211
212impl Field for Polygon {
213 fn sql(&mut self, model: &str) -> String {
214 let not_null = if self.require { " not null" } else { "" };
215 match model {
216 "sqlite" => {
217 format!("{} TEXT{} default '{}'", self.field, not_null, self.def)
218 }
219 "pgsql" => {
220 format!(
221 r#""{}" TEXT{} default '{}'"#,
222 self.field, not_null, self.def
223 )
224 }
225 _ => {
226 let sql = match self.length {
227 1 => format!(
228 "`{}` MEDIUMTEXT{} default '{}'",
229 self.field, not_null, self.def
230 ),
231 2 => format!(
232 "`{}` LONGTEXT{} default '{}'",
233 self.field, not_null, self.def
234 ),
235 _ => format!("`{}` TEXT{} default '{}'", self.field, not_null, self.def),
236 };
237 format!(
238 "{} comment '{}|{}|{}'",
239 sql.clone(),
240 self.mode,
241 self.title,
242 self.length
243 )
244 }
245 }
246 }
247 fn hide(&mut self) -> &mut Self {
248 self.show = false;
249 self
250 }
251 fn describe(&mut self, text: &str) -> &mut Self {
252 self.describe = text.to_string();
253 self
254 }
255
256 fn field(&mut self) -> JsonValue {
257 let mut field = object! {};
258 field
259 .insert("require", JsonValue::from(self.require))
260 .unwrap();
261 field
262 .insert("field", JsonValue::from(self.field.clone()))
263 .unwrap();
264 field
265 .insert("mode", JsonValue::from(self.mode.clone()))
266 .unwrap();
267 field
268 .insert("title", JsonValue::from(self.title.clone()))
269 .unwrap();
270 field
271 .insert("length", JsonValue::from(self.length))
272 .unwrap();
273
274 field.insert("def", self.def.clone()).unwrap();
275
276 field.insert("show", JsonValue::from(self.show)).unwrap();
277 field
278 .insert("describe", JsonValue::from(self.describe.clone()))
279 .unwrap();
280 field.insert("items", self.items.clone()).unwrap();
281 field.insert("example", self.example.clone()).unwrap();
282 field
283 .insert("max_items", JsonValue::from(self.max_items))
284 .unwrap();
285 field
286 }
287
288 fn swagger(&mut self) -> JsonValue {
289 object! {
290 "type": self.mode.clone(),
291 "example": self.example.clone(),
292 }
293 }
294
295 fn example(&mut self, data: JsonValue) -> &mut Self {
296 self.example = data.clone();
297 self
298 }
299}
300
301#[cfg(test)]
302mod test {
303 use crate::Field;
304
305 #[test]
306 fn test() {
307 let res = crate::location::Location::new(true, "str", "地理定位")
308 .describe("1313")
309 .length(0)
310 .field();
311 println!("{res:#}");
312 }
313}