df_fields/
select.rs

1use json::{JsonValue, object};
2use crate::Field;
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 dec: usize,
12    pub length: i32,
13}
14
15impl Radio {
16    pub fn new(require: bool, field: &str, title: &str, option: Vec<&str>, default: &str) -> Self {
17        Self {
18            require,
19            field: field.to_string(),
20            mode: "radio".to_string(),
21            title: title.to_string(),
22            def: default.to_string(),
23            option: option.iter().map(|c| c.to_string()).collect(),
24            dec: 0,
25            length: 0,
26        }
27    }
28}
29
30impl Field for Radio {
31    fn sql(&mut self, model: &str) -> String {
32        let mut sql = format!("{}", self.field);
33        if !self.require {
34            self.option.push("".to_string());
35        }
36        let options = format!("'{}'", self.option.join("','"));
37        match model {
38            "sqlite" => format!("{} varchar(50) default '{}'", sql.clone(), self.def),
39            _ => {
40                sql = format!("{} enum({}) default '{}'", sql.clone(), options, self.def);
41                format!("{} comment '{}|{}|{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def, self.dec, self.option.join("|"))
42            }
43        }
44    }
45    fn field(&mut self) -> JsonValue {
46        let mut field = object! {};
47        field.insert("require", JsonValue::from(self.require.clone())).unwrap();
48        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
49        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
50        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
51        field.insert("length", JsonValue::from(self.length.clone())).unwrap();
52        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
53        field.insert("dec", JsonValue::from(self.dec.clone())).unwrap();
54        field.insert("option", JsonValue::from(self.option.clone())).unwrap();
55        field
56    }
57}
58
59
60pub struct Select {
61    pub require: bool,
62    pub field: String,
63    pub mode: String,
64    pub title: String,
65    pub option: Vec<String>,
66    pub def: Vec<String>,
67    pub dec: usize,
68    pub length: i32,
69
70}
71
72impl Select {
73    pub fn new(require: bool, field: &str, title: &str, option: Vec<&str>, default: Vec<&str>) -> Self {
74        Self {
75            require,
76            field: field.to_string(),
77            mode: "select".to_string(),
78            title: title.to_string(),
79            def: default.iter().map(|c| c.to_string()).collect(),
80            dec: 0,
81            option: option.iter().map(|c| c.to_string()).collect(),
82            length: 0,
83        }
84    }
85}
86
87impl Field for Select {
88    fn sql(&mut self, model: &str) -> String {
89        let mut sql = format!("{}", self.field);
90        if !self.require {
91            self.option.push("".to_string());
92        }
93        let def = self.def.join(",");
94        let options = format!("'{}'", self.option.join("','"));
95
96        match model {
97            "sqlite" => {
98                format!("{} varchar(50) default '{}'", sql.clone(), def)
99            }
100            _ => {
101                sql = format!("{} set({}) default '{}'", sql.clone(), options, def);
102                format!("{} comment '{}|{}|{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def.join("|"), self.dec, self.option.join("|"))
103            }
104        }
105    }
106    fn field(&mut self) -> JsonValue {
107        let mut field = object! {};
108        field.insert("require", JsonValue::from(self.require.clone())).unwrap();
109        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
110        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
111        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
112        field.insert("length", JsonValue::from(self.length.clone())).unwrap();
113        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
114        field.insert("dec", JsonValue::from(self.dec.clone())).unwrap();
115        field.insert("option", JsonValue::from(self.option.clone())).unwrap();
116        field
117    }
118}