Enum JSendResponse

Source
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

§data: Option<T>

Acts as the wrapper for any data returned by the API call. If the call returns no data, data should be set to None.

§

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

Fields

§message: String

A meaningful, end-user-readable (or at the least log-worthy) message, explaining what went wrong.

§code: Option<i64>

A numeric code corresponding to the error, if applicable.

§data: Option<T>

A generic container for any other information about the error, i.e. the conditions that caused the error, stack traces, etc.

Implementations§

Source§

impl<T> JSendResponse<T>

Source

pub fn success(data: Option<T>) -> JSendResponse<T>

Constructs the JSendResponse::Success variant.

Source

pub fn fail(data: T) -> JSendResponse<T>

Constructs the JSendResponse::Fail variant.

Source

pub fn error( message: String, code: Option<i64>, data: Option<T>, ) -> JSendResponse<T>

Constructs the JSendResponse::Error variant.

Source

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)
Source

pub fn message(&self) -> Option<&String>

Returns a reference to message for the Error variant, and None for the other variants.

Source

pub fn code(&self) -> Option<&i64>

Returns a reference to the value of codefor 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>

Source§

fn clone(&self) -> JSendResponse<T>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for JSendResponse<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de, T> Deserialize<'de> for JSendResponse<T>
where T: Deserialize<'de>,

Source§

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: Hash> Hash for JSendResponse<T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: PartialEq> PartialEq for JSendResponse<T>

Source§

fn eq(&self, other: &JSendResponse<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Serialize for JSendResponse<T>
where T: Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<T> StructuralPartialEq for JSendResponse<T>

Auto Trait Implementations§

§

impl<T> Freeze for JSendResponse<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for JSendResponse<T>
where T: RefUnwindSafe,

§

impl<T> Send for JSendResponse<T>
where T: Send,

§

impl<T> Sync for JSendResponse<T>
where T: Sync,

§

impl<T> Unpin for JSendResponse<T>
where T: Unpin,

§

impl<T> UnwindSafe for JSendResponse<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,