Skip to main content

br_fields/
files.rs

1use crate::Field;
2use json::{object, JsonValue};
3
4/// 附件
5///
6/// * require 是否必填
7/// * field 字段名
8/// * mode 模式 file
9/// * title 字段描述
10/// * length 附件数量
11/// * def 默认值
12pub struct Files {
13    pub require: bool,
14    pub field: String,
15    pub mode: String,
16    pub title: String,
17    pub length: i32,
18    /// 加密
19    pub encrypt: bool,
20    /// 文件尺寸
21    pub size: usize,
22    /// 运行的文件类型
23    pub option: Vec<String>,
24    pub show: bool,
25    pub describe: String,
26    /// 是否上传
27    pub upload: bool,
28    pub def: String,
29    pub example: JsonValue,
30}
31
32impl Files {
33    pub fn new(require: bool, field: &str, title: &str) -> Self {
34        Self {
35            require,
36            field: field.to_string(),
37            mode: "file".to_string(),
38            title: title.to_string(),
39            length: 1,
40            encrypt: false,
41            size: 0,
42            option: vec![],
43            def: "".to_string(),
44            show: true,
45            describe: "".to_string(),
46            example: JsonValue::Null,
47            upload: false,
48        }
49    }
50
51    pub fn encrypt(&mut self) -> &mut Self {
52        self.encrypt = true;
53        self
54    }
55    pub fn multiple(&mut self, number: i32) -> &mut Self {
56        self.length = number;
57        self
58    }
59    pub fn option(&mut self, option: Vec<&str>) -> &mut Self {
60        self.option = option.iter().map(|c| c.to_string()).collect();
61        self
62    }
63    pub fn size(&mut self, size: usize) -> &mut Self {
64        self.size = if size == 0 { 1024 * 1024 } else { size };
65        self
66    }
67    pub fn def(&mut self, def: &str) -> &mut Self {
68        self.def = def.to_string();
69        self
70    }
71    pub fn upload(&mut self, data: bool) -> &mut Self {
72        self.upload = data;
73        self
74    }
75}
76
77impl Field for Files {
78    fn sql(&mut self, model: &str) -> String {
79        let not_null = if self.require { " not null" } else { "" };
80        let length = self.length * 64 + self.length;
81        match model {
82            "sqlite" => format!(
83                "`{}` varchar({}){} default '{}'",
84                self.field, length, not_null, self.def
85            ),
86            "pgsql" => {
87                let sql = format!(
88                    r#""{}" varchar({}) default '{}'"#,
89                    self.field, length, self.def
90                );
91                format!(
92                    "{} --{}|{}|{}|{}|{}",
93                    sql, self.title, self.mode, self.require, self.length, self.def
94                )
95            }
96            _ => {
97                let sql = format!(
98                    "`{}` varchar({}){} default '{}'",
99                    self.field, length, not_null, self.def
100                );
101                format!(
102                    "{} comment '{}|{}|{}|{}|{}'",
103                    sql.clone(),
104                    self.title,
105                    self.mode,
106                    self.require,
107                    self.length,
108                    self.def
109                )
110            }
111        }
112    }
113    fn hide(&mut self) -> &mut Self {
114        self.show = false;
115        self
116    }
117    fn describe(&mut self, text: &str) -> &mut Self {
118        self.describe = text.to_string();
119        self
120    }
121
122    fn field(&mut self) -> JsonValue {
123        let mut field = object! {};
124        field
125            .insert("require", JsonValue::from(self.require))
126            .unwrap();
127        field
128            .insert("field", JsonValue::from(self.field.clone()))
129            .unwrap();
130        field
131            .insert("mode", JsonValue::from(self.mode.clone()))
132            .unwrap();
133        field
134            .insert("title", JsonValue::from(self.title.clone()))
135            .unwrap();
136        field
137            .insert("length", JsonValue::from(self.length))
138            .unwrap();
139        field
140            .insert("encrypt", JsonValue::from(self.encrypt))
141            .unwrap();
142        field.insert("size", JsonValue::from(self.size)).unwrap();
143        field
144            .insert("option", JsonValue::from(self.option.clone()))
145            .unwrap();
146        field.insert("show", JsonValue::from(self.show)).unwrap();
147        field
148            .insert("describe", JsonValue::from(self.describe.clone()))
149            .unwrap();
150        field
151            .insert("upload", JsonValue::from(self.upload))
152            .unwrap();
153        field
154            .insert("def", JsonValue::from(self.def.clone()))
155            .unwrap();
156        field.insert("example", self.example.clone()).unwrap();
157        field
158    }
159
160    fn swagger(&mut self) -> JsonValue {
161        object! {
162            "type": self.mode.clone(),
163            "example": self.example.clone(),
164        }
165    }
166
167    fn example(&mut self, data: JsonValue) -> &mut Self {
168        self.example = data.clone();
169        self
170    }
171}
172
173#[cfg(test)]
174mod test {
175    use crate::Field;
176    #[test]
177    fn test() {
178        let res = crate::files::Files::new(true, "str", "附件")
179            .describe("1313")
180            .upload(true)
181            .field();
182        println!("{res:#}");
183    }
184}