pub struct OperationResult<T = ()> {
pub success: bool,
pub status: i32,
pub message: String,
pub result: Option<T>,
}Expand description
Outcome of an operation: success flag, business status, message, and — for
OperationResult<T> — the business payload.
OperationResult (the default T = ()) has no result member at all, so
update/delete-style endpoints can report the outcome without declaring a
one-off type. The fields are public; the constructors keep success and
status agreeing with each other.
Fields§
§success: boolWhether the operation achieved its goal.
status: i32Business status code, defined by the caller (the constructors use 0
when no explicit code is given). It is not an HTTP status: the transport
layer never reads it.
message: StringHuman-readable outcome for the caller (UI toast, CLI output, logs).
result: Option<T>The business payload; omitted from JSON when absent (the unit form, or
a failed result).
Implementations§
Source§impl<T> OperationResult<T>
impl<T> OperationResult<T>
Sourcepub fn ok(result: T, message: impl Into<String>) -> OperationResult<T>
pub fn ok(result: T, message: impl Into<String>) -> OperationResult<T>
A successful result carrying result, with the default business status
0.
Sourcepub fn ok_with_status(
status: i32,
result: T,
message: impl Into<String>,
) -> OperationResult<T>
pub fn ok_with_status( status: i32, result: T, message: impl Into<String>, ) -> OperationResult<T>
A successful result carrying result and an explicit business status.
Sourcepub fn failed(status: i32, message: impl Into<String>) -> OperationResult<T>
pub fn failed(status: i32, message: impl Into<String>) -> OperationResult<T>
A failed result with the business status that explains why. No payload is attached.
Sourcepub fn with_status(self, status: i32) -> OperationResult<T>
pub fn with_status(self, status: i32) -> OperationResult<T>
Replace the business status, keeping success and the message.
Use it when the outcome is only known part-way through the operation
(result = result.with_status(1001)); the fields are public as well, so
result.status = 1001 works too.
Sourcepub fn with_message(self, message: impl Into<String>) -> OperationResult<T>
pub fn with_message(self, message: impl Into<String>) -> OperationResult<T>
Replace the message, keeping success and the business status.
Source§impl OperationResult
impl OperationResult
Sourcepub fn success(message: impl Into<String>) -> OperationResult
pub fn success(message: impl Into<String>) -> OperationResult
A successful result with no payload, and the default business status 0.
The JSON body is {"success":true,"status":0,"message":"..."}.
Sourcepub fn success_with_status(
status: i32,
message: impl Into<String>,
) -> OperationResult
pub fn success_with_status( status: i32, message: impl Into<String>, ) -> OperationResult
A successful result with no payload and an explicit business status.
Sourcepub fn with_result<T>(self, result: T) -> OperationResult<T>
pub fn with_result<T>(self, result: T) -> OperationResult<T>
Attach the business payload once it is known, turning the unit envelope
into OperationResult<T>; success, status and message
are kept.
Trait Implementations§
Source§impl<T> Clone for OperationResult<T>where
T: Clone,
impl<T> Clone for OperationResult<T>where
T: Clone,
Source§fn clone(&self) -> OperationResult<T>
fn clone(&self) -> OperationResult<T>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more