br_fields/
files.rs

1use json::{JsonValue, object};
2use crate::Field;
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 {
65            1024 * 1024
66        } else {
67            size
68        };
69        self
70    }
71    pub fn def(&mut self, def: &str) -> &mut Self {
72        self.def = def.to_string();
73        self
74    }
75    pub fn upload(&mut self, data: bool) -> &mut Self {
76        self.upload = data;
77        self
78    }
79}
80
81impl Field for Files {
82    fn sql(&mut self, model: &str) -> String {
83        let sql = format!("`{}` varchar({})  default '{}'", self.field, self.length * 64 + self.length, self.def);
84        match model {
85            "sqlite" => sql,
86            "pgsql" => {
87                let sql = format!("{} varchar({})  default '{}'", self.field, self.length * 64 + self.length, self.def);
88                format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.length, self.def)
89            }
90            _ => {
91                format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.length, self.def)
92            }
93        }
94    }
95    fn hide(&mut self) -> &mut Self {
96        self.show = false;
97        self
98    }
99    fn describe(&mut self, text: &str) -> &mut Self {
100        self.describe = text.to_string();
101        self
102    }
103
104    fn field(&mut self) -> JsonValue {
105        let mut field = object! {};
106        field.insert("require", JsonValue::from(self.require)).unwrap();
107        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
108        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
109        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
110        field.insert("length", JsonValue::from(self.length)).unwrap();
111        field.insert("encrypt", JsonValue::from(self.encrypt)).unwrap();
112        field.insert("size", JsonValue::from(self.size)).unwrap();
113        field.insert("option", JsonValue::from(self.option.clone())).unwrap();
114        field.insert("show", JsonValue::from(self.show)).unwrap();
115        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
116        field.insert("upload", JsonValue::from(self.upload)).unwrap();
117        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
118        field.insert("example", self.example.clone()).unwrap();
119        field
120    }
121
122    fn swagger(&mut self) -> JsonValue {
123        object! {
124            "type": self.mode.clone(),
125            "example": self.example.clone(),
126        }
127    }
128
129    fn example(&mut self, data: JsonValue) -> &mut Self {
130        self.example = data.clone();
131        self
132    }
133}
134
135#[cfg(test)]
136mod test {
137    use crate::Field;
138    #[test]
139    fn test() {
140        let res = crate::files::Files::new(true, "str", "附件").describe("1313").upload(true).field();
141        println!("{res:#}");
142    }
143}