anycms_core/frameworks/
actix.rs

1#[cfg(feature = "actix")]
2use actix_web::{HttpResponse, Responder};
3#[cfg(feature = "actix")]
4use serde::Serialize;
5#[cfg(feature = "actix")]
6use crate::result::{ApiResult, ErrorCode};
7
8/// Actix-web framework integration for ApiResult
9#[cfg(feature = "actix")]
10impl<T: Serialize> Responder for ApiResult<T> {
11    type Body = actix_web::body::BoxBody;
12
13    fn respond_to(self, _req: &actix_web::HttpRequest) -> actix_web::HttpResponse<Self::Body> {
14        // Use the error code to determine the appropriate HTTP status code
15        let status = self
16            .code
17            .and_then(|code| ErrorCode::from_i32(code))
18            .map(|err_code| err_code.to_actix_status())
19            .unwrap_or_else(|| {
20                // Fallback to success field if code is not set
21                if self.success {
22                    actix_web::http::StatusCode::OK
23                } else {
24                    actix_web::http::StatusCode::INTERNAL_SERVER_ERROR
25                }
26            });
27
28        HttpResponse::build(status).json(self)
29    }
30}