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 format!(
62 r#""{}" varchar({}){} default '{}'"#,
63 self.field, length, not_null, self.def
64 )
65 }
66 _ => {
67 let sql = format!(
68 "`{}` set('{}'){} default '{}'",
69 self.field,
70 self.option.join("','"),
71 not_null,
72 self.def
73 );
74 format!(
75 "{} comment '{}|{}|{}|{}|{}|{}'",
76 sql.clone(),
77 self.mode,
78 self.require,
79 self.title,
80 self.length,
81 self.def,
82 self.option.join("|")
83 )
84 }
85 }
86 }
87 fn hide(&mut self) -> &mut Self {
88 self.show = false;
89 self
90 }
91 fn describe(&mut self, text: &str) -> &mut Self {
92 self.describe = text.to_string();
93 self
94 }
95 fn field(&mut self) -> JsonValue {
96 let mut field = object! {};
97 field
98 .insert("require", JsonValue::from(self.require))
99 .unwrap();
100 field
101 .insert("field", JsonValue::from(self.field.clone()))
102 .unwrap();
103 field
104 .insert("mode", JsonValue::from(self.mode.clone()))
105 .unwrap();
106 field
107 .insert("title", JsonValue::from(self.title.clone()))
108 .unwrap();
109 field
110 .insert("length", JsonValue::from(self.length))
111 .unwrap();
112 field
113 .insert("def", JsonValue::from(self.def.clone()))
114 .unwrap();
115
116 field
117 .insert("option", JsonValue::from(self.option.clone()))
118 .unwrap();
119
120 field.insert("show", JsonValue::from(self.show)).unwrap();
121 field
122 .insert("describe", JsonValue::from(self.describe.clone()))
123 .unwrap();
124 field.insert("example", self.example.clone()).unwrap();
125 field
126 }
127
128 fn swagger(&mut self) -> JsonValue {
129 self.mode = match self.mode.as_str() {
130 "radio" => "string",
131 _ => self.mode.as_str(),
132 }
133 .to_string();
134 object! {
135 "type": self.mode.clone(),
136 "example": self.example.clone(),
137 }
138 }
139
140 fn example(&mut self, data: JsonValue) -> &mut Self {
141 self.example = data.clone();
142 self
143 }
144}
145
146pub struct Select {
147 pub require: bool,
148 pub field: String,
149 pub mode: String,
150 pub title: String,
151 pub option: Vec<String>,
152 pub def: Vec<String>,
153 pub length: i32,
154 pub show: bool,
155 pub describe: String,
156 pub example: JsonValue,
157 pub table_name: String,
158}
159
160impl Select {
161 pub fn new(
162 require: bool,
163 field: &str,
164 title: &str,
165 mut option: Vec<&str>,
166 default: Vec<&str>,
167 ) -> Self {
168 if !require && default.is_empty() && !option.contains(&"") {
169 option.push("")
170 }
171 Self {
172 require,
173 field: field.to_string(),
174 mode: "select".to_string(),
175 title: title.to_string(),
176 def: default.iter().map(|c| c.to_string()).collect(),
177 option: option.iter().map(|c| c.to_string()).collect(),
178 length: 0,
179 show: true,
180 describe: "".to_string(),
181 example: JsonValue::Null,
182 table_name: String::new(),
183 }
184 }
185 pub fn table_name(&mut self, table_name: &str) -> &mut Self {
186 self.table_name = table_name.to_string();
187 self
188 }
189}
190
191impl Field for Select {
192 fn sql(&mut self, model: &str) -> String {
193 let not_null = if self.require { " not null" } else { "" };
194 let length = self.option.join("").len() + self.option.len();
195 let default_val = if self.def.is_empty() {
196 "".to_string()
197 } else {
198 self.def.join(",")
199 };
200 match model {
201 "sqlite" => {
202 format!(
203 "{} varchar({}){} default '{}'",
204 self.field, length, not_null, default_val
205 )
206 }
207 "pgsql" => {
208 format!(
209 r#""{}" varchar({}){} default '{}'"#,
210 self.field, length, not_null, default_val
211 )
212 }
213 _ => {
214 let sql = format!(
215 "`{}` set('{}'){} default '{}'",
216 self.field,
217 self.option.join("','"),
218 not_null,
219 default_val
220 );
221 format!(
222 "{} comment '{}|{}|{}|{}|{}|{}'",
223 sql.clone(),
224 self.mode,
225 self.require,
226 self.title,
227 self.length,
228 self.def.join("|"),
229 self.option.join("|")
230 )
231 }
232 }
233 }
234 fn hide(&mut self) -> &mut Self {
235 self.show = false;
236 self
237 }
238 fn describe(&mut self, text: &str) -> &mut Self {
239 self.describe = text.to_string();
240 self
241 }
242
243 fn field(&mut self) -> JsonValue {
244 let mut field = object! {};
245 field
246 .insert("require", JsonValue::from(self.require))
247 .unwrap();
248 field
249 .insert("field", JsonValue::from(self.field.clone()))
250 .unwrap();
251 field
252 .insert("mode", JsonValue::from(self.mode.clone()))
253 .unwrap();
254 field
255 .insert("title", JsonValue::from(self.title.clone()))
256 .unwrap();
257 field
258 .insert("length", JsonValue::from(self.length))
259 .unwrap();
260
261 field
262 .insert("def", JsonValue::from(self.def.clone()))
263 .unwrap();
264 field
265 .insert("option", JsonValue::from(self.option.clone()))
266 .unwrap();
267
268 field.insert("show", JsonValue::from(self.show)).unwrap();
269 field
270 .insert("describe", JsonValue::from(self.describe.clone()))
271 .unwrap();
272 field.insert("example", self.example.clone()).unwrap();
273 field
274 }
275
276 fn swagger(&mut self) -> JsonValue {
277 object! {
278 "type": self.mode.clone(),
279 "example": self.example.clone(),
280 }
281 }
282
283 fn example(&mut self, data: JsonValue) -> &mut Self {
284 self.example = data.clone();
285 self
286 }
287}