1use crate::Field;
2use json::{object, JsonValue};
3
4pub struct Int {
14 pub require: bool,
15 pub field: String,
16 pub mode: String,
17 pub title: String,
18 pub def: i64,
19 pub length: i32,
20 pub show: bool,
21 pub describe: String,
22 pub example: JsonValue,
23}
24
25impl Int {
26 pub fn new(require: bool, field: &str, title: &str, length: i32, default: i64) -> Self {
27 Self {
28 require,
29 field: field.to_string(),
30 mode: "int".to_string(),
31 title: title.to_string(),
32 def: default,
33 length,
34 show: true,
35 describe: "".to_string(),
36 example: JsonValue::Null,
37 }
38 }
39}
40
41impl Field for Int {
42 fn sql(&mut self, model: &str) -> String {
43 let not_null = if self.require { " not null" } else { "" };
44 match model {
45 "sqlite" => format!("`{}` INTEGER{} default {}", self.field, not_null, self.def),
46 "pgsql" => {
47 let sql = format!(r#""{}" INTEGER default {}"#, self.field, self.def);
48 format!(
49 "{} --{}|{}|{}|{}|{}",
50 sql, self.mode, self.require, self.title, self.length, self.def
51 )
52 }
53 _ => {
54 let sql = format!(
55 "`{}` int({}){} default {}",
56 self.field, self.length, not_null, self.def
57 );
58 format!(
59 "{} comment '{}|{}|{}|{}|{}'",
60 sql.clone(),
61 self.mode,
62 self.require,
63 self.title,
64 self.length,
65 self.def
66 )
67 }
68 }
69 }
70 fn hide(&mut self) -> &mut Self {
71 self.show = false;
72 self
73 }
74 fn describe(&mut self, text: &str) -> &mut Self {
75 self.describe = text.to_string();
76 self
77 }
78 fn field(&mut self) -> JsonValue {
79 let mut field = object! {};
80 field
81 .insert("require", JsonValue::from(self.require))
82 .unwrap();
83 field
84 .insert("field", JsonValue::from(self.field.clone()))
85 .unwrap();
86 field
87 .insert("mode", JsonValue::from(self.mode.clone()))
88 .unwrap();
89 field
90 .insert("title", JsonValue::from(self.title.clone()))
91 .unwrap();
92 field
93 .insert("length", JsonValue::from(self.length))
94 .unwrap();
95 field.insert("def", JsonValue::from(self.def)).unwrap();
96
97 field.insert("show", JsonValue::from(self.show)).unwrap();
98 field
99 .insert("describe", JsonValue::from(self.describe.clone()))
100 .unwrap();
101 field.insert("example", self.example.clone()).unwrap();
102 field
103 }
104
105 fn swagger(&mut self) -> JsonValue {
106 self.mode = match self.mode.as_str() {
107 "int" => "integer",
108 _ => self.mode.as_str(),
109 }
110 .to_string();
111 object! {
112 "type": self.mode.clone(),
113 "example": self.example.clone(),
114 }
115 }
116
117 fn example(&mut self, data: JsonValue) -> &mut Self {
118 self.example = data.clone();
119 self
120 }
121}
122
123pub struct Switch {
131 pub require: bool,
132 pub field: String,
133 pub mode: String,
134 pub title: String,
135 pub def: bool,
136
137 pub show: bool,
138 pub describe: String,
139 pub example: JsonValue,
140}
141
142impl Switch {
143 pub fn new(require: bool, field: &str, title: &str, default: bool) -> Self {
144 Self {
145 require,
146 field: field.to_string(),
147 mode: "switch".to_string(),
148 title: title.to_string(),
149 def: default,
150 show: true,
151 describe: "".to_string(),
152 example: JsonValue::Null,
153 }
154 }
155}
156
157impl Field for Switch {
158 fn sql(&mut self, model: &str) -> String {
159 let not_null = if self.require { " not null" } else { "" };
160 match model {
161 "sqlite" => {
162 let def = if self.def { 1 } else { 0 };
163 format!("`{}` INTEGER{} default {}", self.field, not_null, def)
164 }
165 "pgsql" => {
166 let sql = format!(r#""{}" BOOLEAN default {}"#, self.field, self.def);
167 format!(
168 "{} --{}|{}|{}|{}|BOOLEAN",
169 sql, self.mode, self.require, self.title, self.def
170 )
171 }
172 "mysql" => {
173 let sql = format!("`{}` BOOLEAN{} default {}", self.field, not_null, self.def);
174 format!(
175 "{} comment '{}|{}|{}|{}|BOOLEAN'",
176 sql.clone(),
177 self.mode,
178 self.require,
179 self.title,
180 self.def
181 )
182 }
183 _ => {
184 let sql = format!(
185 "`{}` tinyint(1){} default {}",
186 self.field, not_null, self.def
187 );
188 format!(
189 "{} comment '{}|{}|{}|{}'",
190 sql.clone(),
191 self.mode,
192 self.require,
193 self.title,
194 self.def
195 )
196 }
197 }
198 }
199 fn hide(&mut self) -> &mut Self {
200 self.show = false;
201 self
202 }
203 fn describe(&mut self, text: &str) -> &mut Self {
204 self.describe = text.to_string();
205 self
206 }
207 fn field(&mut self) -> JsonValue {
208 let mut field = object! {};
209 field
210 .insert("require", JsonValue::from(self.require))
211 .unwrap();
212 field
213 .insert("field", JsonValue::from(self.field.clone()))
214 .unwrap();
215 field
216 .insert("mode", JsonValue::from(self.mode.clone()))
217 .unwrap();
218 field
219 .insert("title", JsonValue::from(self.title.clone()))
220 .unwrap();
221
222 field.insert("def", JsonValue::from(self.def)).unwrap();
223
224 field.insert("show", JsonValue::from(self.show)).unwrap();
225 field
226 .insert("describe", JsonValue::from(self.describe.clone()))
227 .unwrap();
228 field.insert("example", self.example.clone()).unwrap();
229 field
230 }
231
232 fn swagger(&mut self) -> JsonValue {
233 object! {
234 "type": self.mode.clone(),
235 "example": self.example.clone(),
236 }
237 }
238
239 fn example(&mut self, data: JsonValue) -> &mut Self {
240 self.example = data.clone();
241 self
242 }
243}