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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ContractResult<S> {
Ok(S),
#[serde(rename = "error")]
Err(String),
}
impl<S> ContractResult<S> {
pub fn into_result(self) -> Result<S, String> {
Result::<S, String>::from(self)
}
pub fn unwrap(self) -> S {
self.into_result().unwrap()
}
pub fn is_ok(&self) -> bool {
matches!(self, ContractResult::Ok(_))
}
pub fn is_err(&self) -> bool {
matches!(self, ContractResult::Err(_))
}
}
impl<S: fmt::Debug> ContractResult<S> {
pub fn unwrap_err(self) -> String {
self.into_result().unwrap_err()
}
}
impl<S, E: ToString> From<Result<S, E>> for ContractResult<S> {
fn from(original: Result<S, E>) -> ContractResult<S> {
match original {
Ok(value) => ContractResult::Ok(value),
Err(err) => ContractResult::Err(err.to_string()),
}
}
}
impl<S> From<ContractResult<S>> for Result<S, String> {
fn from(original: ContractResult<S>) -> Result<S, String> {
match original {
ContractResult::Ok(value) => Ok(value),
ContractResult::Err(err) => Err(err),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{from_slice, to_vec, Response, StdError, StdResult};
#[test]
fn contract_result_serialization_works() {
let result = ContractResult::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\"}");
let result: ContractResult<Response> = ContractResult::Ok(Response::default());
assert_eq!(
to_vec(&result).unwrap(),
br#"{"ok":{"submessages":[],"messages":[],"attributes":[],"data":null}}"#.to_vec()
);
let result: ContractResult<Response> = ContractResult::Err("broken".to_string());
assert_eq!(&to_vec(&result).unwrap(), b"{\"error\":\"broken\"}");
}
#[test]
fn contract_result_deserialization_works() {
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();
assert_eq!(result, ContractResult::Ok("foo".to_string()));
let result: ContractResult<Response> =
from_slice(br#"{"ok":{"submessages":[],"messages":[],"attributes":[],"data":null}}"#)
.unwrap();
assert_eq!(result, ContractResult::Ok(Response::default()));
let result: ContractResult<Response> = from_slice(br#"{"error":"broken"}"#).unwrap();
assert_eq!(result, ContractResult::Err("broken".to_string()));
let result: ContractResult<u64> = from_slice(b" {\n\t \"ok\": 5898\n} ").unwrap();
assert_eq!(result, ContractResult::Ok(5898));
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}"#);
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?"}"#);
match parse.unwrap_err() {
StdError::ParseErr { .. } => {}
err => panic!("Unexpected error: {:?}", err),
}
}
#[test]
fn can_convert_from_core_result() {
let original: Result<Response, StdError> = Ok(Response::default());
let converted: ContractResult<Response> = original.into();
assert_eq!(converted, ContractResult::Ok(Response::default()));
let original: Result<Response, StdError> = Err(StdError::generic_err("broken"));
let converted: ContractResult<Response> = original.into();
assert_eq!(
converted,
ContractResult::Err("Generic error: broken".to_string())
);
}
#[test]
fn can_convert_to_core_result() {
let original = ContractResult::Ok(Response::default());
let converted: Result<Response, String> = original.into();
assert_eq!(converted, Ok(Response::default()));
let original = ContractResult::Err("went wrong".to_string());
let converted: Result<Response, String> = original.into();
assert_eq!(converted, Err("went wrong".to_string()));
}
}