aboutsummaryrefslogtreecommitdiff
path: root/src/response.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/response.rs')
-rw-r--r--src/response.rs56
1 files changed, 38 insertions, 18 deletions
diff --git a/src/response.rs b/src/response.rs
index 8d505a5..25c3008 100644
--- a/src/response.rs
+++ b/src/response.rs
@@ -1,32 +1,52 @@
1use axum::http::StatusCode;
1use axum::response::{IntoResponse, Response}; 2use axum::response::{IntoResponse, Response};
2use serde::Serialize; 3use serde::Serialize;
3use serde_json::json; 4use serde_json::json;
4 5
5pub enum ApiResponse<T> { 6pub struct SuccessResponse<T>(pub T);
6 Success(T), 7pub struct FailResponse(pub String, pub String);
7 Fail(T), 8pub struct ErrorResponse(pub String, pub String);
8 Error(String),
9}
10 9
11impl<T> IntoResponse for ApiResponse<T> 10impl<T> IntoResponse for SuccessResponse<T>
12where 11where
13 T: Serialize, 12 T: Serialize,
14{ 13{
15 fn into_response(self) -> Response { 14 fn into_response(self) -> Response {
16 axum::Json(match self { 15 (
17 ApiResponse::Success(data) => json!({ 16 StatusCode::OK,
17 axum::Json(json!({
18 "status": "success", 18 "status": "success",
19 "data": data 19 "data": self.0
20 }), 20 })),
21 ApiResponse::Fail(data) => json!({ 21 )
22 .into_response()
23 }
24}
25
26impl IntoResponse for FailResponse {
27 fn into_response(self) -> Response {
28 (
29 StatusCode::BAD_REQUEST,
30 axum::Json(json!({
22 "status": "fail", 31 "status": "fail",
23 "data": data 32 "kind": self.0,
24 }), 33 "message": self.1
25 ApiResponse::Error(message) => json!({ 34 })),
35 )
36 .into_response()
37 }
38}
39
40impl IntoResponse for ErrorResponse {
41 fn into_response(self) -> Response {
42 (
43 StatusCode::INTERNAL_SERVER_ERROR,
44 axum::Json(json!({
26 "status": "error", 45 "status": "error",
27 "message": message 46 "kind": self.0,
28 }), 47 "message": self.1
29 }) 48 })),
30 .into_response() 49 )
50 .into_response()
31 } 51 }
32} 52}