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