1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
};
use serde::Serialize;
use serde_json::json;
pub struct SuccessResponse<T>(pub T);
pub struct FailResponse(pub String, pub String);
pub struct ErrorResponse(pub String, pub String);
impl<T> IntoResponse for SuccessResponse<T>
where
T: Serialize,
{
fn into_response(self) -> Response {
(
StatusCode::OK,
axum::Json(json!({
"status": "success",
"data": self.0
})),
)
.into_response()
}
}
impl IntoResponse for FailResponse {
fn into_response(self) -> Response {
(
StatusCode::BAD_REQUEST,
axum::Json(json!({
"status": "fail",
"kind": self.0,
"message": self.1
})),
)
.into_response()
}
}
impl IntoResponse for ErrorResponse {
fn into_response(self) -> Response {
(
StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(json!({
"status": "error",
"kind": self.0,
"message": self.1
})),
)
.into_response()
}
}
|