diff options
| author | Tolmachev Igor <me@igorek.dev> | 2025-09-01 13:32:05 +0300 |
|---|---|---|
| committer | Tolmachev Igor <me@igorek.dev> | 2025-09-01 13:32:05 +0300 |
| commit | b9d75e22db72aabf47815e381aa6432c1bff3877 (patch) | |
| tree | b6b0741461484c36919a3ec74fb075c77e867a59 /src/response.rs | |
| parent | 56d155ac2de9261575d7fd4671a08b95cd16e6bb (diff) | |
| download | queue_server-b9d75e22db72aabf47815e381aa6432c1bff3877.tar.gz queue_server-b9d75e22db72aabf47815e381aa6432c1bff3877.zip | |
Add account endpoints
Diffstat (limited to 'src/response.rs')
| -rw-r--r-- | src/response.rs | 56 |
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 @@ | |||
| 1 | use axum::http::StatusCode; | ||
| 1 | use axum::response::{IntoResponse, Response}; | 2 | use axum::response::{IntoResponse, Response}; |
| 2 | use serde::Serialize; | 3 | use serde::Serialize; |
| 3 | use serde_json::json; | 4 | use serde_json::json; |
| 4 | 5 | ||
| 5 | pub enum ApiResponse<T> { | 6 | pub struct SuccessResponse<T>(pub T); |
| 6 | Success(T), | 7 | pub struct FailResponse(pub String, pub String); |
| 7 | Fail(T), | 8 | pub struct ErrorResponse(pub String, pub String); |
| 8 | Error(String), | ||
| 9 | } | ||
| 10 | 9 | ||
| 11 | impl<T> IntoResponse for ApiResponse<T> | 10 | impl<T> IntoResponse for SuccessResponse<T> |
| 12 | where | 11 | where |
| 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 | |||
| 26 | impl 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 | |||
| 40 | impl 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 | } |
