use std::str::FromStr;
use crate::error::CoolError;
pub mod error_info {
pub const NO_ENTITY: &str = "该服务没有设置实体";
pub const NO_ID: &str = "缺少ID参数";
pub const NOT_FOUND: &str = "数据不存在";
pub const VALIDATE_FAIL: &str = "参数验证失败";
pub const UNAUTHORIZED: &str = "未授权访问";
pub const FORBIDDEN: &str = "禁止访问";
pub const SERVER_ERROR: &str = "服务器内部错误";
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum ResCode {
Success = 1000,
Fail = 1001,
ValidateFail = 1002,
CoreError = 1003,
CommError = 1004,
Unauthorized = 1005,
Forbidden = 1006,
NotFound = 1007,
}
impl ResCode {
pub fn code(&self) -> i32 {
*self as i32
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CrudType {
Add,
Delete,
Update,
Page,
Info,
List,
}
impl FromStr for CrudType {
type Err = CoolError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"add" => Ok(Self::Add),
"delete" => Ok(Self::Delete),
"update" => Ok(Self::Update),
"page" => Ok(Self::Page),
"info" => Ok(Self::Info),
"list" => Ok(Self::List),
_ => Err(CoolError::validate(format!("无效的 CrudType: {}", s))),
}
}
}
impl CrudType {
pub fn as_str(&self) -> &'static str {
match self {
Self::Add => "add",
Self::Delete => "delete",
Self::Update => "update",
Self::Page => "page",
Self::Info => "info",
Self::List => "list",
}
}
pub fn description(&self) -> &'static str {
match self {
Self::Add => "新增",
Self::Delete => "删除",
Self::Update => "修改",
Self::Page => "分页查询",
Self::Info => "详情查询",
Self::List => "列表查询",
}
}
pub fn method(&self) -> &'static str {
match self {
Self::Info => "GET",
_ => "POST",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileMode {
Local,
Cloud,
Other,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CloudType {
Oss,
Cos,
Qiniu,
Aws,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DbType {
Mysql,
Postgres,
Sqlite,
}
impl DbType {
pub fn from_backend(backend: sea_orm::DatabaseBackend) -> Self {
match backend {
sea_orm::DatabaseBackend::MySql => Self::Mysql,
sea_orm::DatabaseBackend::Postgres => Self::Postgres,
sea_orm::DatabaseBackend::Sqlite => Self::Sqlite,
}
}
}