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
use std::error::Error;
use std::fmt::{self, Display, Formatter};
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, RustcEncodable, RustcDecodable)]
pub enum GetError {
NoSuchAccount,
NoSuchData,
NetworkOther(String),
}
impl<T: Into<String>> From<T> for GetError {
fn from(err: T) -> Self {
GetError::NetworkOther(err.into())
}
}
impl Display for GetError {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
match *self {
GetError::NoSuchAccount => write!(formatter, "Account does not exist for client"),
GetError::NoSuchData => write!(formatter, "Requested data not found"),
GetError::NetworkOther(ref error) => {
write!(formatter, "Error on Vault network: {}", error)
}
}
}
}
impl Error for GetError {
fn description(&self) -> &str {
match *self {
GetError::NoSuchAccount => "No such account",
GetError::NoSuchData => "No such data",
GetError::NetworkOther(ref error) => error,
}
}
}
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, RustcEncodable, RustcDecodable)]
pub enum MutationError {
NoSuchAccount,
AccountExists,
NoSuchData,
DataExists,
DataTooLarge,
LowBalance,
InvalidSuccessor,
InvalidOperation,
NetworkFull,
NetworkOther(String),
}
impl<T: Into<String>> From<T> for MutationError {
fn from(err: T) -> Self {
MutationError::NetworkOther(err.into())
}
}
impl Display for MutationError {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
match *self {
MutationError::NoSuchAccount => write!(formatter, "Account does not exist for client"),
MutationError::AccountExists => write!(formatter, "Account already exists for client"),
MutationError::NoSuchData => write!(formatter, "Requested data not found"),
MutationError::DataExists => write!(formatter, "Data given already exists"),
MutationError::DataTooLarge => write!(formatter, "Data given is too large"),
MutationError::LowBalance => {
write!(formatter, "Insufficient account balance for this operation")
}
MutationError::InvalidSuccessor => {
write!(formatter,
"Data given is not a valid successor of stored data")
}
MutationError::InvalidOperation => {
write!(formatter, "Requested operation is not allowed")
}
MutationError::NetworkFull => {
write!(formatter, "Network cannot store any further data")
}
MutationError::NetworkOther(ref error) => {
write!(formatter, "Error on Vault network: {}", error)
}
}
}
}
impl Error for MutationError {
fn description(&self) -> &str {
match *self {
MutationError::NoSuchAccount => "No such account",
MutationError::AccountExists => "Account exists",
MutationError::NoSuchData => "No such data",
MutationError::DataExists => "Data exists",
MutationError::DataTooLarge => "Data is too large",
MutationError::LowBalance => "Low account balance",
MutationError::InvalidSuccessor => "Invalid data successor",
MutationError::InvalidOperation => "Invalid operation",
MutationError::NetworkFull => "Network full",
MutationError::NetworkOther(ref error) => error,
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn conversion_from_str_literal() {
fn mutate() -> Result<(), MutationError> {
try!(Err("Mutation"))
}
let err_get = GetError::from("Get");
let err_mutation = mutate().unwrap_err();
match (err_get, err_mutation) {
(GetError::NetworkOther(val0), MutationError::NetworkOther(val1)) => {
assert_eq!(&val0, "Get");
assert_eq!(&val1, "Mutation");
}
err => panic!("Unexpected conversion: {:?}", err),
}
}
}