br_db/
fields.rs

1use json::{object, JsonValue};
2#[derive(Clone, Debug)]
3pub enum Fields {
4    String,
5    StringPass,
6    StringKey,
7    StringTel,
8    StringIdent,
9    StringEmail,
10    StringColor,
11    StringCode,
12    StringBarcode,
13    StringQrcode,
14    Radio,
15    Select,
16    TableList,
17    TableTree,
18    Int,
19    Switch,
20    Dict,
21    Text,
22    TextEditor,
23    TextUrl,
24    Json,
25    Array,
26    Object,
27    Float,
28    DateTime,
29    Year,
30}
31impl Fields {
32    pub fn name(&mut self, name: &str) -> Field {
33        Field::new(self.clone(), name)
34    }
35    pub fn str(self) -> &'static str {
36        match self {
37            Fields::String | Fields::StringPass | Fields::StringKey | Fields::StringTel | Fields::StringIdent | Fields::StringEmail | Fields::StringColor | Fields::StringCode | Fields::StringBarcode | Fields::StringQrcode => "string",
38            Fields::Radio => "radio",
39            Fields::Select => "select",
40            Fields::TableList => "table",
41            Fields::TableTree => "tree",
42            Fields::Int => "int",
43            Fields::Switch => "bool",
44            Fields::Dict => "string",
45            Fields::Text | Fields::TextEditor | Fields::TextUrl => "text",
46            Fields::Json => "json",
47            Fields::Array => "array",
48            Fields::Object => "object",
49            Fields::Float => "float",
50            Fields::DateTime => "datetime",
51            Fields::Year => "year",
52        }
53    }
54}
55#[derive(Debug)]
56pub struct Field {
57    mode: Fields,
58    /// 必填
59    require: bool,
60    field: String,
61    title: String,
62    /// 默认值
63    def: JsonValue,
64    /// 长度
65    length: usize,
66    /// 小数点位数
67    dec: usize,
68    /// 选择项目
69    option: Vec<&'static str>,
70    /// 是否显示
71    show: bool,
72    describe: String,
73    /// 示例
74    example: JsonValue,
75    /// 接口
76    api: String,
77    /// 表名称
78    table_name: String,
79    /// 是否多选
80    multiple: bool,
81}
82
83impl Field {
84    pub fn new(mode: Fields, name: &str) -> Field {
85        Self {
86            mode,
87            require: true,
88            field: name.to_string(),
89            title: "".to_string(),
90            def: JsonValue::Null,
91            length: 0,
92            dec: 0,
93            option: vec![],
94            show: true,
95            describe: "".to_string(),
96            example: JsonValue::Null,
97            api: "".to_string(),
98            table_name: "".to_string(),
99            multiple: false,
100        }
101    }
102    pub fn title(mut self, name: &str) -> Field {
103        self.title = name.to_string();
104        self
105    }
106    /// 默认值
107    pub fn def(mut self, name: JsonValue) -> Field {
108        self.def = name;
109        self
110    }
111    pub fn require(mut self, require: bool) -> Field {
112        self.require = require;
113        self
114    }
115    pub fn show(mut self, show: bool) -> Field {
116        self.show = show;
117        self
118    }
119    pub fn example(mut self, example: JsonValue) -> Field {
120        self.example = example;
121        self
122    }
123    pub fn length(mut self, length: usize) -> Field {
124        self.length = length;
125        self
126    }
127    pub fn describe(mut self, describe: &str) -> Field {
128        self.describe = describe.to_string();
129        self
130    }
131    pub fn option(mut self, option: Vec<&'static str>) -> Field {
132        self.option = option;
133        self
134    }
135    /// 关联数据表配置
136    pub fn table(mut self, table: &str, api: &str, fields: Vec<&'static str>) -> Field {
137        self.api = api.to_string();
138        self.table_name = table.to_string();
139        self.option = fields;
140        self
141    }
142    pub fn multiple(mut self, multiple: bool, count: usize) -> Field {
143        self.multiple = multiple;
144        if count == 0 {
145            self.length = 1
146        } else {
147            self.length = count;
148        }
149        self
150    }
151    pub fn dec(mut self, dec: usize) -> Field {
152        self.dec = dec;
153        self
154    }
155    pub fn sql(self) -> JsonValue {
156        object! {}
157    }
158    pub fn swagger(self) -> JsonValue {
159        object! {
160            "type": self.mode.str(),
161            "example": self.example,
162        }
163    }
164    pub fn field(self) -> JsonValue {
165        let example = if self.example.is_empty() {
166            self.def.clone()
167        } else {
168            self.example
169        };
170        let mut field = object! {
171            "require"=> self.require,
172            "field"=> self.field,
173            "title"=> self.title,
174            "show"=> self.show,
175            "describe"=> self.describe,
176            "example"=> example
177        };
178        match self.mode {
179            Fields::String | Fields::StringPass | Fields::StringKey | Fields::StringTel | Fields::StringIdent | Fields::StringEmail | Fields::StringColor | Fields::StringCode | Fields::StringBarcode | Fields::StringQrcode => {
180                field["length"] = self.length.into();
181                field["def"] = self.def;
182            }
183            Fields::Radio => {
184                field["option"] = self.option.into();
185                field["def"] = self.def;
186            }
187            Fields::Select => {
188                field["option"] = self.option.into();
189                field["def"] = self.def;
190            }
191            Fields::TableList | Fields::TableTree | Fields::Dict => {
192                field["option"] = self.option.into();
193                field["def"] = self.def;
194                field["api"] = self.api.into();
195                field["table"] = self.table_name.into();
196                field["multiple"] = self.multiple.into();
197                field["length"] = self.length.into();
198            }
199            Fields::Int => {
200                field["length"] = self.length.into();
201                field["def"] = self.def;
202            }
203            Fields::Switch => {
204                field["def"] = self.def;
205            }
206            Fields::Text | Fields::TextEditor | Fields::TextUrl | Fields::Json | Fields::Array | Fields::Object => {
207                field["length"] = self.length.into();
208                field["def"] = self.def;
209            }
210            Fields::Float => {
211                field["length"] = self.length.into();
212                field["dec"] = self.dec.into();
213                field["def"] = self.def;
214            }
215            Fields::DateTime => {
216                field["def"] = self.def;
217            }
218            Fields::Year => {
219                field["def"] = self.def;
220            }
221        };
222        field
223    }
224}
225mod test {
226    #[allow(unused_imports)]
227    use crate::fields::Fields;
228    #[test]
229    fn test() {
230        let res = Fields::String.name("Test").title("花球").def("话".into()).describe("哈哈哈").field();
231        println!("{res:#}");
232        let res = Fields::TableList.name("Test").title("花球").def("话".into()).describe("哈哈哈").table("tree", "atata.dadasd.ads", vec!["12313", "!2313123"]).multiple(false, 1).field();
233        println!("{res:#}");
234        let res = Fields::Int.name("Test").require(false).show(false).title("花球").describe("哈哈哈").length(20).def(1.into()).field();
235        println!("{res:#}");
236        let res = Fields::Radio.name("Test").title("花球").describe("哈哈哈").option(vec!["开心", "不开心"]).def("开心".into()).example("12313".into()).field();
237        println!("{res:#}");
238        let res = Fields::Select.name("Test").title("花球").describe("哈哈哈").option(vec!["开心", "不开心"]).def(vec!["开心", "不开心"].into()).example(vec!["开心", "不开心"].into()).field();
239        println!("{res:#}");
240        let res = Fields::Float.name("Test").title("花球").length(20).dec(3).def(1.02.into()).example(3.into()).field();
241        println!("{res:#}");
242    }
243}