Skip to content

Commit

Permalink
macros for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
afilini committed Jan 26, 2020
1 parent af44e5e commit bd183a0
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions core/electrum_client/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,26 +176,17 @@ pub enum Error {
SslHandshakeError(openssl::ssl::HandshakeError<std::net::TcpStream>),
}

impl std::convert::From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Error::IOError(err)
}
}

impl std::convert::From<serde_json::error::Error> for Error {
fn from(err: serde_json::error::Error) -> Self {
Error::JSON(err)
}
}

impl std::convert::From<bitcoin::hashes::hex::Error> for Error {
fn from(err: bitcoin::hashes::hex::Error) -> Self {
Error::Hex(err)
}
macro_rules! impl_error {
( $from:ty, $to:ident ) => {
impl std::convert::From<$from> for Error {
fn from(err: $from) -> Self {
Error::$to(err)
}
}
};
}

impl std::convert::From<bitcoin::consensus::encode::Error> for Error {
fn from(err: bitcoin::consensus::encode::Error) -> Self {
Error::Bitcoin(err)
}
}
impl_error!(std::io::Error, IOError);
impl_error!(serde_json::Error, JSON);
impl_error!(bitcoin::hashes::hex::Error, Hex);
impl_error!(bitcoin::consensus::encode::Error, Bitcoin);

0 comments on commit bd183a0

Please sign in to comment.