1use json::{JsonValue, object};
2use crate::Field;
3
4pub struct Float {
5 pub require: bool,
6 pub field: String,
7 pub mode: String,
8 pub title: String,
9 pub def: f32,
10 pub length: i32,
11 pub dec: usize,
12}
13
14impl Float {
15 pub fn new(require: bool, field: &str, title: &str, length: i32, dec: usize, default: f32) -> Self {
24 Self {
25 require,
26 field: field.to_string(),
27 mode: "float".to_string(),
28 title: title.to_string(),
29 def: default,
30 length,
31 dec,
32 }
33 }
34}
35
36impl Field for Float {
37 fn sql(&mut self, model: &str) -> String {
38 let mut sql = format!("{} decimal({},{})", self.field, self.length, self.dec);
39 if self.require {
40 sql = format!("{} not null", sql.clone())
41 };
42 sql = format!("{} default {1:.width$}", sql.clone(), self.def, width = self.dec);
43 match model {
44 "sqlite" => sql,
45 _ => format!("{} comment '{}|{}|{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.length, self.dec, self.def)
46 }
47 }
48 fn field(&mut self) -> JsonValue {
49 let mut field = object! {};
50 field.insert("require", JsonValue::from(self.require.clone())).unwrap();
51 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
52 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
53 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
54 field.insert("length", JsonValue::from(self.length.clone())).unwrap();
55 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
56 field.insert("dec", JsonValue::from(self.dec.clone())).unwrap();
57 field
58 }
59}