cool_core/constant/
mod.rs1use std::str::FromStr;
6
7use crate::error::CoolError;
8
9pub mod error_info {
11 pub const NO_ENTITY: &str = "该服务没有设置实体";
13 pub const NO_ID: &str = "缺少ID参数";
15 pub const NOT_FOUND: &str = "数据不存在";
17 pub const VALIDATE_FAIL: &str = "参数验证失败";
19 pub const UNAUTHORIZED: &str = "未授权访问";
21 pub const FORBIDDEN: &str = "禁止访问";
23 pub const SERVER_ERROR: &str = "服务器内部错误";
25}
26
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
29#[repr(i32)]
30pub enum ResCode {
31 Success = 1000,
33 Fail = 1001,
35 ValidateFail = 1002,
37 CoreError = 1003,
39 CommError = 1004,
41 Unauthorized = 1005,
43 Forbidden = 1006,
45 NotFound = 1007,
47}
48
49impl ResCode {
50 pub fn code(&self) -> i32 {
52 *self as i32
53 }
54}
55
56#[derive(Debug, Clone, Copy, PartialEq, Eq)]
58pub enum CrudType {
59 Add,
61 Delete,
63 Update,
65 Page,
67 Info,
69 List,
71}
72
73impl FromStr for CrudType {
75 type Err = CoolError;
76
77 fn from_str(s: &str) -> Result<Self, Self::Err> {
79 match s.to_lowercase().as_str() {
80 "add" => Ok(Self::Add),
81 "delete" => Ok(Self::Delete),
82 "update" => Ok(Self::Update),
83 "page" => Ok(Self::Page),
84 "info" => Ok(Self::Info),
85 "list" => Ok(Self::List),
86 _ => Err(CoolError::validate(format!("无效的 CrudType: {}", s))),
87 }
88 }
89}
90
91impl CrudType {
93 pub fn as_str(&self) -> &'static str {
95 match self {
96 Self::Add => "add",
97 Self::Delete => "delete",
98 Self::Update => "update",
99 Self::Page => "page",
100 Self::Info => "info",
101 Self::List => "list",
102 }
103 }
104
105 pub fn description(&self) -> &'static str {
107 match self {
108 Self::Add => "新增",
109 Self::Delete => "删除",
110 Self::Update => "修改",
111 Self::Page => "分页查询",
112 Self::Info => "详情查询",
113 Self::List => "列表查询",
114 }
115 }
116
117 pub fn method(&self) -> &'static str {
119 match self {
120 Self::Info => "GET",
121 _ => "POST",
122 }
123 }
124}
125
126#[derive(Debug, Clone, Copy, PartialEq, Eq)]
128pub enum FileMode {
129 Local,
131 Cloud,
133 Other,
135}
136
137#[derive(Debug, Clone, Copy, PartialEq, Eq)]
139pub enum CloudType {
140 Oss,
142 Cos,
144 Qiniu,
146 Aws,
148}
149
150#[derive(Debug, Clone, Copy, PartialEq, Eq)]
152pub enum DbType {
153 Mysql,
155 Postgres,
157 Sqlite,
159}
160
161impl DbType {
162 pub fn from_backend(backend: sea_orm::DatabaseBackend) -> Self {
164 match backend {
165 sea_orm::DatabaseBackend::MySql => Self::Mysql,
166 sea_orm::DatabaseBackend::Postgres => Self::Postgres,
167 sea_orm::DatabaseBackend::Sqlite => Self::Sqlite,
168 }
169 }
170}