1use self::Error::*;
19use coin::Coin;
20use either::Either;
21use openssl::error as openssl;
22use std::{error, fmt, io};
23
24#[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
63impl 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}