1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
use core::fmt;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use super::super::errors::SystemError;
/// This is the outer result type returned by a querier to the contract.
///
/// We use a custom type here instead of Rust's Result because we want to be able to
/// define the serialization, which is a public interface. Every language that compiles
/// to Wasm and runs in the ComsWasm VM needs to create the same JSON representation.
///
/// # Examples
///
/// Success:
///
/// ```
/// # use cosmwasm_std::{to_vec, Binary, ContractResult, SystemResult};
/// let data = Binary::from(b"hello, world");
/// let result = SystemResult::Ok(ContractResult::Ok(data));
/// assert_eq!(to_vec(&result).unwrap(), br#"{"ok":{"ok":"aGVsbG8sIHdvcmxk"}}"#);
/// ```
///
/// Failure:
///
/// ```
/// # use cosmwasm_std::{to_vec, Binary, ContractResult, SystemResult, SystemError};
/// let error = SystemError::Unknown {};
/// let result: SystemResult<Binary> = SystemResult::Err(error);
/// assert_eq!(to_vec(&result).unwrap(), br#"{"error":{"unknown":{}}}"#);
/// ```
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum SystemResult<S> {
Ok(S),
#[serde(rename = "error")]
Err(SystemError),
}
// Implementations here mimic the Result API and should be implemented via a conversion to Result
// to ensure API consistency
impl<S> SystemResult<S> {
/// Converts a `ContractResult<S>` to a `Result<S, SystemError>` as a convenient way
/// to access the full Result API.
pub fn into_result(self) -> Result<S, SystemError> {
Result::<S, SystemError>::from(self)
}
pub fn unwrap(self) -> S {
self.into_result().unwrap()
}
}
impl<S: fmt::Debug> SystemResult<S> {
pub fn unwrap_err(self) -> SystemError {
self.into_result().unwrap_err()
}
}
impl<S> From<Result<S, SystemError>> for SystemResult<S> {
fn from(original: Result<S, SystemError>) -> SystemResult<S> {
match original {
Ok(value) => SystemResult::Ok(value),
Err(err) => SystemResult::Err(err),
}
}
}
impl<S> From<SystemResult<S>> for Result<S, SystemError> {
fn from(original: SystemResult<S>) -> Result<S, SystemError> {
match original {
SystemResult::Ok(value) => Ok(value),
SystemResult::Err(err) => Err(err),
}
}
}