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                format!(
60                    r#""{}" decimal({},{}){} default {}"#,
61                    self.field, self.length, self.dec, not_null, self.def
62                )
63            }
64            _ => {
65                let sql = format!(
66                    "`{}` decimal({},{}){} default {}",
67                    self.field, self.length, self.dec, not_null, self.def
68                );
69                format!(
70                    "{} comment '{}|{}|{}|{}|{}|{}'",
71                    sql.clone(),
72                    self.title,
73                    self.mode,
74                    self.require,
75                    self.length,
76                    self.dec,
77                    self.def
78                )
79            }
80        }
81    }
82    fn hide(&mut self) -> &mut Self {
83        self.show = false;
84        self
85    }
86
87    fn describe(&mut self, text: &str) -> &mut Self {
88        self.describe = text.to_string();
89        self
90    }
91
92    fn field(&mut self) -> JsonValue {
93        let mut field = object! {};
94        field.insert("require", JsonValue::from(self.require)).unwrap();
95        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
96        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
97        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
98        field.insert("length", JsonValue::from(self.length)).unwrap();
99        field.insert("dec", JsonValue::from(self.dec)).unwrap();
100        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
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        object! {
109            "type": self.mode.clone(),
110            "example": self.example.clone(),
111        }
112    }
113
114    fn example(&mut self, data: JsonValue) -> &mut Self {
115        self.example = data.clone();
116        self
117    }
118}