diff --git a/library/src/res/mod.rs b/library/src/res/mod.rs index 7d49cb5..d1f472c 100644 --- a/library/src/res/mod.rs +++ b/library/src/res/mod.rs @@ -1,4 +1,3 @@ pub mod response; -pub mod status; pub mod pageable; pub mod validator; \ No newline at end of file diff --git a/library/src/res/response.rs b/library/src/res/response.rs index 56c1776..4452a45 100644 --- a/library/src/res/response.rs +++ b/library/src/res/response.rs @@ -1,4 +1,4 @@ -use std::{error::Error as StdError, fmt::Display}; +use std::{error::Error as StdError, fmt::Display, sync::Arc}; use axum::{ response::{IntoResponse, Response}, @@ -7,7 +7,17 @@ use axum::{ use http::StatusCode; use serde::Serialize; -use super::status::Status; +#[derive(Serialize)] +pub struct Reply +where + T: Serialize, +{ + pub code: i32, + pub err: bool, + pub msg: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub data: Option, +} pub struct ResData(pub Option) where @@ -19,9 +29,14 @@ where { fn into_response(self) -> Response { let ResData(data) = self; - let status = Status::OK(data); + let reply = Reply { + code: 0, + err: false, + msg: String::from("OK"), + data, + }; - Json(status.to_reply()).into_response() + Json(reply).into_response() } } @@ -52,71 +67,77 @@ use ResErr::*; impl IntoResponse for ResErr { fn into_response(self) -> Response { let status = match self { - Error(code, msg) => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, msg), + Error(code, msg) => (StatusCode::INTERNAL_SERVER_ERROR, code, msg), ErrParams(msg) => { let code = 10000; match msg { - Some(v) => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, v), - None => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, String::from("参数错误")), + Some(v) => (StatusCode::BAD_REQUEST, code, v), + None => (StatusCode::BAD_REQUEST, code, String::from("参数错误")), } } ErrAuth(msg) => { let code = 20000; match msg { - Some(v) => Status::<()>::Err(StatusCode::UNAUTHORIZED, code, v), - None => Status::<()>::Err(StatusCode::UNAUTHORIZED, code, String::from("未授权,请先登录")), + Some(v) => (StatusCode::UNAUTHORIZED, code, v), + None => (StatusCode::UNAUTHORIZED, code, String::from("未授权,请先登录")), } } ErrPerm(msg) => { let code = 30000; match msg { - Some(v) => Status::<()>::Err(StatusCode::FORBIDDEN, code, v), - None => Status::<()>::Err(StatusCode::FORBIDDEN, code, String::from("权限不足")), + Some(v) => (StatusCode::FORBIDDEN, code, v), + None => (StatusCode::FORBIDDEN, code, String::from("权限不足")), } } ErrNotFound(msg) => { let code = 40000; match msg { - Some(v) => Status::<()>::Err(StatusCode::NOT_FOUND, code, v), - None => Status::<()>::Err(StatusCode::NOT_FOUND, code, String::from("数据不存在")), + Some(v) => (StatusCode::NOT_FOUND, code, v), + None => (StatusCode::NOT_FOUND, code, String::from("数据不存在")), } } ErrService(msg) => { let code = 50000; match msg { - Some(v) => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, v), - None => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, String::from("内部服务器错误,请稍后重试")), + Some(v) => (StatusCode::INTERNAL_SERVER_ERROR, code, v), + None => (StatusCode::INTERNAL_SERVER_ERROR, code, String::from("内部服务器错误,请稍后重试")), } } ErrSocial(msg) => { let code = 80000; match msg { - Some(v) => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, v), - None => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, String::from("社交业务异常")), + Some(v) => (StatusCode::INTERNAL_SERVER_ERROR, code, v), + None => (StatusCode::INTERNAL_SERVER_ERROR, code, String::from("社交业务异常")), } } ErrAccountDisable(msg) => { let code = 90000; match msg { - Some(v) => Status::<()>::Err(StatusCode::FORBIDDEN, code, v), - None => Status::<()>::Err(StatusCode::FORBIDDEN, code, String::from("账户已禁用")), + Some(v) => (StatusCode::FORBIDDEN, code, v), + None => (StatusCode::FORBIDDEN, code, String::from("账户已禁用")), } } ErrSqlx(msg) => { let code = 90000; match msg { - Some(v) => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, v), - None => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, String::from("业务错误")), + Some(v) => (StatusCode::INTERNAL_SERVER_ERROR, code, v), + None => (StatusCode::INTERNAL_SERVER_ERROR, code, String::from("业务错误")), } } }; - Json(status.to_reply()).into_response() + let reply: Reply<()> = Reply { + code: status.1, + err: false, + msg: status.2, + data: None, + }; + (status.0, Json(reply)).into_response() } } diff --git a/library/src/res/status.rs b/library/src/res/status.rs deleted file mode 100644 index 0a8eb7c..0000000 --- a/library/src/res/status.rs +++ /dev/null @@ -1,53 +0,0 @@ -use http::StatusCode; -use serde::Serialize; - -#[derive(Serialize)] -pub struct Reply -where - T: Serialize, -{ - #[serde(skip_serializing)] - pub status: StatusCode, - pub code: i32, - pub err: bool, - pub msg: String, - #[serde(skip_serializing_if = "Option::is_none")] - pub data: Option, -} - -pub enum Status -where - T: Serialize, -{ - OK(Option), - Err(StatusCode, i32, String), -} - -impl Status -where - T: Serialize, -{ - pub fn to_reply(self) -> Reply { - let mut resp = Reply { - status: StatusCode::OK, - code: 0, - err: false, - msg: String::from("OK"), - data: None, - }; - - match self { - Status::OK(data) => { - resp.data = data; - } - Status::Err(status, code, msg) => { - resp.status = status; - resp.code = code; - resp.err = true; - resp.msg = msg; - } - } - - resp - } -}