diff options
Diffstat (limited to 'src/response/success.rs')
| -rw-r--r-- | src/response/success.rs | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/response/success.rs b/src/response/success.rs new file mode 100644 index 0000000..c2ec4e5 --- /dev/null +++ b/src/response/success.rs | |||
| @@ -0,0 +1,33 @@ | |||
| 1 | use axum::{ | ||
| 2 | http::StatusCode, | ||
| 3 | response::{IntoResponse, Response}, | ||
| 4 | }; | ||
| 5 | use serde::Serialize; | ||
| 6 | use utoipa::ToSchema; | ||
| 7 | |||
| 8 | #[derive(Serialize, ToSchema)] | ||
| 9 | enum SuccessStatus { | ||
| 10 | #[serde(rename = "success")] | ||
| 11 | Success, | ||
| 12 | } | ||
| 13 | |||
| 14 | #[derive(Serialize, ToSchema)] | ||
| 15 | pub struct SuccessResponse<T> { | ||
| 16 | status: SuccessStatus, | ||
| 17 | data: T, | ||
| 18 | } | ||
| 19 | |||
| 20 | impl<T> SuccessResponse<T> { | ||
| 21 | pub fn ok(data: T) -> Self { | ||
| 22 | Self { | ||
| 23 | status: SuccessStatus::Success, | ||
| 24 | data, | ||
| 25 | } | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | impl<T: Serialize> IntoResponse for SuccessResponse<T> { | ||
| 30 | fn into_response(self) -> Response { | ||
| 31 | (StatusCode::OK, axum::Json(self)).into_response() | ||
| 32 | } | ||
| 33 | } | ||
