From 81519555cfa74462c2dd0030d973ffdd2ae9c254 Mon Sep 17 00:00:00 2001 From: Riccardo Casatta Date: Fri, 4 Dec 2020 11:23:01 +0100 Subject: [PATCH] generalize impl_error! macro so that used for other errors type --- src/blockchain/compact_filters/mod.rs | 18 ++++------------- src/blockchain/esplora.rs | 29 +++++---------------------- src/error.rs | 7 +++++-- src/keys/mod.rs | 13 ++---------- 4 files changed, 16 insertions(+), 51 deletions(-) diff --git a/src/blockchain/compact_filters/mod.rs b/src/blockchain/compact_filters/mod.rs index 233ea89a..668726f9 100644 --- a/src/blockchain/compact_filters/mod.rs +++ b/src/blockchain/compact_filters/mod.rs @@ -553,20 +553,10 @@ impl fmt::Display for CompactFiltersError { impl std::error::Error for CompactFiltersError {} -macro_rules! impl_error { - ( $from:ty, $to:ident ) => { - impl std::convert::From<$from> for CompactFiltersError { - fn from(err: $from) -> Self { - CompactFiltersError::$to(err) - } - } - }; -} - -impl_error!(rocksdb::Error, DB); -impl_error!(std::io::Error, IO); -impl_error!(bitcoin::util::bip158::Error, BIP158); -impl_error!(std::time::SystemTimeError, Time); +impl_error!(rocksdb::Error, DB, CompactFiltersError); +impl_error!(std::io::Error, IO, CompactFiltersError); +impl_error!(bitcoin::util::bip158::Error, BIP158, CompactFiltersError); +impl_error!(std::time::SystemTimeError, Time, CompactFiltersError); impl From for CompactFiltersError { fn from(err: crate::error::Error) -> Self { diff --git a/src/blockchain/esplora.rs b/src/blockchain/esplora.rs index a6c4ffa5..45523bbb 100644 --- a/src/blockchain/esplora.rs +++ b/src/blockchain/esplora.rs @@ -47,7 +47,7 @@ use serde::Deserialize; use reqwest::{Client, StatusCode}; -use bitcoin::consensus::{deserialize, serialize}; +use bitcoin::consensus::{self, deserialize, serialize}; use bitcoin::hashes::hex::{FromHex, ToHex}; use bitcoin::hashes::{sha256, Hash}; use bitcoin::{BlockHash, BlockHeader, Script, Transaction, Txid}; @@ -423,26 +423,7 @@ impl fmt::Display for EsploraError { impl std::error::Error for EsploraError {} -impl From for EsploraError { - fn from(other: reqwest::Error) -> Self { - EsploraError::Reqwest(other) - } -} - -impl From for EsploraError { - fn from(other: std::num::ParseIntError) -> Self { - EsploraError::Parsing(other) - } -} - -impl From for EsploraError { - fn from(other: bitcoin::consensus::encode::Error) -> Self { - EsploraError::BitcoinEncoding(other) - } -} - -impl From for EsploraError { - fn from(other: bitcoin::hashes::hex::Error) -> Self { - EsploraError::Hex(other) - } -} +impl_error!(reqwest::Error, Reqwest, EsploraError); +impl_error!(std::num::ParseIntError, Parsing, EsploraError); +impl_error!(consensus::encode::Error, BitcoinEncoding, EsploraError); +impl_error!(bitcoin::hashes::hex::Error, Hex, EsploraError); diff --git a/src/error.rs b/src/error.rs index bc35e1f1..70b0e7b6 100644 --- a/src/error.rs +++ b/src/error.rs @@ -110,9 +110,12 @@ impl std::error::Error for Error {} macro_rules! impl_error { ( $from:ty, $to:ident ) => { - impl std::convert::From<$from> for Error { + impl_error!($from, $to, Error); + }; + ( $from:ty, $to:ident, $impl_for:ty ) => { + impl std::convert::From<$from> for $impl_for { fn from(err: $from) -> Self { - Error::$to(err) + <$impl_for>::$to(err) } } }; diff --git a/src/keys/mod.rs b/src/keys/mod.rs index 8367122b..2f0b8610 100644 --- a/src/keys/mod.rs +++ b/src/keys/mod.rs @@ -679,17 +679,8 @@ pub enum KeyError { Miniscript(miniscript::Error), } -impl From for KeyError { - fn from(inner: miniscript::Error) -> Self { - KeyError::Miniscript(inner) - } -} - -impl From for KeyError { - fn from(inner: bitcoin::util::bip32::Error) -> Self { - KeyError::BIP32(inner) - } -} +impl_error!(miniscript::Error, Miniscript, KeyError); +impl_error!(bitcoin::util::bip32::Error, BIP32, KeyError); impl std::fmt::Display for KeyError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {