mycommon_utils/database/common/
res.rs1use std::fmt::Debug;
2
3use axum::{http::{header, HeaderValue, StatusCode}, response::{IntoResponse, Response}, Json};
4use serde::{Deserialize, Serialize};
5use crate::error::Error;
6
7static RES_CODE_SUCCESS: i32 = 0;
8
9#[derive(Debug, Serialize, Clone,Deserialize)]
10pub struct ListData<T> {
12 pub data: Vec<T>,
13 pub total: u64,
14 #[serde(rename(serialize = "totalPages"))]
15 pub total_pages: u64,
16 #[serde(rename(serialize = "current"))]
17 pub page_num: u64,
18 #[serde(rename(serialize = "pageSize"))]
19 pub page_size: u64,
20}
21
22#[derive(Deserialize, Clone, Debug, Serialize, Default)]
24#[serde(rename_all = "camelCase")]
25pub struct PageParams {
26 #[serde(rename(deserialize = "current"))]
27 pub page_num: Option<u64>,
28 pub page_size: Option<u64>,
29}
30
31#[derive(Debug, Serialize, Default)]
33pub struct Res<T> {
34 #[serde(rename(serialize = "errorCode"))]
35 pub code: Option<i32>,
36 pub data: Option<T>,
37 #[serde(rename(serialize = "errorMsg"))]
38 pub msg: Option<String>,
39}
40
41#[derive(Debug, Clone)] pub struct ResJsonString(pub String);
44
45#[allow(unconditional_recursion)]
46impl<T> IntoResponse for Res<T>
47where
48 T: Serialize + Send + Sync + Debug + 'static,
49{
50 fn into_response(self) -> Response {
51 let data = Self {
52 code: self.code,
53 data: self.data,
54 msg: self.msg,
55 };
56 let json_string = match serde_json::to_string(&data) {
57 Ok(v) => v,
58 Err(e) => {
59 return Response::builder()
60 .status(StatusCode::INTERNAL_SERVER_ERROR)
61 .header(header::CONTENT_TYPE, HeaderValue::from_static(mime::TEXT_PLAIN_UTF_8.as_ref()))
62 .body(e.to_string().into())
63 .unwrap();
64 }
65 };
66 let res_json_string = ResJsonString(json_string.clone());
67 let mut response = json_string.into_response();
68 response.headers_mut().insert(
69 header::CONTENT_TYPE,
70 HeaderValue::from_static("application/json"),
71 );
72 response.extensions_mut().insert(res_json_string); response
74 }
75}
76
77impl<T: Serialize> Res<T> {
78
79 pub fn result(arg: Result<T, Error>) -> Self {
80 match arg {
81 Ok(data) => Self::with_data(data),
82 Err(e) => Self {
83 code: Some(e.error_code),
84 msg: Some(e.error_msg.clone()),
85 data: None,
86 },
87 }
88 }
89
90 pub fn with_data(data: T) -> Self {
91 Self {
92 code: Some(RES_CODE_SUCCESS),
93 data: Some(data),
94 msg: Some("success".to_string()),
95 }
96 }
97 pub fn with_err(err: &str) -> Self {
98 Self {
99 code: Some(500),
100 data: None,
101 msg: Some(err.to_string()),
102 }
103 }
104
105 pub fn with_warn(err: &str) -> Self {
106 Self {
107 code: Some(601),
108 data: None,
109 msg: Some(err.to_string()),
110 }
111 }
112 pub fn with_msg(msg: &str) -> Self {
113 Self {
114 code: Some(RES_CODE_SUCCESS),
115 data: None,
116 msg: Some(msg.to_string()),
117 }
118 }
119 #[allow(dead_code)]
120 pub fn with_data_msg(data: T, msg: &str) -> Self {
121 Self {
122 code: Some(RES_CODE_SUCCESS),
123 data: Some(data),
124 msg: Some(msg.to_string()),
125 }
126 }
127 pub fn resp_json(self) -> Json<Self> {
129 Json(self) }
131}