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,
20 pub size: usize,
22 pub option: Vec<String>,
24 pub show: bool,
25 pub describe: String,
26 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 not_null = if self.require { " not null" } else { "" };
84 let length = self.length * 64 + self.length;
85 match model {
86 "sqlite" => format!(
87 "{} varchar({}){} default '{}'",
88 self.field, length, not_null, self.def
89 ),
90 "pgsql" => {
91 format!(
92 r#""{}" varchar({}){} default '{}'"#,
93 self.field, length, not_null, self.def
94 )
95 }
96 _ => {
97 let sql = format!("`{}` varchar({}){} default '{}'", self.field, length, not_null, self.def);
98 format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.length, self.def)
99 }
100 }
101 }
102 fn hide(&mut self) -> &mut Self {
103 self.show = false;
104 self
105 }
106 fn describe(&mut self, text: &str) -> &mut Self {
107 self.describe = text.to_string();
108 self
109 }
110
111 fn field(&mut self) -> JsonValue {
112 let mut field = object! {};
113 field.insert("require", JsonValue::from(self.require)).unwrap();
114 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
115 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
116 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
117 field.insert("length", JsonValue::from(self.length)).unwrap();
118 field.insert("encrypt", JsonValue::from(self.encrypt)).unwrap();
119 field.insert("size", JsonValue::from(self.size)).unwrap();
120 field.insert("option", JsonValue::from(self.option.clone())).unwrap();
121 field.insert("show", JsonValue::from(self.show)).unwrap();
122 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
123 field.insert("upload", JsonValue::from(self.upload)).unwrap();
124 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
125 field.insert("example", self.example.clone()).unwrap();
126 field
127 }
128
129 fn swagger(&mut self) -> JsonValue {
130 object! {
131 "type": self.mode.clone(),
132 "example": self.example.clone(),
133 }
134 }
135
136 fn example(&mut self, data: JsonValue) -> &mut Self {
137 self.example = data.clone();
138 self
139 }
140}
141
142#[cfg(test)]
143mod test {
144 use crate::Field;
145 #[test]
146 fn test() {
147 let res = crate::files::Files::new(true, "str", "附件").describe("1313").upload(true).field();
148 println!("{res:#}");
149 }
150}