#[cfg(feature = "actix")]
use actix_web::{HttpResponse, Responder};
#[cfg(feature = "actix")]
use serde::Serialize;
#[cfg(feature = "actix")]
use crate::result::{ApiResult, ErrorCode};
#[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> {
let status = self
.code
.and_then(|code| ErrorCode::from_i32(code))
.map(|err_code| err_code.to_actix_status())
.unwrap_or_else(|| {
if self.success {
actix_web::http::StatusCode::OK
} else {
actix_web::http::StatusCode::INTERNAL_SERVER_ERROR
}
});
HttpResponse::build(status).json(self)
}
}