anycms_core/frameworks/
actix.rs1#[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#[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 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 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}