1use crate::Field;
2use json::{object, JsonValue};
3
4pub struct Radio {
5 pub require: bool,
6 pub field: String,
7 pub mode: String,
8 pub title: String,
9 pub def: String,
10 pub option: Vec<String>,
11 pub length: i32,
12 pub show: bool,
13 pub describe: String,
14 pub example: JsonValue,
15 table_name: String,
16}
17
18impl Radio {
19 pub fn new(
20 require: bool,
21 field: &str,
22 title: &str,
23 mut option: Vec<&str>,
24 default: &str,
25 ) -> Self {
26 if !require && default.is_empty() && !option.contains(&"") {
27 option.push("")
28 }
29 Self {
30 require,
31 field: field.to_string(),
32 mode: "radio".to_string(),
33 title: title.to_string(),
34 def: default.to_string(),
35 option: option.iter().map(|c| c.to_string()).collect(),
36 length: 0,
37 show: true,
38 describe: "".to_string(),
39 example: JsonValue::Null,
40 table_name: "".to_string(),
41 }
42 }
43 pub fn table_name(&mut self, table_name: &str) -> &mut Self {
44 self.table_name = table_name.to_string();
45 self
46 }
47}
48
49impl Field for Radio {
50 fn sql(&mut self, model: &str) -> String {
51 let not_null = if self.require { " not null" } else { "" };
52 let length = self.option.join("").len() + self.option.len();
53 match model {
54 "sqlite" => {
55 format!(
56 "`{}` varchar({}){} default '{}'",
57 self.field, length, not_null, self.def
58 )
59 }
60 "pgsql" => {
61 let sql = format!(
62 r#""{}" varchar({}) default '{}'"#,
63 self.field, length, self.def
64 );
65 format!(
66 "{} --{}|{}|{}|{}|{}|{}",
67 sql,
68 self.mode,
69 self.require,
70 self.title,
71 self.length,
72 self.def,
73 self.option.join("|")
74 )
75 }
76 _ => {
77 let sql = format!(
78 "`{}` set('{}'){} default '{}'",
79 self.field,
80 self.option.join("','"),
81 not_null,
82 self.def
83 );
84 format!(
85 "{} comment '{}|{}|{}|{}|{}|{}'",
86 sql,
87 self.mode,
88 self.require,
89 self.title,
90 self.length,
91 self.def,
92 self.option.join("|")
93 )
94 }
95 }
96 }
97 fn hide(&mut self) -> &mut Self {
98 self.show = false;
99 self
100 }
101 fn describe(&mut self, text: &str) -> &mut Self {
102 self.describe = text.to_string();
103 self
104 }
105 fn field(&mut self) -> JsonValue {
106 let mut field = object! {};
107 field
108 .insert("require", JsonValue::from(self.require))
109 .unwrap();
110 field
111 .insert("field", JsonValue::from(self.field.clone()))
112 .unwrap();
113 field
114 .insert("mode", JsonValue::from(self.mode.clone()))
115 .unwrap();
116 field
117 .insert("title", JsonValue::from(self.title.clone()))
118 .unwrap();
119 field
120 .insert("length", JsonValue::from(self.length))
121 .unwrap();
122 field
123 .insert("def", JsonValue::from(self.def.clone()))
124 .unwrap();
125
126 field
127 .insert("option", JsonValue::from(self.option.clone()))
128 .unwrap();
129
130 field.insert("show", JsonValue::from(self.show)).unwrap();
131 field
132 .insert("describe", JsonValue::from(self.describe.clone()))
133 .unwrap();
134 field.insert("example", self.example.clone()).unwrap();
135 field
136 }
137
138 fn swagger(&mut self) -> JsonValue {
139 self.mode = match self.mode.as_str() {
140 "radio" => "string",
141 _ => self.mode.as_str(),
142 }
143 .to_string();
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 Select {
157 pub require: bool,
158 pub field: String,
159 pub mode: String,
160 pub title: String,
161 pub option: Vec<String>,
162 pub def: Vec<String>,
163 pub length: i32,
164 pub show: bool,
165 pub describe: String,
166 pub example: JsonValue,
167 pub table_name: String,
168}
169
170impl Select {
171 pub fn new(
172 require: bool,
173 field: &str,
174 title: &str,
175 mut option: Vec<&str>,
176 default: Vec<&str>,
177 ) -> Self {
178 if !require && default.is_empty() && !option.contains(&"") {
179 option.push("")
180 }
181 Self {
182 require,
183 field: field.to_string(),
184 mode: "select".to_string(),
185 title: title.to_string(),
186 def: default.iter().map(|c| c.to_string()).collect(),
187 option: option.iter().map(|c| c.to_string()).collect(),
188 length: 0,
189 show: true,
190 describe: "".to_string(),
191 example: JsonValue::Null,
192 table_name: String::new(),
193 }
194 }
195 pub fn table_name(&mut self, table_name: &str) -> &mut Self {
196 self.table_name = table_name.to_string();
197 self
198 }
199}
200
201impl Field for Select {
202 fn sql(&mut self, model: &str) -> String {
203 let not_null = if self.require { " not null" } else { "" };
204 let length = self.option.join("").len() + self.option.len();
205 let default_val = if self.def.is_empty() {
206 "".to_string()
207 } else {
208 self.def.join(",")
209 };
210 match model {
211 "sqlite" => {
212 format!(
213 "{} varchar({}){} default '{}'",
214 self.field, length, not_null, default_val
215 )
216 }
217 "pgsql" => {
218 let sql = format!(
219 r#""{}" varchar({}) default '{}'"#,
220 self.field, length, default_val
221 );
222 format!(
223 "{} --{}|{}|{}|{}|{}|{}",
224 sql,
225 self.mode,
226 self.require,
227 self.title,
228 self.length,
229 self.def.join("|"),
230 self.option.join("|")
231 )
232 }
233 _ => {
234 let json_default = if self.def.is_empty() {
235 "('[]')".to_string()
236 } else {
237 format!("('[\"{}\"]')", self.def.join("\",\""))
238 };
239 let sql = format!("`{}` json default {}", self.field, json_default);
240 format!(
241 "{} comment '{}|{}|{}|{}|{}|{}'",
242 sql,
243 self.mode,
244 self.require,
245 self.title,
246 self.length,
247 self.def.join("|"),
248 self.option.join("|")
249 )
250 }
251 }
252 }
253 fn hide(&mut self) -> &mut Self {
254 self.show = false;
255 self
256 }
257 fn describe(&mut self, text: &str) -> &mut Self {
258 self.describe = text.to_string();
259 self
260 }
261
262 fn field(&mut self) -> JsonValue {
263 let mut field = object! {};
264 field
265 .insert("require", JsonValue::from(self.require))
266 .unwrap();
267 field
268 .insert("field", JsonValue::from(self.field.clone()))
269 .unwrap();
270 field
271 .insert("mode", JsonValue::from(self.mode.clone()))
272 .unwrap();
273 field
274 .insert("title", JsonValue::from(self.title.clone()))
275 .unwrap();
276 field
277 .insert("length", JsonValue::from(self.length))
278 .unwrap();
279
280 field
281 .insert("def", JsonValue::from(self.def.clone()))
282 .unwrap();
283 field
284 .insert("option", JsonValue::from(self.option.clone()))
285 .unwrap();
286
287 field.insert("show", JsonValue::from(self.show)).unwrap();
288 field
289 .insert("describe", JsonValue::from(self.describe.clone()))
290 .unwrap();
291 field.insert("example", self.example.clone()).unwrap();
292 field
293 }
294
295 fn swagger(&mut self) -> JsonValue {
296 object! {
297 "type": self.mode.clone(),
298 "example": self.example.clone(),
299 }
300 }
301
302 fn example(&mut self, data: JsonValue) -> &mut Self {
303 self.example = data.clone();
304 self
305 }
306}