JsonResult

Enum JsonResult 

Source
pub enum JsonResult<T, E> {
    Ok(T),
    Err(E),
}
Expand description

A generic enum representing a JSON result that can either be a success (Ok) with a value of type T or an error (Err) with a value of type E.

This enum is designed to be serialized and deserialized using Serde’s untagged enum representation, allowing it to seamlessly handle JSON values that could match either type.

Variants§

§

Ok(T)

Variant representing a successful result containing a value of type T.

§

Err(E)

Variant representing an error result containing a value of type E.

Trait Implementations§

Source§

impl<T: Debug, E: Debug> Debug for JsonResult<T, E>

Source§

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

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

impl<'de, T, E> Deserialize<'de> for JsonResult<T, E>
where T: Deserialize<'de>, E: 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, E> From<JsonResult<T, E>> for Value
where T: Serialize, E: Serialize,

Source§

fn from(res: JsonResult<T, E>) -> Self

Converts a JsonResult<T, E> into a serde_json::Value.

Serializes the contained value in either the Ok or Err variant into a JSON value.

§Examples
let res: JsonResult<i32, String> = JsonResult::Ok(42);
let json_value: serde_json::Value = res.into();
assert_eq!(json_value, json!(42));
Source§

impl<T, E> From<Result<T, E>> for JsonResult<T, E>

Source§

fn from(r: Result<T, E>) -> Self

Converts to this type from the input type.
Source§

impl<T, E> Serialize for JsonResult<T, E>
where T: Serialize, E: 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, E> TryFrom<Value> for JsonResult<T, E>

Source§

fn try_from(value: Value) -> Result<Self, Self::Error>

Attempts to convert a serde_json::Value into a JsonResult<T, E> by trying to deserialize it first into T (success variant), then into E (error variant).

If deserialization into both types fails, returns a combined error message detailing both failures.

§Errors

Returns a serde_json::Error if the input JSON value cannot be parsed as either T or E.

§Examples
let json_val = json!(42);
let res: JsonResult<i32, String> = json_val.try_into().unwrap();
match res {
    JsonResult::Ok(val) => assert_eq!(val, 42),
    JsonResult::Err(_) => panic!("Expected Ok variant"),
}
Source§

type Error = Error

The type returned in the event of a conversion error.

Auto Trait Implementations§

§

impl<T, E> Freeze for JsonResult<T, E>
where T: Freeze, E: Freeze,

§

impl<T, E> RefUnwindSafe for JsonResult<T, E>

§

impl<T, E> Send for JsonResult<T, E>
where T: Send, E: Send,

§

impl<T, E> Sync for JsonResult<T, E>
where T: Sync, E: Sync,

§

impl<T, E> Unpin for JsonResult<T, E>
where T: Unpin, E: Unpin,

§

impl<T, E> UnwindSafe for JsonResult<T, E>
where T: UnwindSafe, E: 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> 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, 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>,