diff --git a/crates/bdk/src/descriptor/error.rs b/crates/bdk/src/descriptor/error.rs index 07a874efed..3a704d2ead 100644 --- a/crates/bdk/src/descriptor/error.rs +++ b/crates/bdk/src/descriptor/error.rs @@ -87,9 +87,38 @@ impl fmt::Display for Error { #[cfg(feature = "std")] impl std::error::Error for Error {} -impl_error!(bitcoin::bip32::Error, Bip32); -impl_error!(bitcoin::base58::Error, Base58); -impl_error!(bitcoin::key::Error, Pk); -impl_error!(miniscript::Error, Miniscript); -impl_error!(bitcoin::hashes::hex::Error, Hex); -impl_error!(crate::descriptor::policy::PolicyError, Policy); +impl From for Error { + fn from(err: bitcoin::bip32::Error) -> Self { + Error::Bip32(err) + } +} + +impl From for Error { + fn from(err: bitcoin::base58::Error) -> Self { + Error::Base58(err) + } +} + +impl From for Error { + fn from(err: bitcoin::key::Error) -> Self { + Error::Pk(err) + } +} + +impl From for Error { + fn from(err: miniscript::Error) -> Self { + Error::Miniscript(err) + } +} + +impl From for Error { + fn from(err: bitcoin::hashes::hex::Error) -> Self { + Error::Hex(err) + } +} + +impl From for Error { + fn from(err: crate::descriptor::policy::PolicyError) -> Self { + Error::Policy(err) + } +} diff --git a/crates/bdk/src/keys/mod.rs b/crates/bdk/src/keys/mod.rs index b47c4b86d4..2050c7f8a0 100644 --- a/crates/bdk/src/keys/mod.rs +++ b/crates/bdk/src/keys/mod.rs @@ -932,8 +932,17 @@ pub enum KeyError { Miniscript(miniscript::Error), } -impl_error!(miniscript::Error, Miniscript, KeyError); -impl_error!(bitcoin::bip32::Error, Bip32, KeyError); +impl From for KeyError { + fn from(err: miniscript::Error) -> Self { + KeyError::Miniscript(err) + } +} + +impl From for KeyError { + fn from(err: bip32::Error) -> Self { + KeyError::Bip32(err) + } +} impl fmt::Display for KeyError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { diff --git a/crates/bdk/src/lib.rs b/crates/bdk/src/lib.rs index e5f1146a2b..3f0c7a2626 100644 --- a/crates/bdk/src/lib.rs +++ b/crates/bdk/src/lib.rs @@ -27,8 +27,6 @@ extern crate serde_json; #[cfg(feature = "keys-bip39")] extern crate bip39; -#[macro_use] -pub mod error; pub mod descriptor; pub mod keys; pub mod psbt; diff --git a/crates/bdk/src/wallet/coin_selection.rs b/crates/bdk/src/wallet/coin_selection.rs index 7803c55091..2de7aec24c 100644 --- a/crates/bdk/src/wallet/coin_selection.rs +++ b/crates/bdk/src/wallet/coin_selection.rs @@ -27,7 +27,7 @@ //! # use std::str::FromStr; //! # use bitcoin::*; //! # use bdk::wallet::{self, ChangeSet, coin_selection::*, coin_selection}; -//! # use bdk::error::CreateTxError; +//! # use bdk::wallet::error::CreateTxError; //! # use bdk_chain::PersistBackend; //! # use bdk::*; //! # use bdk::wallet::coin_selection::decide_change; diff --git a/crates/bdk/src/error.rs b/crates/bdk/src/wallet/error.rs similarity index 97% rename from crates/bdk/src/error.rs rename to crates/bdk/src/wallet/error.rs index 723e2617f0..328ab291b6 100644 --- a/crates/bdk/src/error.rs +++ b/crates/bdk/src/wallet/error.rs @@ -43,19 +43,6 @@ impl fmt::Display for MiniscriptPsbtError { #[cfg(feature = "std")] impl std::error::Error for MiniscriptPsbtError {} -macro_rules! impl_error { - ( $from:ty, $to:ident ) => { - impl_error!($from, $to, Error); - }; - ( $from:ty, $to:ident, $impl_for:ty ) => { - impl core::convert::From<$from> for $impl_for { - fn from(err: $from) -> Self { - <$impl_for>::$to(err) - } - } - }; -} - #[derive(Debug)] /// Error returned from [`TxBuilder::finish`] /// @@ -259,8 +246,6 @@ impl

From for CreateTxError

{ #[cfg(feature = "std")] impl std::error::Error for CreateTxError

{} -// - #[derive(Debug)] /// Error returned from [`Wallet::build_fee_bump`] /// diff --git a/crates/bdk/src/wallet/mod.rs b/crates/bdk/src/wallet/mod.rs index 9d006b440d..86a876dfef 100644 --- a/crates/bdk/src/wallet/mod.rs +++ b/crates/bdk/src/wallet/mod.rs @@ -51,6 +51,7 @@ pub mod signer; pub mod tx_builder; pub(crate) mod utils; +pub mod error; #[cfg(feature = "hardware-signer")] #[cfg_attr(docsrs, doc(cfg(feature = "hardware-signer")))] pub mod hardwaresigner; @@ -69,11 +70,11 @@ use crate::descriptor::{ calc_checksum, into_wallet_descriptor_checked, DerivedDescriptor, DescriptorMeta, ExtendedDescriptor, ExtractPolicy, IntoWalletDescriptor, Policy, XKeyUtils, }; -use crate::error::{BuildFeeBumpError, CreateTxError, MiniscriptPsbtError, SignError}; use crate::psbt::PsbtUtils; use crate::signer::SignerError; use crate::types::*; use crate::wallet::coin_selection::Excess::{Change, NoChange}; +use crate::wallet::error::{BuildFeeBumpError, CreateTxError, MiniscriptPsbtError, SignError}; const COINBASE_MATURITY: u32 = 100; @@ -854,7 +855,7 @@ impl Wallet { /// # use bitcoin::*; /// # use bdk::*; /// # use bdk::wallet::ChangeSet; - /// # use bdk::error::CreateTxError; + /// # use bdk::wallet::error::CreateTxError; /// # use bdk_chain::PersistBackend; /// # let descriptor = "wpkh(tpubD6NzVbkrYhZ4Xferm7Pz4VnjdcDPFyjVu5K4iZXQ4pVN8Cks4pHVowTBXBKRhX64pkRyJZJN5xAKj4UDNnLPb5p2sSKXhewoYx5GbTdUFWq/*)"; /// # let mut wallet = doctest_wallet!(); @@ -1254,7 +1255,7 @@ impl Wallet { /// # use bitcoin::*; /// # use bdk::*; /// # use bdk::wallet::ChangeSet; - /// # use bdk::error::CreateTxError; + /// # use bdk::wallet::error::CreateTxError; /// # use bdk_chain::PersistBackend; /// # use anyhow::Error; /// # let descriptor = "wpkh(tpubD6NzVbkrYhZ4Xferm7Pz4VnjdcDPFyjVu5K4iZXQ4pVN8Cks4pHVowTBXBKRhX64pkRyJZJN5xAKj4UDNnLPb5p2sSKXhewoYx5GbTdUFWq/*)"; @@ -1428,7 +1429,7 @@ impl Wallet { /// # use bitcoin::*; /// # use bdk::*; /// # use bdk::wallet::ChangeSet; - /// # use bdk::error::{CreateTxError, SignError}; + /// # use bdk::wallet::error::{CreateTxError, SignError}; /// # use bdk_chain::PersistBackend; /// # let descriptor = "wpkh(tpubD6NzVbkrYhZ4Xferm7Pz4VnjdcDPFyjVu5K4iZXQ4pVN8Cks4pHVowTBXBKRhX64pkRyJZJN5xAKj4UDNnLPb5p2sSKXhewoYx5GbTdUFWq/*)"; /// # let mut wallet = doctest_wallet!(); diff --git a/crates/bdk/src/wallet/tx_builder.rs b/crates/bdk/src/wallet/tx_builder.rs index 0e007885d0..29d71c668b 100644 --- a/crates/bdk/src/wallet/tx_builder.rs +++ b/crates/bdk/src/wallet/tx_builder.rs @@ -18,7 +18,7 @@ //! # use bitcoin::*; //! # use bdk::*; //! # use bdk::wallet::ChangeSet; -//! # use bdk::error::CreateTxError; +//! # use bdk::wallet::error::CreateTxError; //! # use bdk::wallet::tx_builder::CreateTx; //! # let to_address = Address::from_str("2N4eQYCbKUHCCTUjBJeHcJp9ok6J2GZsTDt").unwrap().assume_checked(); //! # use bdk_chain::PersistBackend; @@ -51,8 +51,8 @@ use bitcoin::{absolute, script::PushBytes, OutPoint, ScriptBuf, Sequence, Transa use super::coin_selection::{CoinSelectionAlgorithm, DefaultCoinSelectionAlgorithm}; use super::ChangeSet; -use crate::error::{AddForeignUtxoError, AddUtxoError, AllowShrinkingError}; use crate::types::{FeeRate, KeychainKind, LocalUtxo, WeightedUtxo}; +use crate::wallet::error::{AddForeignUtxoError, AddUtxoError, AllowShrinkingError}; use crate::wallet::CreateTxError; use crate::{Utxo, Wallet}; @@ -85,7 +85,7 @@ impl TxBuilderContext for BumpFee {} /// # use bitcoin::*; /// # use core::str::FromStr; /// # use bdk::wallet::ChangeSet; -/// # use bdk::error::CreateTxError; +/// # use bdk::wallet::error::CreateTxError; /// # use bdk_chain::PersistBackend; /// # let mut wallet = doctest_wallet!(); /// # let addr1 = Address::from_str("2N4eQYCbKUHCCTUjBJeHcJp9ok6J2GZsTDt").unwrap().assume_checked(); @@ -652,7 +652,7 @@ impl<'a, D, Cs: CoinSelectionAlgorithm> TxBuilder<'a, D, Cs, CreateTx> { /// # use bitcoin::*; /// # use bdk::*; /// # use bdk::wallet::ChangeSet; - /// # use bdk::error::CreateTxError; + /// # use bdk::wallet::error::CreateTxError; /// # use bdk::wallet::tx_builder::CreateTx; /// # use bdk_chain::PersistBackend; /// # let to_address = diff --git a/crates/bdk/tests/wallet.rs b/crates/bdk/tests/wallet.rs index 3e57b8534a..dcf6fa06e0 100644 --- a/crates/bdk/tests/wallet.rs +++ b/crates/bdk/tests/wallet.rs @@ -1,9 +1,9 @@ use assert_matches::assert_matches; use bdk::descriptor::calc_checksum; -use bdk::error::{AddForeignUtxoError, CreateTxError, SignError}; use bdk::psbt::PsbtUtils; use bdk::signer::{SignOptions, SignerError}; use bdk::wallet::coin_selection::{self, LargestFirstCoinSelection}; +use bdk::wallet::error::{AddForeignUtxoError, CreateTxError, SignError}; use bdk::wallet::AddressIndex::*; use bdk::wallet::{AddressIndex, AddressInfo, Balance, Wallet}; use bdk::{FeeRate, KeychainKind};