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
use alloc::vec::Vec;
use core::convert::{TryFrom, TryInto};
use failure::Fail;
use crate::{
bytesrepr::{self, FromBytes, ToBytes},
system_contracts::mint::purse_id::PurseIdError,
value::{CLType, CLTyped},
};
#[derive(Fail, Debug, Copy, Clone, PartialEq, Eq)]
#[repr(u8)]
pub enum Error {
#[fail(display = "Insufficient funds")]
InsufficientFunds = 0,
#[fail(display = "Source not found")]
SourceNotFound = 1,
#[fail(display = "Destination not found")]
DestNotFound = 2,
#[fail(display = "Invalid URef")]
InvalidURef = 3,
#[fail(display = "Invalid AccessRights")]
InvalidAccessRights = 4,
#[fail(display = "Invalid non-empty purse creation")]
InvalidNonEmptyPurseCreation = 5,
#[fail(display = "Missing argument")]
MissingArgument = 102,
#[fail(display = "Passed argument is invalid")]
InvalidArgument = 103,
}
impl From<PurseIdError> for Error {
fn from(purse_id_error: PurseIdError) -> Error {
match purse_id_error {
PurseIdError::InvalidURef => Error::InvalidURef,
PurseIdError::InvalidAccessRights(_) => {
Error::InvalidAccessRights
}
}
}
}
impl CLTyped for Error {
fn cl_type() -> CLType {
CLType::U8
}
}
pub struct TryFromU8ForError(());
impl TryFrom<u8> for Error {
type Error = TryFromU8ForError;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
d if d == Error::InsufficientFunds as u8 => Ok(Error::InsufficientFunds),
d if d == Error::SourceNotFound as u8 => Ok(Error::SourceNotFound),
d if d == Error::DestNotFound as u8 => Ok(Error::DestNotFound),
d if d == Error::InvalidURef as u8 => Ok(Error::InvalidURef),
d if d == Error::InvalidAccessRights as u8 => Ok(Error::InvalidAccessRights),
d if d == Error::MissingArgument as u8 => Ok(Error::MissingArgument),
d if d == Error::InvalidArgument as u8 => Ok(Error::InvalidArgument),
d if d == Error::InvalidNonEmptyPurseCreation as u8 => {
Ok(Error::InvalidNonEmptyPurseCreation)
}
_ => Err(TryFromU8ForError(())),
}
}
}
impl ToBytes for Error {
fn to_bytes(&self) -> Result<Vec<u8>, bytesrepr::Error> {
let value = *self as u8;
value.to_bytes()
}
}
impl FromBytes for Error {
fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), bytesrepr::Error> {
let (value, rem): (u8, _) = FromBytes::from_bytes(bytes)?;
let error: Error = value
.try_into()
.map_err(|_| bytesrepr::Error::FormattingError)?;
Ok((error, rem))
}
}