mwc_bch/util/
result.rs

1use hex::FromHexError;
2use ring;
3use rust_base58::base58::FromBase58Error;
4use secp256k1;
5use std;
6use std::io;
7use std::string::FromUtf8Error;
8
9/// Standard error type used in the library
10#[derive(Debug)]
11pub enum Error {
12    /// An argument provided is invalid
13    BadArgument(String),
14    /// The data given is not valid
15    BadData(String),
16    /// Base58 string could not be decoded
17    FromBase58Error(FromBase58Error),
18    /// Hex string could not be decoded
19    FromHexError(FromHexError),
20    /// UTF8 parsing error
21    FromUtf8Error(FromUtf8Error),
22    /// The state is not valid
23    IllegalState(String),
24    /// The operation is not valid on this object
25    InvalidOperation(String),
26    /// Standard library IO error
27    IOError(io::Error),
28    /// Error parsing an integer
29    ParseIntError(std::num::ParseIntError),
30    /// Error evaluating the script
31    ScriptError(String),
32    /// Error in the Secp256k1 library
33    Secp256k1Error(secp256k1::Error),
34    /// The operation timed out
35    Timeout,
36    /// An unknown error in the Ring library
37    UnspecifiedRingError,
38    /// The data or functionality is not supported by this library
39    Unsupported(String),
40}
41
42impl std::fmt::Display for Error {
43    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
44        match self {
45            Error::BadArgument(s) => f.write_str(&format!("Bad argument: {}", s)),
46            Error::BadData(s) => f.write_str(&format!("Bad data: {}", s)),
47            Error::FromBase58Error(e) => f.write_str(&format!("Base58 decoding error: {}", e)),
48            Error::FromHexError(e) => f.write_str(&format!("Hex decoding error: {}", e)),
49            Error::FromUtf8Error(e) => f.write_str(&format!("Utf8 parsing error: {}", e)),
50            Error::IllegalState(s) => f.write_str(&format!("Illegal state: {}", s)),
51            Error::InvalidOperation(s) => f.write_str(&format!("Invalid operation: {}", s)),
52            Error::IOError(e) => f.write_str(&format!("IO error: {}", e)),
53            Error::ParseIntError(e) => f.write_str(&format!("ParseIntError: {}", e)),
54            Error::ScriptError(s) => f.write_str(&format!("Script error: {}", s)),
55            Error::Secp256k1Error(e) => f.write_str(&format!("Secp256k1 error: {}", e)),
56            Error::Timeout => f.write_str("Timeout"),
57            Error::UnspecifiedRingError => f.write_str("Unspecified ring error"),
58            Error::Unsupported(s) => f.write_str(&format!("Unsuppored: {}", s)),
59        }
60    }
61}
62
63impl std::error::Error for Error {
64    fn description(&self) -> &str {
65        match self {
66            Error::BadArgument(_) => "Bad argument",
67            Error::BadData(_) => "Bad data",
68            Error::FromBase58Error(_) => "Base58 decoding error",
69            Error::FromHexError(_) => "Hex decoding error",
70            Error::FromUtf8Error(_) => "Utf8 parsing error",
71            Error::IllegalState(_) => "Illegal state",
72            Error::InvalidOperation(_) => "Invalid operation",
73            Error::IOError(_) => "IO error",
74            Error::ParseIntError(_) => "Parse int error",
75            Error::ScriptError(_) => "Script error",
76            Error::Secp256k1Error(_) => "Secp256k1 error",
77            Error::Timeout => "Timeout",
78            Error::UnspecifiedRingError => "Unspecified ring error",
79            Error::Unsupported(_) => "Unsupported",
80        }
81    }
82
83    fn cause(&self) -> Option<&dyn std::error::Error> {
84        match self {
85            Error::FromHexError(e) => Some(e),
86            Error::FromUtf8Error(e) => Some(e),
87            Error::IOError(e) => Some(e),
88            Error::ParseIntError(e) => Some(e),
89            Error::Secp256k1Error(e) => Some(e),
90            _ => None,
91        }
92    }
93}
94
95impl From<FromBase58Error> for Error {
96    fn from(e: FromBase58Error) -> Self {
97        Error::FromBase58Error(e)
98    }
99}
100
101impl From<FromHexError> for Error {
102    fn from(e: FromHexError) -> Self {
103        Error::FromHexError(e)
104    }
105}
106
107impl From<FromUtf8Error> for Error {
108    fn from(e: FromUtf8Error) -> Self {
109        Error::FromUtf8Error(e)
110    }
111}
112
113impl From<io::Error> for Error {
114    fn from(e: io::Error) -> Self {
115        Error::IOError(e)
116    }
117}
118
119impl From<std::num::ParseIntError> for Error {
120    fn from(e: std::num::ParseIntError) -> Self {
121        Error::ParseIntError(e)
122    }
123}
124
125impl From<secp256k1::Error> for Error {
126    fn from(e: secp256k1::Error) -> Self {
127        Error::Secp256k1Error(e)
128    }
129}
130
131impl From<ring::error::Unspecified> for Error {
132    fn from(_: ring::error::Unspecified) -> Self {
133        Error::UnspecifiedRingError
134    }
135}
136
137/// Standard Result used in the library
138pub type Result<T> = std::result::Result<T, Error>;