br_fields/
int.rs

1use crate::Field;
2use json::{object, JsonValue};
3
4/// 数字
5///
6/// * require 是否必填
7/// * field 字段名
8/// * mode 模式 string
9/// * title 字段描述
10/// * length 字段总长度(含小数位)
11/// * def 默认值
12/// * dec 符号
13pub 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 sql = format!(
44            "`{}` int({})  default {}",
45            self.field, self.length, self.def
46        );
47        match model {
48            "sqlite" => sql,
49            "pgsql" => {
50                let sql = format!("{} INTEGER  default {}", self.field, self.def);
51                format!(
52                    "{} comment '{}|{}|{}|{}|{}'",
53                    sql.clone(),
54                    self.mode,
55                    self.require,
56                    self.title,
57                    self.length,
58                    self.def
59                )
60            }
61            _ => {
62                let sql = format!(
63                    "`{}` int({})  default {}",
64                    self.field, self.length, self.def
65                );
66                format!(
67                    "{} comment '{}|{}|{}|{}|{}'",
68                    sql.clone(),
69                    self.mode,
70                    self.require,
71                    self.title,
72                    self.length,
73                    self.def
74                )
75            }
76        }
77    }
78    fn hide(&mut self) -> &mut Self {
79        self.show = false;
80        self
81    }
82    fn describe(&mut self, text: &str) -> &mut Self {
83        self.describe = text.to_string();
84        self
85    }
86    fn field(&mut self) -> JsonValue {
87        let mut field = object! {};
88        field.insert("require", JsonValue::from(self.require)).unwrap();
89        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
90        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
91        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
92        field.insert("length", JsonValue::from(self.length)).unwrap();
93        field.insert("def", JsonValue::from(self.def)).unwrap();
94
95        field.insert("show", JsonValue::from(self.show)).unwrap();
96        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
97        field.insert("example", self.example.clone()).unwrap();
98        field
99    }
100
101    fn swagger(&mut self) -> JsonValue {
102        self.mode = match self.mode.as_str() {
103            "int" => "integer",
104            _ => self.mode.as_str(),
105        }.to_string();
106        object! {
107            "type": self.mode.clone(),
108            "example": self.example.clone(),
109        }
110    }
111
112    fn example(&mut self, data: JsonValue) -> &mut Self {
113        self.example = data.clone();
114        self
115    }
116}
117
118/// 开关
119///
120/// * field 字段名
121/// * mode 模式 string
122/// * title 字段描述
123/// * default 默认值
124/// * empty 是否可空
125pub struct Switch {
126    pub require: bool,
127    pub field: String,
128    pub mode: String,
129    pub title: String,
130    pub def: bool,
131
132    pub show: bool,
133    pub describe: String,
134    pub example: JsonValue,
135}
136
137impl Switch {
138    pub fn new(require: bool, field: &str, title: &str, default: bool) -> Self {
139        Self {
140            require,
141            field: field.to_string(),
142            mode: "switch".to_string(),
143            title: title.to_string(),
144            def: default,
145            show: true,
146            describe: "".to_string(),
147            example: JsonValue::Null,
148        }
149    }
150}
151
152impl Field for Switch {
153    fn sql(&mut self, model: &str) -> String {
154        match model {
155            "sqlite" => {
156                let def = if self.def { 1 } else { 0 };
157                format!("`{}` INTEGER  default {}", self.field, def)
158            }
159            "pgsql" => {
160                let sql = format!(r#""{}" BOOLEAN  default {}"#, self.field, self.def);
161                format!(
162                    "{} comment '{}|{}|{}|{}'",
163                    sql.clone(),
164                    self.mode,
165                    self.require,
166                    self.title,
167                    self.def
168                )
169            }
170            "mysql"=>{
171                let sql = format!("`{}` BOOLEAN  default {}", self.field, 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!("`{}` tinyint(1)  default {}", self.field, self.def);
183                format!(
184                    "{} comment '{}|{}|{}|{}'",
185                    sql.clone(),
186                    self.mode,
187                    self.require,
188                    self.title,
189                    self.def
190                )
191            }
192        }
193    }
194    fn hide(&mut self) -> &mut Self {
195        self.show = false;
196        self
197    }
198    fn describe(&mut self, text: &str) -> &mut Self {
199        self.describe = text.to_string();
200        self
201    }
202    fn field(&mut self) -> JsonValue {
203        let mut field = object! {};
204        field.insert("require", JsonValue::from(self.require)).unwrap();
205        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
206        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
207        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
208
209        field.insert("def", JsonValue::from(self.def)).unwrap();
210
211        field.insert("show", JsonValue::from(self.show)).unwrap();
212        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
213        field.insert("example", self.example.clone()).unwrap();
214        field
215    }
216
217    fn swagger(&mut self) -> JsonValue {
218        object! {
219            "type": self.mode.clone(),
220            "example": self.example.clone(),
221        }
222    }
223
224    fn example(&mut self, data: JsonValue) -> &mut Self {
225        self.example = data.clone();
226        self
227    }
228}