pub struct ApiResult<T> {
pub success: bool,
pub data: Option<ResponseData<T>>,
pub message: Option<String>,
pub code: Option<i32>,
pub pagination: Option<ResultPagination>,
pub errors: Option<Vec<FieldError>>,
pub trace_id: Option<String>,
pub biz_code: Option<i32>,
pub timestamp: Option<i64>,
pub extra: Option<Value>,
}Expand description
API response wrapper with unified structure
Fields§
§success: bool§data: Option<ResponseData<T>>§message: Option<String>§code: Option<i32>§pagination: Option<ResultPagination>§errors: Option<Vec<FieldError>>§trace_id: Option<String>§biz_code: Option<i32>§timestamp: Option<i64>§extra: Option<Value>Implementations§
Source§impl<T> ApiResult<T>
impl<T> ApiResult<T>
Sourcepub fn success() -> Self
pub fn success() -> Self
Create a successful response without data
Alias for ok() - use ok() for clarity
Sourcepub fn validation_errors(errors: Vec<FieldError>) -> Self
pub fn validation_errors(errors: Vec<FieldError>) -> Self
Create a validation error response with field-level details
Sourcepub fn from_result<E: Display>(result: Result<T, E>) -> Self
pub fn from_result<E: Display>(result: Result<T, E>) -> Self
Convert a Result<T, E> into ApiResult<T>, mapping all errors to InternalError
Sourcepub fn is_success(&self) -> bool
pub fn is_success(&self) -> bool
Whether this is a successful response
Sourcepub fn error_code(&self) -> Option<ErrorCode>
pub fn error_code(&self) -> Option<ErrorCode>
Get the error code if this is an error response
Sourcepub fn with_extra(self, key: &str, value: Value) -> Self
pub fn with_extra(self, key: &str, value: Value) -> Self
Add extra metadata to the response
Sourcepub fn with_pagination(self, pagination: ResultPagination) -> Self
pub fn with_pagination(self, pagination: ResultPagination) -> Self
Set pagination metadata for list responses
Sourcepub fn with_message(self, message: &str) -> Self
pub fn with_message(self, message: &str) -> Self
Set message for the response
Sourcepub fn with_errors(self, errors: Vec<FieldError>) -> Self
pub fn with_errors(self, errors: Vec<FieldError>) -> Self
Set field-level validation errors
Sourcepub fn with_error(
self,
field: impl Into<String>,
message: impl Into<String>,
) -> Self
pub fn with_error( self, field: impl Into<String>, message: impl Into<String>, ) -> Self
Add a single field-level validation error
Sourcepub fn with_trace_id(self, id: impl Into<String>) -> Self
pub fn with_trace_id(self, id: impl Into<String>) -> Self
Set request trace ID
Sourcepub fn with_biz_code(self, code: i32) -> Self
pub fn with_biz_code(self, code: i32) -> Self
Set business error code (independent of HTTP status code)
Sourcepub fn with_timestamp(self, ts: i64) -> Self
pub fn with_timestamp(self, ts: i64) -> Self
Set timestamp (Unix milliseconds)
Sourcepub fn with_current_timestamp(self) -> Self
pub fn with_current_timestamp(self) -> Self
Set timestamp to current time automatically
Trait Implementations§
Source§impl<'de, T> Deserialize<'de> for ApiResult<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for ApiResult<T>where
T: Deserialize<'de>,
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<T, E> From<ApiResult<T>> for Result<ApiResult<T>, E>
Convert ApiResult<T> into Result<ApiResult<T>, E> for any error type E.
impl<T, E> From<ApiResult<T>> for Result<ApiResult<T>, E>
Convert ApiResult<T> into Result<ApiResult<T>, E> for any error type E.
This allows ApiResult::value(data).into() to work as a shorthand for
Ok(ApiResult::value(data)) in handlers returning DefaultResult<impl Responder>.
The conversion always produces Ok — the Err variant is never used because
ApiResult itself encodes success/failure in its success field.
Source§impl<T> From<FromUtf8Error> for ApiResult<T>
impl<T> From<FromUtf8Error> for ApiResult<T>
Source§fn from(err: FromUtf8Error) -> Self
fn from(err: FromUtf8Error) -> Self
Source§impl<T, E: Display + Debug> From<Result<T, E>> for ApiResult<T>
Convert any Result<T, E> where E implements Display to ApiResult<T>
impl<T, E: Display + Debug> From<Result<T, E>> for ApiResult<T>
Convert any Result<T, E> where E implements Display to ApiResult<T>
Source§impl<T: Serialize> Responder for ApiResult<T>
Available on crate feature actix only.Actix-web framework integration for ApiResult.
impl<T: Serialize> Responder for ApiResult<T>
actix only.Actix-web framework integration for ApiResult.
When ApiResultLayer middleware is active, the trace_id is automatically
injected from request extensions into the response.
type Body = BoxBody
Source§fn respond_to(self, req: &HttpRequest) -> HttpResponse<Self::Body>
fn respond_to(self, req: &HttpRequest) -> HttpResponse<Self::Body>
HttpResponse.