wallet_gen/
error.rs

1/*
2 * error.rs
3 *
4 * Copyright 2018 Standard Mining
5 *
6 * Available to be used and modified under the terms of the MIT License.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
9 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
10 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
11 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
12 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
13 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14 */
15
16//! Error type for the crate.
17
18use self::Error::*;
19use coin::Coin;
20use either::Either;
21use openssl::error as openssl;
22use std::{error, fmt, io};
23
24/// Enum that stores various possible error types when generating wallets.
25#[allow(missing_docs)]
26#[derive(Debug)]
27pub enum Error {
28    StaticMsg(&'static str),
29    Msg(String),
30    CoinNotSupported(Coin),
31    Io(io::Error),
32    OpenSsl(Either<openssl::Error, openssl::ErrorStack>),
33}
34
35impl error::Error for Error {
36    fn description(&self) -> &str {
37        match *self {
38            StaticMsg(s) => s,
39            Msg(ref s) => s,
40            CoinNotSupported(_) => "This coin is not supported",
41            Io(ref e) => e.description(),
42            OpenSsl(Either::Left(ref e)) => e.description(),
43            OpenSsl(Either::Right(ref e)) => e.description(),
44        }
45    }
46
47    fn cause(&self) -> Option<&error::Error> {
48        match *self {
49            StaticMsg(_) | Msg(_) | CoinNotSupported(_) => None,
50            Io(ref e) => Some(e),
51            OpenSsl(Either::Left(ref e)) => Some(e),
52            OpenSsl(Either::Right(ref e)) => Some(e),
53        }
54    }
55}
56
57impl fmt::Display for Error {
58    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59        write!(f, "{}", error::Error::description(self))
60    }
61}
62
63// Auto-conversion
64impl From<String> for Error {
65    fn from(error: String) -> Self { Error::Msg(error) }
66}
67
68impl From<&'static str> for Error {
69    fn from(error: &'static str) -> Self { Error::StaticMsg(error) }
70}
71
72impl From<io::Error> for Error {
73    fn from(error: io::Error) -> Self { Error::Io(error) }
74}
75
76impl From<openssl::Error> for Error {
77    fn from(error: openssl::Error) -> Self { Error::OpenSsl(Either::Left(error)) }
78}
79
80impl From<openssl::ErrorStack> for Error {
81    fn from(error: openssl::ErrorStack) -> Self { Error::OpenSsl(Either::Right(error)) }
82}