df_fields/
text.rs

1use json::{JsonValue, object};
2use crate::Field;
3
4/// 长文本
5///
6/// * require 是否必填
7/// * field 字段名
8/// * mode 模式 string
9/// * title 字段描述
10/// * length 附件数量
11/// * def 默认值
12/// * dec 后缀
13pub struct Text {
14    pub require: bool,
15    pub field: String,
16    pub mode: String,
17    pub title: String,
18    pub def: String,
19    pub length: i32,
20    pub dec: String,
21}
22
23impl Text {
24    pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
25        Self {
26            require,
27            field: field.to_string(),
28            mode: "text".to_string(),
29            title: title.to_string(),
30            def: default.to_string(),
31            length: 0,
32            dec: "".to_string(),
33        }
34    }
35}
36
37impl Field for Text {
38    fn sql(&mut self, model: &str) -> String {
39        let sql = format!("{} text", self.field);
40        match model {
41            "sqlite" => sql,
42            _ => format!("{} comment '{}|{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def, self.dec)
43        }
44    }
45    fn field(&mut self) -> JsonValue {
46        let mut field = object! {};
47        field.insert("require", JsonValue::from(self.require.clone())).unwrap();
48        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
49        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
50        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
51        field.insert("length", JsonValue::from(self.length.clone())).unwrap();
52        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
53        field.insert("dec", JsonValue::from(self.dec.clone())).unwrap();
54        field
55    }
56}
57
58
59/// 附件
60///
61/// * require 是否必填
62/// * field 字段名
63/// * mode 模式 file
64/// * title 字段描述
65/// * length 附件数量
66/// * def 默认值
67pub struct File {
68    pub require: bool,
69    pub field: String,
70    pub mode: String,
71    pub title: String,
72    pub length: i32,
73    pub encrypt: bool,
74    pub size: usize,
75    pub option: Vec<String>,
76}
77
78impl File {
79    pub fn new(require: bool, field: &str, title: &str, length: i32, encrypt: bool, size: usize, option: Vec<&str>) -> Self {
80        let len = {
81            if length <= 0 {
82                1
83            } else {
84                length
85            }
86        };
87        let size = {
88            if size <= 0 {
89                1024 * 1024
90            } else {
91                size
92            }
93        };
94        Self {
95            require,
96            field: field.to_string(),
97            mode: "file".to_string(),
98            title: title.to_string(),
99            length: len,
100            encrypt,
101            size,
102            option: option.iter().map(|c| c.to_string()).collect(),
103
104        }
105    }
106}
107
108impl Field for File {
109    fn sql(&mut self, model: &str) -> String {
110        let sql = format!("{} text", self.field);
111        let options = format!("{}", self.option.join(","));
112        match model {
113            "sqlite" => sql,
114            _ => format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.length, options)
115        }
116    }
117    fn field(&mut self) -> JsonValue {
118        let mut field = object! {};
119        field.insert("require", JsonValue::from(self.require.clone())).unwrap();
120        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
121        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
122        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
123        field.insert("length", JsonValue::from(self.length.clone())).unwrap();
124        field.insert("encrypt", JsonValue::from(self.encrypt.clone())).unwrap();
125        field.insert("size", JsonValue::from(self.size.clone())).unwrap();
126        field.insert("option", JsonValue::from(self.option.clone())).unwrap();
127        field
128    }
129}
130
131
132/// 关联表
133///
134/// * field 字段名
135/// * mode 模式 string
136/// * title 字段描述
137/// * length 字段总长度(含小数位)
138/// * default 默认值
139/// * empty 是否可空
140/// * dec 小数位
141pub struct Table {
142    pub require: bool,
143    pub field: String,
144    pub mode: String,
145    pub title: String,
146    pub table: String,
147    pub fields: Vec<String>,
148    pub api: String,
149}
150
151impl Table {
152    pub fn new(require: bool, field: &str, title: &str, table: &str, fields: Vec<&str>, api: &str) -> Self {
153        Self {
154            require,
155            field: field.to_string(),
156            mode: "table".to_string(),
157            title: title.to_string(),
158            table: table.to_string(),
159            api: api.to_string(),
160            fields: fields.iter().map(|c| c.to_string()).collect(),
161        }
162    }
163}
164
165impl Field for Table {
166    fn sql(&mut self, model: &str) -> String {
167        let sql = format!("{} text", self.field);
168        match model {
169            "sqlite" => sql,
170            _ => format!("{} comment '{}|{}|{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.table, self.fields.join("|"), self.api)
171        }
172    }
173    fn field(&mut self) -> JsonValue {
174        let mut field = object! {};
175        field.insert("require", JsonValue::from(self.require.clone())).unwrap();
176        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
177        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
178        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
179        field.insert("table", JsonValue::from(self.table.clone())).unwrap();
180        field.insert("api", JsonValue::from(self.api.clone())).unwrap();
181        field.insert("fields", JsonValue::from(self.fields.clone())).unwrap();
182        field
183    }
184}
185
186
187/// JSON
188///
189/// * field 字段名
190/// * mode 模式 json
191/// * title 字段描述
192/// * default 默认值
193pub struct Json {
194    pub require: bool,
195    pub field: String,
196    pub mode: String,
197    pub title: String,
198    pub def: JsonValue,
199    pub length: i32,
200}
201
202impl Json {
203    pub fn new(require: bool, field: &str, title: &str, def: JsonValue) -> Self {
204        Self {
205            require,
206            field: field.to_string(),
207            mode: "json".to_string(),
208            title: title.to_string(),
209            def,
210            length: 0,
211        }
212    }
213}
214
215impl Field for Json {
216    fn sql(&mut self, model: &str) -> String {
217        let sql = format!("{} text", self.field);
218        match model {
219            "sqlite" => sql,
220            _ => format!("{} comment '{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.def.to_string())
221        }
222    }
223    fn field(&mut self) -> JsonValue {
224        let mut field = object! {};
225        field.insert("require", JsonValue::from(self.require.clone())).unwrap();
226        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
227        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
228        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
229        field.insert("table", JsonValue::from(self.def.clone())).unwrap();
230        field
231    }
232}
233
234
235/// 网址
236///
237/// * require 是否必填
238/// * field 字段名
239/// * mode 模式 string
240/// * title 字段描述
241/// * length 附件数量
242/// * def 默认值
243/// * dec 后缀
244pub struct Url {
245    pub require: bool,
246    pub field: String,
247    pub mode: String,
248    pub title: String,
249    pub def: String,
250    pub length: i32,
251    pub dec: String,
252}
253
254impl Url {
255    pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
256        Self {
257            require,
258            field: field.to_string(),
259            mode: "url".to_string(),
260            title: title.to_string(),
261            def: default.to_string(),
262            length: 0,
263            dec: "".to_string(),
264        }
265    }
266}
267
268impl Field for Url {
269    fn sql(&mut self, model: &str) -> String {
270        let sql = format!("{} text", self.field);
271        match model {
272            "sqlite" => sql,
273            _ => format!("{} comment '{}|{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def, self.dec)
274        }
275    }
276    fn field(&mut self) -> JsonValue {
277        let mut field = object! {};
278        field.insert("require", JsonValue::from(self.require.clone())).unwrap();
279        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
280        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
281        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
282        field.insert("length", JsonValue::from(self.length.clone())).unwrap();
283        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
284        field.insert("dec", JsonValue::from(self.dec.clone())).unwrap();
285        field
286    }
287}