Skip to content

Secret cw1.1 changes #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions packages/std/src/results/contract_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::fmt;
/// # use cosmwasm_std::{to_vec, ContractResult, Response};
/// let response: Response = Response::default();
/// let result: ContractResult<Response> = ContractResult::Ok(response);
/// assert_eq!(to_vec(&result).unwrap(), br#"{"ok":{"messages":[],"attributes":[],"events":[],"data":null}}"#);
/// assert_eq!(to_vec(&result).unwrap(), br#"{"Ok":{"messages":[],"attributes":[],"events":[],"data":null}}"#);
/// ```
///
/// Failure:
Expand All @@ -29,15 +29,13 @@ use std::fmt;
/// # use cosmwasm_std::{to_vec, ContractResult, Response};
/// let error_msg = String::from("Something went wrong");
/// let result: ContractResult<Response> = ContractResult::Err(error_msg);
/// assert_eq!(to_vec(&result).unwrap(), br#"{"error":"Something went wrong"}"#);
/// assert_eq!(to_vec(&result).unwrap(), br#"{"Err":"Something went wrong"}"#);
/// ```
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ContractResult<S> {
Ok(S),
/// An error type that every custom error created by contract developers can be converted to.
/// This could potientially have more structure, but String is the easiest.
#[serde(rename = "error")]
Err(String),
}

Expand Down Expand Up @@ -95,54 +93,54 @@ mod tests {
#[test]
fn contract_result_serialization_works() {
let result = ContractResult::Ok(12);
assert_eq!(&to_vec(&result).unwrap(), b"{\"ok\":12}");
assert_eq!(&to_vec(&result).unwrap(), b"{\"Ok\":12}");

let result = ContractResult::Ok("foo");
assert_eq!(&to_vec(&result).unwrap(), b"{\"ok\":\"foo\"}");
assert_eq!(&to_vec(&result).unwrap(), b"{\"Ok\":\"foo\"}");

let result: ContractResult<Response> = ContractResult::Ok(Response::default());
assert_eq!(
to_vec(&result).unwrap(),
br#"{"ok":{"messages":[],"attributes":[],"events":[],"data":null}}"#
br#"{"Ok":{"messages":[],"attributes":[],"events":[],"data":null}}"#
);

let result: ContractResult<Response> = ContractResult::Err("broken".to_string());
assert_eq!(&to_vec(&result).unwrap(), b"{\"error\":\"broken\"}");
assert_eq!(&to_vec(&result).unwrap(), b"{\"Err\":\"broken\"}");
}

#[test]
fn contract_result_deserialization_works() {
let result: ContractResult<u64> = from_slice(br#"{"ok":12}"#).unwrap();
let result: ContractResult<u64> = from_slice(br#"{"Ok":12}"#).unwrap();
assert_eq!(result, ContractResult::Ok(12));

let result: ContractResult<String> = from_slice(br#"{"ok":"foo"}"#).unwrap();
let result: ContractResult<String> = from_slice(br#"{"Ok":"foo"}"#).unwrap();
assert_eq!(result, ContractResult::Ok("foo".to_string()));

let result: ContractResult<Response> =
from_slice(br#"{"ok":{"messages":[],"attributes":[],"events":[],"data":null}}"#)
from_slice(br#"{"Ok":{"messages":[],"attributes":[],"events":[],"data":null}}"#)
.unwrap();
assert_eq!(result, ContractResult::Ok(Response::default()));

let result: ContractResult<Response> = from_slice(br#"{"error":"broken"}"#).unwrap();
let result: ContractResult<Response> = from_slice(br#"{"Err":"broken"}"#).unwrap();
assert_eq!(result, ContractResult::Err("broken".to_string()));

// ignores whitespace
let result: ContractResult<u64> = from_slice(b" {\n\t \"ok\": 5898\n} ").unwrap();
let result: ContractResult<u64> = from_slice(b" {\n\t \"Ok\": 5898\n} ").unwrap();
assert_eq!(result, ContractResult::Ok(5898));

// fails for additional attributes
let parse: StdResult<ContractResult<u64>> = from_slice(br#"{"unrelated":321,"ok":4554}"#);
let parse: StdResult<ContractResult<u64>> = from_slice(br#"{"unrelated":321,"Ok":4554}"#);
match parse.unwrap_err() {
StdError::ParseErr { .. } => {}
err => panic!("Unexpected error: {:?}", err),
}
let parse: StdResult<ContractResult<u64>> = from_slice(br#"{"ok":4554,"unrelated":321}"#);
let parse: StdResult<ContractResult<u64>> = from_slice(br#"{"Ok":4554,"unrelated":321}"#);
match parse.unwrap_err() {
StdError::ParseErr { .. } => {}
err => panic!("Unexpected error: {:?}", err),
}
let parse: StdResult<ContractResult<u64>> =
from_slice(br#"{"ok":4554,"error":"What's up now?"}"#);
from_slice(br#"{"Ok":4554,"Err":"What's up now?"}"#);
match parse.unwrap_err() {
StdError::ParseErr { .. } => {}
err => panic!("Unexpected error: {:?}", err),
Expand Down
6 changes: 2 additions & 4 deletions packages/std/src/results/system_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use super::super::errors::SystemError;
/// # 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"}}"#);
/// assert_eq!(to_vec(&result).unwrap(), br#"{"Ok":{"Ok":"aGVsbG8sIHdvcmxk"}}"#);
/// ```
///
/// Failure:
Expand All @@ -29,13 +29,11 @@ use super::super::errors::SystemError;
/// # 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":{}}}"#);
/// assert_eq!(to_vec(&result).unwrap(), br#"{"Err":{"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),
}

Expand Down