pub struct ServiceResult<T: Serialize> {
pub status: String,
pub message: String,
pub data: Option<T>,
pub code: u16,
pub response_type: ResponseType,
/* private fields */
}Expand description
Standard success envelope: serializes as {"status","message","data"} unless
ServiceResult::stripped is used, in which case only data is serialized.
Type parameter T is the data payload. code and response_type control the
HTTP status and content type but are not serialized into the body.
Fields§
§status: StringEnvelope status label (e.g. "success").
message: StringHuman-readable message.
data: Option<T>Optional payload serialized as data.
code: u16HTTP status code for the response.
response_type: ResponseTypeContent type used when rendering the response.
Implementations§
Source§impl<T: Serialize> ServiceResult<T>
impl<T: Serialize> ServiceResult<T>
Sourcepub fn new(
status: impl Into<String>,
message: impl Into<String>,
data: Option<T>,
code: u16,
) -> Self
pub fn new( status: impl Into<String>, message: impl Into<String>, data: Option<T>, code: u16, ) -> Self
Creates a result with an explicit status label, message, optional payload, and HTTP code.
Sourcepub fn ok(message: impl Into<String>, data: T) -> Self
pub fn ok(message: impl Into<String>, data: T) -> Self
Creates a 200 success result with status "success", the given message, and data.
Sourcepub fn ok_empty(message: impl Into<String>) -> Selfwhere
T: Default,
pub fn ok_empty(message: impl Into<String>) -> Selfwhere
T: Default,
Creates a 200 success result with status "success" and no payload.
Sourcepub fn stripped(self) -> Self
pub fn stripped(self) -> Self
Serializes only the data field instead of the status/message/data envelope.
Sourcepub fn with_response_type(self, rt: ResponseType) -> Self
pub fn with_response_type(self, rt: ResponseType) -> Self
Overrides the content type used when rendering this result.
Sourcepub fn with_code(self, code: u16) -> Self
pub fn with_code(self, code: u16) -> Self
Overrides the HTTP status code used when rendering this result.
Sourcepub fn boxed(self) -> Box<dyn TypedServiceResult>
pub fn boxed(self) -> Box<dyn TypedServiceResult>
Boxes this result so one handler can return different ServiceResult<T> payload shapes.
Sourcepub fn serialize_inherent(&self) -> Result<String, Error>
pub fn serialize_inherent(&self) -> Result<String, Error>
Serializes the result body directly; the trait serialize delegates to this.
A stripped result serializes only data, otherwise the full envelope is used.
Trait Implementations§
Source§impl<T> Serialize for ServiceResult<T>
impl<T> Serialize for ServiceResult<T>
Source§impl<T: Serialize + Send + Sync> TypedServiceResult for ServiceResult<T>
impl<T: Serialize + Send + Sync> TypedServiceResult for ServiceResult<T>
Source§fn response_type(&self) -> ResponseType
fn response_type(&self) -> ResponseType
Content-Type header.Auto Trait Implementations§
impl<T> Freeze for ServiceResult<T>
impl<T> RefUnwindSafe for ServiceResult<T>where
Option<T>: RefUnwindSafe,
impl<T> Send for ServiceResult<T>
impl<T> Sync for ServiceResult<T>
impl<T> Unpin for ServiceResult<T>
impl<T> UnsafeUnpin for ServiceResult<T>where
Option<T>: UnsafeUnpin,
impl<T> UnwindSafe for ServiceResult<T>where
Option<T>: UnwindSafe,
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§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> ⓘ
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> ⓘ
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