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