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