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                format!(
88                    r#""{}" varchar({}){} default '{}'"#,
89                    self.field, length, not_null, self.def
90                )
91            }
92            _ => {
93                let sql = format!(
94                    "`{}` varchar({}){} default '{}'",
95                    self.field, length, not_null, self.def
96                );
97                format!(
98                    "{} comment '{}|{}|{}|{}|{}'",
99                    sql.clone(),
100                    self.title,
101                    self.mode,
102                    self.require,
103                    self.length,
104                    self.def
105                )
106            }
107        }
108    }
109    fn hide(&mut self) -> &mut Self {
110        self.show = false;
111        self
112    }
113    fn describe(&mut self, text: &str) -> &mut Self {
114        self.describe = text.to_string();
115        self
116    }
117
118    fn field(&mut self) -> JsonValue {
119        let mut field = object! {};
120        field
121            .insert("require", JsonValue::from(self.require))
122            .unwrap();
123        field
124            .insert("field", JsonValue::from(self.field.clone()))
125            .unwrap();
126        field
127            .insert("mode", JsonValue::from(self.mode.clone()))
128            .unwrap();
129        field
130            .insert("title", JsonValue::from(self.title.clone()))
131            .unwrap();
132        field
133            .insert("length", JsonValue::from(self.length))
134            .unwrap();
135        field
136            .insert("encrypt", JsonValue::from(self.encrypt))
137            .unwrap();
138        field.insert("size", JsonValue::from(self.size)).unwrap();
139        field
140            .insert("option", JsonValue::from(self.option.clone()))
141            .unwrap();
142        field.insert("show", JsonValue::from(self.show)).unwrap();
143        field
144            .insert("describe", JsonValue::from(self.describe.clone()))
145            .unwrap();
146        field
147            .insert("upload", JsonValue::from(self.upload))
148            .unwrap();
149        field
150            .insert("def", JsonValue::from(self.def.clone()))
151            .unwrap();
152        field.insert("example", self.example.clone()).unwrap();
153        field
154    }
155
156    fn swagger(&mut self) -> JsonValue {
157        object! {
158            "type": self.mode.clone(),
159            "example": self.example.clone(),
160        }
161    }
162
163    fn example(&mut self, data: JsonValue) -> &mut Self {
164        self.example = data.clone();
165        self
166    }
167}
168
169#[cfg(test)]
170mod test {
171    use crate::Field;
172    #[test]
173    fn test() {
174        let res = crate::files::Files::new(true, "str", "附件")
175            .describe("1313")
176            .upload(true)
177            .field();
178        println!("{res:#}");
179    }
180}