1use crate::Field;
2use json::{object, JsonValue};
3#[derive(Clone, Debug)]
4pub struct Float {
5 pub require: bool,
6 pub field: String,
7 pub mode: String,
8 pub title: String,
9 pub def: String,
10 pub length: i32,
11 pub dec: usize,
12 pub show: bool,
13 pub describe: String,
14 pub example: JsonValue,
15}
16
17impl Float {
18 pub fn new(
27 require: bool,
28 field: &str,
29 title: &str,
30 length: i32,
31 dec: usize,
32 default: f64,
33 ) -> Self {
34 let def = format!("{default:.dec$}");
35 Self {
36 require,
37 field: field.to_string(),
38 mode: "float".to_string(),
39 title: title.to_string(),
40 def,
41 length,
42 dec,
43 show: true,
44 describe: "".to_string(),
45 example: JsonValue::Null,
46 }
47 }
48}
49
50impl Field for Float {
51 fn sql(&mut self, model: &str) -> String {
52 let sql = format!(
53 "`{}` decimal({},{}) default {}",
54 self.field, self.length, self.dec, self.def
55 );
56 match model {
57 "sqlite" => sql,
58 "pgsql" => {
59 let sql = format!(
60 "{} decimal({},{}) default {}",
61 self.field, self.length, self.dec, self.def
62 );
63 format!(
64 "{} comment '{}|{}|{}|{}|{}|{}'",
65 sql.clone(),
66 self.title,
67 self.mode,
68 self.require,
69 self.length,
70 self.dec,
71 self.def
72 )
73 }
74 _ => {
75 let sql = format!(
76 "`{}` decimal({},{}) default {}",
77 self.field, self.length, self.dec, self.def
78 );
79 format!(
80 "{} comment '{}|{}|{}|{}|{}|{}'",
81 sql.clone(),
82 self.title,
83 self.mode,
84 self.require,
85 self.length,
86 self.dec,
87 self.def
88 )
89 }
90 }
91 }
92 fn hide(&mut self) -> &mut Self {
93 self.show = false;
94 self
95 }
96
97 fn describe(&mut self, text: &str) -> &mut Self {
98 self.describe = text.to_string();
99 self
100 }
101
102 fn field(&mut self) -> JsonValue {
103 let mut field = object! {};
104 field.insert("require", JsonValue::from(self.require)).unwrap();
105 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
106 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
107 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
108 field.insert("length", JsonValue::from(self.length)).unwrap();
109 field.insert("dec", JsonValue::from(self.dec)).unwrap();
110 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
111 field.insert("show", JsonValue::from(self.show)).unwrap();
112 field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
113 field.insert("example", self.example.clone()).unwrap();
114 field
115 }
116
117 fn swagger(&mut self) -> JsonValue {
118 object! {
119 "type": self.mode.clone(),
120 "example": self.example.clone(),
121 }
122 }
123
124 fn example(&mut self, data: JsonValue) -> &mut Self {
125 self.example = data.clone();
126 self
127 }
128}