anycms-core 0.4.0

A unified API response library supporting multiple Rust web frameworks
Documentation
#[cfg(feature = "actix")]
use actix_web::{HttpResponse, Responder};
#[cfg(feature = "actix")]
use serde::Serialize;
#[cfg(feature = "actix")]
use crate::result::{ApiResult, ErrorCode};

/// Actix-web framework integration for ApiResult
#[cfg(feature = "actix")]
impl<T: Serialize> Responder for ApiResult<T> {
    type Body = actix_web::body::BoxBody;

    fn respond_to(self, _req: &actix_web::HttpRequest) -> actix_web::HttpResponse<Self::Body> {
        // Use the error code to determine the appropriate HTTP status code
        let status = self
            .code
            .and_then(|code| ErrorCode::from_i32(code))
            .map(|err_code| err_code.to_actix_status())
            .unwrap_or_else(|| {
                // Fallback to success field if code is not set
                if self.success {
                    actix_web::http::StatusCode::OK
                } else {
                    actix_web::http::StatusCode::INTERNAL_SERVER_ERROR
                }
            });

        HttpResponse::build(status).json(self)
    }
}