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
use cosmwasm_std::{from_slice, to_binary, Binary, Coin};
use serde::{de::DeserializeOwned, Serialize};
#[cosmwasm_schema::cw_serde]
pub enum StdAck {
Result(Binary),
Error(String),
}
impl StdAck {
pub fn success(data: impl Serialize) -> Binary {
let res = to_binary(&data).unwrap();
StdAck::Result(res).ack()
}
pub fn fail(err: String) -> Binary {
StdAck::Error(err).ack()
}
pub fn ack(&self) -> Binary {
to_binary(self).unwrap()
}
pub fn unwrap(self) -> Binary {
match self {
StdAck::Result(data) => data,
StdAck::Error(err) => panic!("{}", err),
}
}
pub fn unwrap_into<T: DeserializeOwned>(self) -> T {
from_slice(&self.unwrap()).unwrap()
}
pub fn unwrap_err(self) -> String {
match self {
StdAck::Result(_) => panic!("not an error"),
StdAck::Error(err) => err,
}
}
}
#[cosmwasm_schema::cw_serde]
pub struct DispatchResponse {
pub results: Vec<Binary>,
}
#[cosmwasm_schema::cw_serde]
pub struct SendAllBackResponse {}
#[cosmwasm_schema::cw_serde]
pub struct WhoAmIResponse {
pub chain: String,
}
#[cosmwasm_schema::cw_serde]
pub struct IbcQueryResponse {
pub results: Vec<Binary>,
}
#[cosmwasm_schema::cw_serde]
pub struct RegisterResponse {
pub account: String,
}
#[cosmwasm_schema::cw_serde]
pub struct BalancesResponse {
pub account: String,
pub balances: Vec<Coin>,
}
#[cfg(test)]
mod test {
use super::*;
use speculoos::prelude::*;
const TEST_DATA_STR: &str = "test data";
fn test_binary_data() -> Binary {
to_binary(TEST_DATA_STR).unwrap()
}
#[test]
fn success_should_wrap_in_result_with_binary_data() {
let expected = StdAck::Result(test_binary_data()).ack();
let actual = StdAck::success(TEST_DATA_STR);
assert_that!(&actual).is_equal_to(&expected);
}
#[test]
fn fail_should_wrap_in_error() {
let err = "my-error";
let expected = to_binary(&StdAck::Error(err.to_string())).unwrap();
let actual = StdAck::fail(err.to_string());
assert_that!(&actual).is_equal_to(&expected);
}
#[test]
fn ack_should_binary_contents() {
let actual: Binary = StdAck::Result(test_binary_data()).ack();
let expected = to_binary(&StdAck::Result(test_binary_data())).unwrap();
assert_that!(&actual).is_equal_to(&expected);
}
#[test]
fn unwrap_with_result_should_return_binary_data() {
let expected_data = test_binary_data();
let actual = StdAck::Result(expected_data.clone()).unwrap();
let expected = expected_data;
assert_that!(&actual).is_equal_to(&expected);
}
#[test]
#[should_panic]
fn unwrap_with_error_should_panic() {
StdAck::Error("my-error".to_string()).unwrap();
}
#[test]
fn unwrap_into_should_return_deserialized_data() {
let actual = StdAck::Result(test_binary_data()).unwrap_into::<String>();
let expected = TEST_DATA_STR.to_string();
assert_that!(&actual).is_equal_to(&expected);
}
#[test]
fn unwrap_err_with_err_should_return_error_message() {
let err = "my-error";
let actual = StdAck::Error(err.to_string()).unwrap_err();
let expected = err.to_string();
assert_that!(&actual).is_equal_to(&expected);
}
#[test]
#[should_panic]
fn unwrap_err_with_result_should_panic() {
let _data = "my-data";
let _actual = StdAck::Result(Binary::default()).unwrap_err();
}
}