1use json::{JsonValue, object};
2use crate::Field;
3
4pub struct Files {
13 pub require: bool,
14 pub field: String,
15 pub mode: String,
16 pub title: String,
17 pub length: i32,
18 pub encrypt: bool,
19 pub size: usize,
20 pub option: Vec<String>,
21 pub show: bool,
22 pub describe: String,
23 pub def: String,
24 pub example: JsonValue,
25}
26
27impl Files {
28 pub fn new(require: bool, field: &str, title: &str) -> Self {
29 Self {
30 require,
31 field: field.to_string(),
32 mode: "file".to_string(),
33 title: title.to_string(),
34 length: 1,
35 encrypt: false,
36 size: 0,
37 option: vec![],
38 def: "".to_string(),
39 show: true,
40 describe: "".to_string(),
41 example: JsonValue::Null,
42 }
43 }
44
45 pub fn encrypt(&mut self) -> &mut Self {
46 self.encrypt = true;
47 self
48 }
49 pub fn multiple(&mut self, number: i32) -> &mut Self {
50 self.length = number;
51 self
52 }
53 pub fn option(&mut self, option: Vec<&str>) -> &mut Self {
54 self.option = option.iter().map(|c| c.to_string()).collect();
55 self
56 }
57 pub fn size(&mut self, size: usize) -> &mut Self {
58 self.size = if size == 0 {
59 1024 * 1024
60 } else {
61 size
62 };
63 self
64 }
65 pub fn def(&mut self, def: &str) -> &mut Self {
66 self.def = def.to_string();
67 self
68 }
69}
70
71impl Field for Files {
72 fn sql(&mut self, model: &str) -> String {
73 let sql = format!("`{}` varchar({}) default '{}'", self.field, self.length * 64 + self.length, self.def);
74 match model {
75 "sqlite" => sql,
76 "pgsql" => {
77 let sql = format!("{} varchar({}) default '{}'", self.field, self.length * 64 + self.length, self.def);
78 format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.length, self.def)
79 },
80 _ => {
81 format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.length, self.def)
82 }
83 }
84 }
85 fn hide(&mut self) -> &mut Self {
86 self.show = false;
87 self
88 }
89 fn describe(&mut self, text: &str) -> &mut Self {
90 self.describe = text.to_string();
91 self
92 }
93
94 fn field(&mut self) -> JsonValue {
95 let mut field = object! {};
96 field.insert("require", JsonValue::from(self.require)).unwrap();
97 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
98 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
99 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
100 field.insert("length", JsonValue::from(self.length)).unwrap();
101 field.insert("encrypt", JsonValue::from(self.encrypt)).unwrap();
102 field.insert("size", JsonValue::from(self.size)).unwrap();
103 field.insert("option", JsonValue::from(self.option.clone())).unwrap();
104 field.insert("show", JsonValue::from(self.show)).unwrap();
105 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
106 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
107 field.insert("example", self.example.clone()).unwrap();
108 field
109 }
110
111 fn swagger(&mut self) -> JsonValue {
112 object! {
113 "type": self.mode.clone(),
114 "example": self.example.clone(),
115 }
116 }
117
118 fn example(&mut self, data: JsonValue) ->&mut Self {
119 self.example = data.clone();
120 self
121 }
122}
123