aboutsummaryrefslogtreecommitdiff
path: root/src/response.rs
blob: 25c300834cc18b2b61a04ef4a4c14b1d930f618b (plain)
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
use axum::http::StatusCode;
use axum::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()
    }
}