pub struct ApiResponse<T>where
T: Serialize,{
pub success: bool,
pub data: Option<T>,
pub message: String,
}
Expand description
Standardized API response wrapper for all outgoing JSON responses.
This struct enforces a consistent response structure across all endpoints:
{
"success": true,
"data": { ... },
"message": "Some message"
}
T
is the type of thedata
payload.success
is a boolean indicating operation status.message
provides a human-readable context string.
§Example (success):
{
"success": true,
"data": { "id": 1, "name": "Alice" },
"message": "User fetched successfully"
}
§Example (error):
{
"success": false,
"data": {},
"message": "User not found"
}
Fields§
§success: bool
§data: Option<T>
§message: String
Implementations§
Source§impl<T> ApiResponse<T>where
T: Serialize,
impl<T> ApiResponse<T>where
T: Serialize,
Sourcepub fn success(data: T, message: impl Into<String>) -> Self
pub fn success(data: T, message: impl Into<String>) -> Self
Constructs a success response with the given data and message.
§Arguments
data
: The result payload.message
: A descriptive message to accompany the success.
Sourcepub fn success_without_data(message: impl Into<String>) -> Self
pub fn success_without_data(message: impl Into<String>) -> Self
Constructs a success response with no data, only a message.
§Arguments
message
: A descriptive message to accompany the success.
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for ApiResponse<T>where
T: Freeze,
impl<T> RefUnwindSafe for ApiResponse<T>where
T: RefUnwindSafe,
impl<T> Send for ApiResponse<T>where
T: Send,
impl<T> Sync for ApiResponse<T>where
T: Sync,
impl<T> Unpin for ApiResponse<T>where
T: Unpin,
impl<T> UnwindSafe for ApiResponse<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
§impl<T> Chain<T> for T
impl<T> Chain<T> for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more