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 extra: Option<Value>,
}Expand description
API response wrapper with unified structure
§Fields
success: Indicates if the request was successfuldata: Contains either a single value or a list (viaResponseData)message: Optional message or error descriptioncode: Optional error codepagination: Optional pagination metadata for list responsesextra: Additional metadata as key-value pairs
§JSON Serialization Examples
// Single value
{ "success": true, "data": { "id": 1, "name": "Alice" } }
// List
{ "success": true, "data": [{ "id": 1 }, { "id": 2 }], "pagination": {...} }
// Empty success
{ "success": true }
// Error
{ "success": false, "message": "Not found", "code": 404 }Fields§
§success: bool§data: Option<ResponseData<T>>§message: Option<String>§code: Option<i32>§pagination: Option<ResultPagination>§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 to avoid confusion with the success field
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
§Example
use anycms_core::ApiResult;
use serde_json::json;
let result: ApiResult<()> = ApiResult::ok()
.with_extra("timestamp", json!(1234567890))
.with_extra("version", json!("1.0.0"));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
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>,
Deserialize this value from the given Serde deserializer. Read more
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, E> Into<Result<ApiResult<T>, E>> for ApiResult<T>
Conversion into Result for convenient error handling
impl<T, E> Into<Result<ApiResult<T>, E>> for ApiResult<T>
Conversion into Result for convenient error handling
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>
Available on crate feature
actix only.Actix-web framework integration for ApiResult
type Body = BoxBody
Source§fn respond_to(self, _req: &HttpRequest) -> HttpResponse<Self::Body>
fn respond_to(self, _req: &HttpRequest) -> HttpResponse<Self::Body>
Convert self to
HttpResponse.Auto Trait Implementations§
impl<T> Freeze for ApiResult<T>where
T: Freeze,
impl<T> RefUnwindSafe for ApiResult<T>where
T: RefUnwindSafe,
impl<T> Send for ApiResult<T>where
T: Send,
impl<T> Sync for ApiResult<T>where
T: Sync,
impl<T> Unpin for ApiResult<T>where
T: Unpin,
impl<T> UnwindSafe for ApiResult<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more