br_addon/
module.rs

1use crate::action::Action;
2use crate::Tools;
3use crate::PLUGIN_TOOLS;
4use json::{array, object, JsonValue};
5use std::any::type_name;
6/// 模型配置
7pub trait Module {
8    /// =================基础配置=============================
9    /// 插件名称
10    fn _name(&self) -> &'static str {
11        type_name::<Self>().split("::").collect::<Vec<&str>>()[3].to_lowercase().leak()
12    }
13    /// 模型标题
14    fn title(&self) -> &'static str;
15    /// 模型描述
16    fn description(&self) -> &'static str {
17        ""
18    }
19    /// =================视图配置=============================
20    /// 模型icon
21    fn icon(&self) -> &'static str {
22        ""
23    }
24    /// =================数据库配置=============================
25    /// 是否数据表
26    fn table(&self) -> bool {
27        false
28    }
29    /// 数据库表名称
30    fn _table_name(&self) -> &'static str {
31        let t = type_name::<Self>().split("::").collect::<Vec<&str>>();
32        format!("{}_{}", t[2], t[3]).leak()
33    }
34    /// 主键
35    fn table_key(&mut self) -> &'static str {
36        "id"
37    }
38    /// 唯一索引
39    fn table_unique(&self) -> &'static [&'static str] {
40        &[]
41    }
42    /// 查询索引
43    fn table_index(&mut self) -> &'static [&'static [&'static str]] {
44        &[]
45    }
46    /// 是否分区
47    fn table_partition(&mut self) -> bool {
48        false
49    }
50    /// 分区配置
51    /// *列名
52    /// *分区key
53    /// *实例 array![str, array![range1, range2, range3...]]
54    /// *如果在已有分区的情况下想要修改分区的参照列,先要将当前分区代码删除后刷库,然后写新的分区规则再刷库
55    fn table_partition_columns(&mut self) -> JsonValue {
56        array![]
57    }
58    /// =================功能配置=============================
59    /// 加载功能
60    /// * name 功能名称
61    fn action(&mut self, name: &str) -> Result<Box<dyn Action>, String>;
62    /// 模型字段
63    fn fields(&mut self) -> JsonValue {
64        object! {}
65    }
66    /// 使用工具
67    fn tools(&mut self) -> Tools {
68        let tools = PLUGIN_TOOLS.lock().unwrap();
69        let tools = tools.get("tools").unwrap().clone();
70        tools
71    }
72}