pub struct ApiResult<T> {
pub success: bool,
pub data: Option<T>,
pub list: Option<Vec<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 a single value (mutually exclusive withlist)list: Contains a list of values (mutually exclusive withdata)message: Optional message or error descriptioncode: Optional error codepagination: Optional pagination metadata for list responsesextra: Additional metadata as key-value pairs
Fields§
§success: bool§data: Option<T>§list: Option<Vec<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 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
let result = ApiResult::success()
.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> 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