br_fields/
location.rs

1use json::{JsonValue, object};
2use crate::Field;
3
4/// 附件
5///
6/// * require 是否必填
7/// * field 字段名
8/// * mode 模式 file
9/// * title 字段描述
10/// * length 附件数量
11/// * def 默认值
12pub struct Location {
13    pub require: bool,
14    pub field: String,
15    pub mode: String,
16    pub title: String,
17    pub length: i32,
18    pub show: bool,
19    pub describe: String,
20    pub def: String,
21    pub example: JsonValue,
22}
23
24impl Location {
25    pub fn new(require: bool, field: &str, title: &str) -> Self {
26        Self {
27            require,
28            field: field.to_string(),
29            mode: "location".to_string(),
30            title: title.to_string(),
31            length: 39,
32            def: "".to_string(),
33            show: true,
34            describe: "".to_string(),
35            example: JsonValue::Null,
36        }
37    }
38    pub fn def(&mut self, def: &str) -> &mut Self {
39        self.def = def.to_string();
40        self
41    }
42}
43
44impl Field for Location {
45    fn sql(&mut self, model: &str) -> String {
46        let mut sql = format!("`{}` varchar({}) ", self.field, self.length);
47        if !self.require {
48            sql = format!("{} default '{}'", sql.clone(), self.def);
49        };
50        match model {
51            "sqlite" => sql,
52            "pgsql" => {
53                let mut sql = format!("{} varchar({}) ", self.field, self.length);
54                if !self.require {
55                    sql = format!("{} default '{}'", sql.clone(), self.def);
56                }
57                format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.def, self.length)
58            }
59            _ => {
60                format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.def, self.length)
61            }
62        }
63    }
64    fn hide(&mut self) -> &mut Self {
65        self.show = false;
66        self
67    }
68    fn describe(&mut self, text: &str) -> &mut Self {
69        self.describe = text.to_string();
70        self
71    }
72
73    fn field(&mut self) -> JsonValue {
74        let mut field = object! {};
75        field.insert("require", JsonValue::from(self.require)).unwrap();
76        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
77        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
78        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
79        field.insert("length", JsonValue::from(self.length)).unwrap();
80        field.insert("show", JsonValue::from(self.show)).unwrap();
81        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
82        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
83        field.insert("example", self.example.clone()).unwrap();
84        field
85    }
86
87    fn swagger(&mut self) -> JsonValue {
88        object! {
89            "type": self.mode.clone(),
90            "example": self.example.clone(),
91        }
92    }
93
94    fn example(&mut self, data: JsonValue) -> &mut Self {
95        self.example = data.clone();
96        self
97    }
98}
99