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