Skip to main content

cool_plugin/
constant.rs

1//! 插件常量定义
2//!
3//! 对应 TypeScript 版本的 `constant/global.ts`
4
5/// 返回码
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7pub enum ResCode {
8    /// 成功
9    Success = 1000,
10    /// 失败
11    CommFail = 1001,
12    /// 参数验证失败
13    ValidateFail = 1002,
14    /// 核心异常
15    CoreFail = 1003,
16}
17
18impl ResCode {
19    /// 获取返回码数值
20    pub fn as_u32(self) -> u32 {
21        self as u32
22    }
23}
24
25/// 返回信息
26#[derive(Debug, Clone)]
27pub struct ResMessage;
28
29impl ResMessage {
30    /// 成功
31    pub const SUCCESS: &'static str = "success";
32    /// 失败
33    pub const COMM_FAIL: &'static str = "comm fail";
34    /// 参数验证失败
35    pub const VALIDATE_FAIL: &'static str = "validate fail";
36    /// 核心异常
37    pub const CORE_FAIL: &'static str = "core fail";
38}
39
40/// 错误提示
41#[derive(Debug, Clone)]
42pub struct ErrInfo;
43
44impl ErrInfo {
45    /// 未设置操作实体
46    pub const NO_ENTITY: &'static str = "未设置操作实体";
47    /// 查询参数[id]不存在
48    pub const NO_ID: &'static str = "查询参数[id]不存在";
49    /// 排序参数不正确
50    pub const SORT_FIELD: &'static str = "排序参数不正确";
51}
52
53/// 事件名称
54#[derive(Debug, Clone)]
55pub struct Event;
56
57impl Event {
58    /// 软删除
59    pub const SOFT_DELETE: &'static str = "onSoftDelete";
60    /// 服务成功启动
61    pub const SERVER_READY: &'static str = "onServerReady";
62    /// 服务就绪
63    pub const READY: &'static str = "onReady";
64    /// ES 数据改变
65    pub const ES_DATA_CHANGE: &'static str = "esDataChange";
66}
67
68/// 全局配置
69///
70/// 对应 TypeScript 版本的 `GlobalConfig`
71pub struct GlobalConfig {
72    res_code: ResCodeConfig,
73    res_message: ResMessageConfig,
74}
75
76pub struct ResCodeConfig {
77    success: u32,
78    comm_fail: u32,
79    validate_fail: u32,
80    core_fail: u32,
81}
82
83pub struct ResMessageConfig {
84    success: String,
85    comm_fail: String,
86    validate_fail: String,
87    core_fail: String,
88}
89
90impl GlobalConfig {
91    /// 获取单例实例
92    pub fn get_instance() -> &'static GlobalConfig {
93        static INSTANCE: once_cell::sync::Lazy<GlobalConfig> =
94            once_cell::sync::Lazy::new(|| GlobalConfig {
95                res_code: ResCodeConfig {
96                    success: 1000,
97                    comm_fail: 1001,
98                    validate_fail: 1002,
99                    core_fail: 1003,
100                },
101                res_message: ResMessageConfig {
102                    success: "success".to_string(),
103                    comm_fail: "comm fail".to_string(),
104                    validate_fail: "validate fail".to_string(),
105                    core_fail: "core fail".to_string(),
106                },
107            });
108        &INSTANCE
109    }
110
111    /// 获取返回码配置
112    pub fn res_code(&self) -> &ResCodeConfig {
113        &self.res_code
114    }
115
116    /// 获取返回信息配置
117    pub fn res_message(&self) -> &ResMessageConfig {
118        &self.res_message
119    }
120}
121
122impl ResCodeConfig {
123    pub fn success(&self) -> u32 {
124        self.success
125    }
126
127    pub fn comm_fail(&self) -> u32 {
128        self.comm_fail
129    }
130
131    pub fn validate_fail(&self) -> u32 {
132        self.validate_fail
133    }
134
135    pub fn core_fail(&self) -> u32 {
136        self.core_fail
137    }
138}
139
140impl ResMessageConfig {
141    pub fn success(&self) -> &str {
142        &self.success
143    }
144
145    pub fn comm_fail(&self) -> &str {
146        &self.comm_fail
147    }
148
149    pub fn validate_fail(&self) -> &str {
150        &self.validate_fail
151    }
152
153    pub fn core_fail(&self) -> &str {
154        &self.core_fail
155    }
156}