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