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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
use crate::{
decl_tagged_val_wrapper_methods, BitSetError, ConversionError, Env, EnvVal, RawVal,
SymbolError, Tag,
};
use core::{
cmp::Ordering,
convert::TryFrom,
fmt::Debug,
hash::{Hash, Hasher},
};
use stellar_xdr::{
ScHostContextErrorCode, ScHostFnErrorCode, ScHostObjErrorCode, ScHostStorageErrorCode,
ScHostValErrorCode, ScStatus, ScStatusType, ScUnknownErrorCode, ScVal, ScVmErrorCode,
};
#[derive(Copy, Clone)]
pub struct Status(RawVal);
decl_tagged_val_wrapper_methods!(Status);
impl Status {
pub const UNKNOWN_ERROR: Status =
unsafe { Status::from_major_minor(0, ScStatusType::UnknownError as u32) };
pub const OK: Status = unsafe { Status::from_major_minor(0, ScStatusType::Ok as u32) };
}
impl Hash for Status {
#[inline(always)]
fn hash<H: Hasher>(&self, state: &mut H) {
self.as_raw().get_payload().hash(state);
}
}
impl PartialEq for Status {
#[inline(always)]
fn eq(&self, other: &Self) -> bool {
self.as_raw().get_payload() == other.as_raw().get_payload()
}
}
impl Eq for Status {}
impl PartialOrd for Status {
#[inline(always)]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Status {
#[inline(always)]
fn cmp(&self, other: &Self) -> Ordering {
let self_tup = (self.as_raw().get_minor(), self.as_raw().get_major());
let other_tup = (other.as_raw().get_minor(), other.as_raw().get_major());
self_tup.cmp(&other_tup)
}
}
trait NamedCode {
fn fmt_code_name(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result;
}
impl NamedCode for ScHostContextErrorCode {
fn fmt_code_name(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.name())
}
}
impl NamedCode for ScHostFnErrorCode {
fn fmt_code_name(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.name())
}
}
impl NamedCode for ScHostObjErrorCode {
fn fmt_code_name(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.name())
}
}
impl NamedCode for ScHostStorageErrorCode {
fn fmt_code_name(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.name())
}
}
impl NamedCode for ScHostValErrorCode {
fn fmt_code_name(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.name())
}
}
impl NamedCode for ScVmErrorCode {
fn fmt_code_name(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.name())
}
}
fn fmt_named_code<C: NamedCode>(code: u32, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result
where
C: TryFrom<i32>,
{
match C::try_from(code as i32) {
Ok(c) => c.fmt_code_name(f),
Err(_) => write!(f, "UnknownCode"),
}
}
impl Debug for Status {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let st_res: Result<ScStatusType, _> = (self.as_raw().get_minor() as i32).try_into();
let code = self.as_raw().get_major();
let st = match st_res {
Ok(t) => t,
Err(_) => return write!(f, "Status(UnknownType)"),
};
write!(f, "Status({}(", st.name())?;
match st {
ScStatusType::Ok => write!(f, "{}", code),
ScStatusType::UnknownError => write!(f, "{}", code),
ScStatusType::HostValueError => fmt_named_code::<ScHostValErrorCode>(code, f),
ScStatusType::HostObjectError => fmt_named_code::<ScHostObjErrorCode>(code, f),
ScStatusType::HostFunctionError => fmt_named_code::<ScHostFnErrorCode>(code, f),
ScStatusType::HostStorageError => fmt_named_code::<ScHostStorageErrorCode>(code, f),
ScStatusType::HostContextError => fmt_named_code::<ScHostContextErrorCode>(code, f),
ScStatusType::VmError => fmt_named_code::<ScVmErrorCode>(code, f),
ScStatusType::ContractError => write!(f, "{}", code),
}?;
write!(f, "))")
}
}
impl TryFrom<Status> for ScStatus {
type Error = stellar_xdr::Error;
fn try_from(st: Status) -> Result<Self, Self::Error> {
let ok = {
if st.is_type(ScStatusType::Ok) {
ScStatus::Ok
} else if st.is_type(ScStatusType::UnknownError) {
ScStatus::UnknownError((st.get_code() as i32).try_into()?)
} else if st.is_type(ScStatusType::HostValueError) {
ScStatus::HostValueError((st.get_code() as i32).try_into()?)
} else if st.is_type(ScStatusType::HostObjectError) {
ScStatus::HostObjectError((st.get_code() as i32).try_into()?)
} else if st.is_type(ScStatusType::HostFunctionError) {
ScStatus::HostFunctionError((st.get_code() as i32).try_into()?)
} else if st.is_type(ScStatusType::HostStorageError) {
ScStatus::HostStorageError((st.get_code() as i32).try_into()?)
} else if st.is_type(ScStatusType::HostContextError) {
ScStatus::HostContextError((st.get_code() as i32).try_into()?)
} else if st.is_type(ScStatusType::VmError) {
ScStatus::VmError((st.get_code() as i32).try_into()?)
} else if st.is_type(ScStatusType::ContractError) {
ScStatus::ContractError(st.get_code())
} else {
return Err(stellar_xdr::Error::Invalid);
}
};
Ok(ok)
}
}
impl TryFrom<Status> for ScVal {
type Error = stellar_xdr::Error;
fn try_from(st: Status) -> Result<Self, Self::Error> {
Ok(ScVal::Status(<_ as TryInto<ScStatus>>::try_into(st)?))
}
}
impl From<ScStatus> for Status {
fn from(st: ScStatus) -> Self {
Status::from_status(st)
}
}
impl From<ScUnknownErrorCode> for Status {
fn from(code: ScUnknownErrorCode) -> Self {
ScStatus::UnknownError(code).into()
}
}
impl From<ScHostValErrorCode> for Status {
fn from(code: ScHostValErrorCode) -> Self {
ScStatus::HostValueError(code).into()
}
}
impl From<ScHostObjErrorCode> for Status {
fn from(code: ScHostObjErrorCode) -> Self {
ScStatus::HostObjectError(code).into()
}
}
impl From<ScHostFnErrorCode> for Status {
fn from(code: ScHostFnErrorCode) -> Self {
ScStatus::HostFunctionError(code).into()
}
}
impl From<ScHostStorageErrorCode> for Status {
fn from(code: ScHostStorageErrorCode) -> Self {
ScStatus::HostStorageError(code).into()
}
}
impl From<ScHostContextErrorCode> for Status {
fn from(code: ScHostContextErrorCode) -> Self {
ScStatus::HostContextError(code).into()
}
}
impl From<ScVmErrorCode> for Status {
fn from(code: ScVmErrorCode) -> Self {
ScStatus::VmError(code).into()
}
}
impl From<BitSetError> for Status {
fn from(bse: BitSetError) -> Self {
let s = match bse {
BitSetError::TooManyBits(_) => ScHostValErrorCode::BitsetTooManyBits,
};
ScStatus::HostValueError(s).into()
}
}
impl From<SymbolError> for Status {
fn from(se: SymbolError) -> Self {
let s = match se {
SymbolError::TooLong(_) => ScHostValErrorCode::SymbolTooLong,
SymbolError::BadChar(_) => ScHostValErrorCode::SymbolBadChar,
};
ScStatus::HostValueError(s).into()
}
}
impl From<ConversionError> for Status {
fn from(_: ConversionError) -> Self {
let s = ScHostValErrorCode::UnexpectedValType;
ScStatus::HostValueError(s).into()
}
}
impl Status {
#[inline(always)]
pub const fn is_type(&self, ty: ScStatusType) -> bool {
self.as_raw().has_minor(ty as u32)
}
#[inline(always)]
pub const fn get_code(&self) -> u32 {
self.as_raw().get_major()
}
#[inline(always)]
pub const fn is_ok(&self) -> bool {
self.is_type(ScStatusType::Ok)
}
#[inline(always)]
pub const fn from_contract_error(code: u32) -> Status {
Self::from_type_and_code(ScStatusType::ContractError, code)
}
#[inline(always)]
pub const fn from_type_and_code(ty: ScStatusType, code: u32) -> Status {
unsafe { Self::from_major_minor(code, ty as u32) }
}
#[inline(always)]
pub const fn from_status(sc: ScStatus) -> Status {
let code = match sc {
ScStatus::Ok => 0,
ScStatus::HostContextError(code) => code as i32 as u32,
ScStatus::HostValueError(code) => code as i32 as u32,
ScStatus::HostObjectError(code) => code as i32 as u32,
ScStatus::HostFunctionError(code) => code as i32 as u32,
ScStatus::HostStorageError(code) => code as i32 as u32,
ScStatus::VmError(code) => code as i32 as u32,
ScStatus::UnknownError(code) => code as i32 as u32,
ScStatus::ContractError(code) => code as u32,
};
Self::from_type_and_code(sc.discriminant(), code)
}
}
impl From<core::convert::Infallible> for crate::Status {
fn from(_: core::convert::Infallible) -> Self {
panic!()
}
}