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
use std::{error, fmt};
use std::io;
use std::num;
use std::string;
use bech32;
use secp256k1;
pub type ConvertResult = Result<Vec<u8>, Error>;
#[derive(Debug)]
pub enum Error {
InvalidParameter(String),
InvalidInputValue(u8),
InvalidValue(String),
InvalidPadding,
InvalidLength(String),
IOErr(io::Error),
ParseFloatErr(num::ParseFloatError),
ParseIntErr(num::ParseIntError),
FromUTF8Err(string::FromUtf8Error),
Bech32Err(bech32::Error),
SignatureError(secp256k1::Error),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::InvalidParameter(ref e) => write!(f, "{}", e),
Error::InvalidInputValue(e) => write!(f, "invalid input value ({})", e),
Error::InvalidValue(ref e) => write!(f, "{}", e),
Error::InvalidPadding => write!(f, "invalid padding"),
Error::InvalidLength(ref e) => write!(f, "{}", e),
Error::IOErr(ref e) => write!(f, "{}", e),
Error::ParseFloatErr(ref e) => write!(f, "{}", e),
Error::ParseIntErr(ref e) => write!(f, "{}", e),
Error::FromUTF8Err(ref e) => write!(f, "{}", e),
Error::Bech32Err(ref e) => write!(f, "{}", e),
Error::SignatureError(ref e) => write!(f, "{:?}", e),
}
}
}
impl error::Error for Error {
fn description(&self) -> &str {
match *self {
Error::InvalidParameter(ref e) => e,
Error::InvalidInputValue(_) => "invalid input value",
Error::InvalidValue(ref e) => e,
Error::InvalidPadding => "invalid padding",
Error::InvalidLength(ref e) => e,
Error::IOErr(ref e) => error::Error::description(e),
Error::ParseFloatErr(ref e) => error::Error::description(e),
Error::ParseIntErr(ref e) => error::Error::description(e),
Error::FromUTF8Err(ref e) => error::Error::description(e),
Error::Bech32Err(ref e) => error::Error::description(e),
Error::SignatureError(ref e) => match *e {
secp256k1::Error::InvalidSignature => "invalid signature",
secp256k1::Error::InvalidPublicKey => "invalid public key",
secp256k1::Error::InvalidSecretKey => "invalid secret key",
secp256k1::Error::InvalidRecoveryId => "invalid recovery id",
secp256k1::Error::InvalidMessage => "invalid message",
},
}
}
fn cause(&self) -> Option<&error::Error> {
match *self {
Error::IOErr(ref e) => Some(e),
Error::ParseFloatErr(ref e) => Some(e),
Error::ParseIntErr(ref e) => Some(e),
Error::FromUTF8Err(ref e) => Some(e),
_ => None,
}
}
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Error {
Error::IOErr(e)
}
}
impl From<num::ParseFloatError> for Error {
fn from(e: num::ParseFloatError) -> Error {
Error::ParseFloatErr(e)
}
}
impl From<num::ParseIntError> for Error {
fn from(e: num::ParseIntError) -> Error {
Error::ParseIntErr(e)
}
}
impl From<string::FromUtf8Error> for Error {
fn from(e: string::FromUtf8Error) -> Error {
Error::FromUTF8Err(e)
}
}
impl From<bech32::Error> for Error {
fn from(e: bech32::Error) -> Error {
Error::Bech32Err(e)
}
}
impl From<secp256k1::Error> for Error {
fn from(e: secp256k1::Error) -> Error {
Error::SignatureError(e)
}
}