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<'de, T, E> Deserialize<'de> for JsonResult<T, E>where
T: Deserialize<'de>,
E: Deserialize<'de>,
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>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<T, E> From<JsonResult<T, E>> for Value
impl<T, E> From<JsonResult<T, E>> for Value
Source§fn from(res: JsonResult<T, E>) -> Self
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>
impl<T, E> From<Result<T, E>> for JsonResult<T, E>
Source§impl<T, E> Serialize for JsonResult<T, E>
impl<T, E> Serialize for JsonResult<T, E>
Source§impl<T, E> TryFrom<Value> for JsonResult<T, E>where
T: DeserializeOwned,
E: DeserializeOwned,
impl<T, E> TryFrom<Value> for JsonResult<T, E>where
T: DeserializeOwned,
E: DeserializeOwned,
Source§fn try_from(value: Value) -> Result<Self, Self::Error>
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"),
}