actix_response/
lib.rs

1pub use actix_response_macro::ResponseError;
2use actix_web::{HttpResponse, Responder};
3use serde::Serialize;
4use utoipa::ToSchema;
5
6#[derive(Serialize)]
7#[serde(tag = "type", content = "data")]
8pub enum ApiResponse<T> {
9    #[serde(rename = "success")]
10    Success(T),
11    #[serde(rename = "error")]
12    Error(String),
13}
14
15#[derive(Serialize, ToSchema)]
16pub struct ApiSuccessModel<T: ToSchema> {
17    #[serde(rename = "type")]
18    pub r#type: String,
19    pub data: T,
20}
21
22#[derive(Serialize, ToSchema)]
23pub struct ApiErrorModel {
24    #[serde(rename = "type")]
25    pub r#type: String,
26    pub data: String,
27}
28
29impl<T: Serialize> Responder for ApiResponse<T> {
30    type Body = actix_web::body::BoxBody;
31    fn respond_to(self, _req: &actix_web::HttpRequest) -> HttpResponse<Self::Body> {
32        HttpResponse::Ok().json(self)
33    }
34}