Skip to main content

br_fields/
float.rs

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    /// 数字
19    ///
20    /// * field 字段名
21    /// * mode 模式 string
22    /// * title 字段描述
23    /// * length 字段总长度(含小数位)
24    /// * default 默认值
25    /// * dec 小数位
26    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 not_null = if self.require { " not null" } else { "" };
53        match model {
54            "sqlite" => format!(
55                "`{}` decimal({},{}){} default {}",
56                self.field, self.length, self.dec, not_null, self.def
57            ),
58            "pgsql" => {
59                let sql = format!(
60                    r#""{}" decimal({},{}) default {}"#,
61                    self.field, self.length, self.dec, self.def
62                );
63                format!(
64                    "{} --{}|{}|{}|{}|{}|{}",
65                    sql, self.title, self.mode, self.require, self.length, self.dec, self.def
66                )
67            }
68            _ => {
69                let sql = format!(
70                    "`{}` decimal({},{}){} default {}",
71                    self.field, self.length, self.dec, not_null, self.def
72                );
73                format!(
74                    "{} comment '{}|{}|{}|{}|{}|{}'",
75                    sql.clone(),
76                    self.title,
77                    self.mode,
78                    self.require,
79                    self.length,
80                    self.dec,
81                    self.def
82                )
83            }
84        }
85    }
86    fn hide(&mut self) -> &mut Self {
87        self.show = false;
88        self
89    }
90
91    fn describe(&mut self, text: &str) -> &mut Self {
92        self.describe = text.to_string();
93        self
94    }
95
96    fn field(&mut self) -> JsonValue {
97        let mut field = object! {};
98        field
99            .insert("require", JsonValue::from(self.require))
100            .unwrap();
101        field
102            .insert("field", JsonValue::from(self.field.clone()))
103            .unwrap();
104        field
105            .insert("mode", JsonValue::from(self.mode.clone()))
106            .unwrap();
107        field
108            .insert("title", JsonValue::from(self.title.clone()))
109            .unwrap();
110        field
111            .insert("length", JsonValue::from(self.length))
112            .unwrap();
113        field.insert("dec", JsonValue::from(self.dec)).unwrap();
114        field
115            .insert("def", JsonValue::from(self.def.clone()))
116            .unwrap();
117        field.insert("show", JsonValue::from(self.show)).unwrap();
118        field
119            .insert("describe", JsonValue::from(self.describe.clone()))
120            .unwrap();
121        field.insert("example", self.example.clone()).unwrap();
122        field
123    }
124
125    fn swagger(&mut self) -> JsonValue {
126        object! {
127            "type": self.mode.clone(),
128            "example": self.example.clone(),
129        }
130    }
131
132    fn example(&mut self, data: JsonValue) -> &mut Self {
133        self.example = data.clone();
134        self
135    }
136}