br_fields/
select.rs

1use crate::Field;
2use json::{object, JsonValue};
3
4pub struct Radio {
5    pub require: bool,
6    pub field: String,
7    pub mode: String,
8    pub title: String,
9    pub def: String,
10    pub option: Vec<String>,
11    pub length: i32,
12    pub show: bool,
13    pub describe: String,
14    pub example: JsonValue,
15    table_name: String,
16}
17
18impl Radio {
19    pub fn new(require: bool, field: &str, title: &str, option: Vec<&str>, default: &str) -> Self {
20        Self {
21            require,
22            field: field.to_string(),
23            mode: "radio".to_string(),
24            title: title.to_string(),
25            def: default.to_string(),
26            option: option.iter().map(|c| c.to_string()).collect(),
27            length: 0,
28            show: true,
29            describe: "".to_string(),
30            example: JsonValue::Null,
31            table_name: "".to_string(),
32        }
33    }
34    pub fn table_name(&mut self, table_name: &str) -> &mut Self {
35        self.table_name = table_name.to_string();
36        self
37    }
38}
39
40impl Field for Radio {
41    fn sql(&mut self, model: &str) -> String {
42        if !self.require && self.def.is_empty() {
43            self.option.push("".to_string());
44        }
45        match model {
46            "sqlite" => {
47                format!(
48                    "`{}` varchar({})  default '{}'",
49                    self.field,
50                    self.option.join("").len() + self.option.len(),
51                    self.def
52                )
53            }
54            "pgsql" => {
55                let sql = format!(
56                    r#""{}" varchar({})  default '{}'"#,
57                    self.field, self.option.join("").len() + self.option.len(), self.def
58                );
59                format!(
60                    "{} comment '{}|{}|{}|{}|{}|{}'",
61                    sql.clone(),
62                    self.mode,
63                    self.require,
64                    self.title,
65                    self.length,
66                    self.def,
67                    self.option.join("|")
68                )
69            }
70            _ => {
71                let sql = format!(
72                    "`{}` set('{}')  default '{}'",
73                    self.field,
74                    self.option.join("','"),
75                    self.def
76                );
77                format!(
78                    "{} comment '{}|{}|{}|{}|{}|{}'",
79                    sql.clone(),
80                    self.mode,
81                    self.require,
82                    self.title,
83                    self.length,
84                    self.def,
85                    self.option.join("|")
86                )
87            }
88        }
89    }
90    fn hide(&mut self) -> &mut Self {
91        self.show = false;
92        self
93    }
94    fn describe(&mut self, text: &str) -> &mut Self {
95        self.describe = text.to_string();
96        self
97    }
98    fn field(&mut self) -> JsonValue {
99        let mut field = object! {};
100        field.insert("require", JsonValue::from(self.require)).unwrap();
101        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
102        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
103        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
104        field.insert("length", JsonValue::from(self.length)).unwrap();
105        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
106
107        field.insert("option", JsonValue::from(self.option.clone())).unwrap();
108
109        field.insert("show", JsonValue::from(self.show)).unwrap();
110        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
111        field.insert("example", self.example.clone()).unwrap();
112        field
113    }
114
115    fn swagger(&mut self) -> JsonValue {
116        self.mode = match self.mode.as_str() {
117            "radio" => "string",
118            _ => self.mode.as_str(),
119        }.to_string();
120        object! {
121            "type": self.mode.clone(),
122            "example": self.example.clone(),
123        }
124    }
125
126    fn example(&mut self, data: JsonValue) -> &mut Self {
127        self.example = data.clone();
128        self
129    }
130}
131
132pub struct Select {
133    pub require: bool,
134    pub field: String,
135    pub mode: String,
136    pub title: String,
137    pub option: Vec<String>,
138    pub def: Vec<String>,
139    pub length: i32,
140    pub show: bool,
141    pub describe: String,
142    pub example: JsonValue,
143    pub table_name: String,
144}
145
146impl Select {
147    pub fn new(
148        require: bool,
149        field: &str,
150        title: &str,
151        option: Vec<&str>,
152        default: Vec<&str>,
153    ) -> Self {
154        Self {
155            require,
156            field: field.to_string(),
157            mode: "select".to_string(),
158            title: title.to_string(),
159            def: default.iter().map(|c| c.to_string()).collect(),
160            option: option.iter().map(|c| c.to_string()).collect(),
161            length: 0,
162            show: true,
163            describe: "".to_string(),
164            example: JsonValue::Null,
165            table_name: String::new(),
166        }
167    }
168    pub fn table_name(&mut self, table_name: &str) -> &mut Self {
169        self.table_name = table_name.to_string();
170        self
171    }
172}
173
174impl Field for Select {
175    fn sql(&mut self, model: &str) -> String {
176        if !self.require && self.def.contains(&"".to_string()) {
177            self.option.push("".to_string());
178        }
179        match model {
180            "sqlite" => {
181                format!(
182                    "`{}` varchar({})  default '{}'",
183                    self.field,
184                    self.option.join("").len() + self.option.len(),
185                    if self.def.is_empty() {
186                        "".to_string()
187                    } else {
188                        self.def.join(",")
189                    }
190                )
191            }
192            "pgsql" => {
193                let sql = format!("{} varchar({})  default '{}'", self.field, self.option.join("").len() + self.option.len(), self.def.join(","));
194                format!("{} comment '{}|{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def.join("|"), self.option.join("|"))
195            }
196            _ => {
197                let sql = format!(
198                    "`{}` set('{}')  default '{}'",
199                    self.field,
200                    self.option.join("','"),
201                    self.def.join(",")
202                );
203                format!(
204                    "{} comment '{}|{}|{}|{}|{}|{}'",
205                    sql.clone(),
206                    self.mode,
207                    self.require,
208                    self.title,
209                    self.length,
210                    self.def.join("|"),
211                    self.option.join("|")
212                )
213            }
214        }
215    }
216    fn hide(&mut self) -> &mut Self {
217        self.show = false;
218        self
219    }
220    fn describe(&mut self, text: &str) -> &mut Self {
221        self.describe = text.to_string();
222        self
223    }
224
225    fn field(&mut self) -> JsonValue {
226        let mut field = object! {};
227        field.insert("require", JsonValue::from(self.require)).unwrap();
228        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
229        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
230        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
231        field.insert("length", JsonValue::from(self.length)).unwrap();
232
233        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
234        field.insert("option", JsonValue::from(self.option.clone())).unwrap();
235
236        field.insert("show", JsonValue::from(self.show)).unwrap();
237        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
238        field.insert("example", self.example.clone()).unwrap();
239        field
240    }
241
242    fn swagger(&mut self) -> JsonValue {
243        object! {
244            "type": self.mode.clone(),
245            "example": self.example.clone(),
246        }
247    }
248
249    fn example(&mut self, data: JsonValue) -> &mut Self {
250        self.example = data.clone();
251        self
252    }
253}