pub enum JSendResponse<T> {
Success {
data: Option<T>,
},
Fail {
data: T,
},
Error {
message: String,
code: Option<i64>,
data: Option<T>,
},
}
Expand description
The JSendResponse
enum provides a way to model JSend compliant responses.
It supports the three JSend response types as variants: Success
, Fail
,
and Error
.
Variants§
Success
Fields
Fail
Fields
data: T
Provides the wrapper for the details of why the request failed. If the reasons for failure correspond to POST values, the response object’s keys SHOULD correspond to those POST values.
Error
Implementations§
Source§impl<T> JSendResponse<T>
impl<T> JSendResponse<T>
Sourcepub fn success(data: Option<T>) -> JSendResponse<T>
pub fn success(data: Option<T>) -> JSendResponse<T>
Constructs the JSendResponse::Success variant.
Sourcepub fn fail(data: T) -> JSendResponse<T>
pub fn fail(data: T) -> JSendResponse<T>
Constructs the JSendResponse::Fail variant.
Sourcepub fn error(
message: String,
code: Option<i64>,
data: Option<T>,
) -> JSendResponse<T>
pub fn error( message: String, code: Option<i64>, data: Option<T>, ) -> JSendResponse<T>
Constructs the JSendResponse::Error variant.
Sourcepub fn data(&self) -> Option<&T>
pub fn data(&self) -> Option<&T>
Returns a reference to the underlying Option
value if set, and None
otherwise.
This getter “flattens” the structure of the enum:
let response_with_data = JSendResponse::success(Some(data.clone()));
assert_eq!(response_with_data.data(), Some(data).as_ref());
let response_without_data = JSendResponse::success(None::<HashMap<&str, &str>>);
assert_eq!(response_without_data.data(), None)
Sourcepub fn message(&self) -> Option<&String>
pub fn message(&self) -> Option<&String>
Returns a reference to message
for the Error
variant, and None
for the other variants.
Sourcepub fn code(&self) -> Option<&i64>
pub fn code(&self) -> Option<&i64>
Returns a reference to the value of code
for the Error
variant, and
None
for the other variants.
This getter “flattens” the structure of the enum:
let response_with_code = JSendResponse::error(message.clone(), Some(code), Some(data.clone()));
assert_eq!(response_with_code.code(), Some(code).as_ref());
let response_without_code = JSendResponse::error(message.clone(), None, Some(data.clone()));
assert_eq!(response_without_code.code(), None);
Trait Implementations§
Source§impl<T: Clone> Clone for JSendResponse<T>
impl<T: Clone> Clone for JSendResponse<T>
Source§fn clone(&self) -> JSendResponse<T>
fn clone(&self) -> JSendResponse<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more