br_addon/
module.rs

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