Skip to content

Commit

Permalink
refactor!(bdk): require std feature for all impl of fmt::Display on E…
Browse files Browse the repository at this point in the history
…rrors

use anyhow::Error in more doc tests
  • Loading branch information
notmandatory committed Oct 27, 2023
1 parent 255785d commit acc9ed2
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 12 deletions.
3 changes: 2 additions & 1 deletion crates/bdk/src/descriptor/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// licenses.

//! Descriptor errors
#[cfg(feature = "std")]
use core::fmt;

/// Errors related to the parsing and usage of descriptors
Expand Down Expand Up @@ -55,6 +55,7 @@ impl From<crate::keys::KeyError> for Error {
}
}

#[cfg(feature = "std")]
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expand Down
3 changes: 3 additions & 0 deletions crates/bdk/src/descriptor/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ use crate::collections::{BTreeMap, HashSet, VecDeque};
use alloc::string::String;
use alloc::vec::Vec;
use core::cmp::max;

#[cfg(feature = "std")]
use core::fmt;

use serde::ser::SerializeMap;
Expand Down Expand Up @@ -518,6 +520,7 @@ pub enum PolicyError {
IncompatibleConditions,
}

#[cfg(feature = "std")]
impl fmt::Display for PolicyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expand Down
2 changes: 2 additions & 0 deletions crates/bdk/src/keys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::collections::HashSet;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::any::TypeId;
#[cfg(feature = "std")]
use core::fmt;
use core::marker::PhantomData;
use core::ops::Deref;
Expand Down Expand Up @@ -944,6 +945,7 @@ impl From<bip32::Error> for KeyError {
}
}

#[cfg(feature = "std")]
impl fmt::Display for KeyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expand Down
5 changes: 4 additions & 1 deletion crates/bdk/src/wallet/coin_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
//! # use bdk_chain::PersistBackend;
//! # use bdk::*;
//! # use bdk::wallet::coin_selection::decide_change;
//! # use anyhow::Error;
//! # const TXIN_BASE_WEIGHT: usize = (32 + 4 + 4) * 4;
//! #[derive(Debug)]
//! struct AlwaysSpendEverything;
Expand Down Expand Up @@ -96,7 +97,7 @@
//!
//! // inspect, sign, broadcast, ...
//!
//! # Ok::<(), CreateTxError<<() as PersistBackend<ChangeSet>>::WriteError>>(())
//! # Ok::<(), anyhow::Error>(())
//! ```
use crate::types::FeeRate;
Expand All @@ -109,6 +110,7 @@ use bitcoin::consensus::encode::serialize;
use bitcoin::{Script, Weight};

use core::convert::TryInto;
#[cfg(feature = "std")]
use core::fmt;
use core::fmt::Formatter;
use rand::seq::SliceRandom;
Expand Down Expand Up @@ -139,6 +141,7 @@ pub enum Error {
BnBTotalTriesExceeded,
}

#[cfg(feature = "std")]
impl fmt::Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Expand Down
2 changes: 2 additions & 0 deletions crates/bdk/src/wallet/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::wallet::coin_selection;
use crate::{descriptor, wallet, FeeRate, KeychainKind};
use alloc::string::String;
use bitcoin::{absolute, psbt, OutPoint, ScriptBuf, Sequence, Txid};
#[cfg(feature = "std")]
use core::fmt;

/// Errors returned by miniscript when updating inconsistent PSBTs
Expand All @@ -30,6 +31,7 @@ pub enum MiniscriptPsbtError {
OutputUpdate(miniscript::psbt::OutputUpdateError),
}

#[cfg(feature = "std")]
impl fmt::Display for MiniscriptPsbtError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expand Down
8 changes: 5 additions & 3 deletions crates/bdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ pub enum NewError<P> {
Persist(P),
}

#[cfg(feature = "std")]
impl<P> fmt::Display for NewError<P>
where
P: fmt::Display,
Expand Down Expand Up @@ -857,6 +858,7 @@ impl<D> Wallet<D> {
/// # use bdk::wallet::ChangeSet;
/// # use bdk::wallet::error::CreateTxError;
/// # use bdk_chain::PersistBackend;
/// # use anyhow::Error;
/// # let descriptor = "wpkh(tpubD6NzVbkrYhZ4Xferm7Pz4VnjdcDPFyjVu5K4iZXQ4pVN8Cks4pHVowTBXBKRhX64pkRyJZJN5xAKj4UDNnLPb5p2sSKXhewoYx5GbTdUFWq/*)";
/// # let mut wallet = doctest_wallet!();
/// # let to_address = Address::from_str("2N4eQYCbKUHCCTUjBJeHcJp9ok6J2GZsTDt").unwrap().assume_checked();
Expand All @@ -868,7 +870,7 @@ impl<D> Wallet<D> {
/// };
///
/// // sign and broadcast ...
/// # Ok::<(), CreateTxError<<() as PersistBackend<ChangeSet>>::WriteError>>(())
/// # Ok::<(), anyhow::Error>(())
/// ```
///
/// [`TxBuilder`]: crate::TxBuilder
Expand Down Expand Up @@ -1266,7 +1268,7 @@ impl<D> Wallet<D> {
/// builder
/// .add_recipient(to_address.script_pubkey(), 50_000)
/// .enable_rbf();
/// builder.finish().expect("psbt")
/// builder.finish()?
/// };
/// let _ = wallet.sign(&mut psbt, SignOptions::default())?;
/// let tx = psbt.extract_tx();
Expand All @@ -1275,7 +1277,7 @@ impl<D> Wallet<D> {
/// let mut builder = wallet.build_fee_bump(tx.txid())?;
/// builder
/// .fee_rate(bdk::FeeRate::from_sat_per_vb(5.0));
/// builder.finish().expect("psbt")
/// builder.finish()?
/// };
///
/// let _ = wallet.sign(&mut psbt, SignOptions::default())?;
Expand Down
1 change: 1 addition & 0 deletions crates/bdk/src/wallet/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ impl From<sighash::Error> for SignerError {
}
}

#[cfg(feature = "std")]
impl fmt::Display for SignerError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expand Down
11 changes: 7 additions & 4 deletions crates/bdk/src/wallet/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
//! # use bdk::wallet::ChangeSet;
//! # 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;
//! # use anyhow::Error;
//! # let to_address = Address::from_str("2N4eQYCbKUHCCTUjBJeHcJp9ok6J2GZsTDt").unwrap().assume_checked();
//! # let mut wallet = doctest_wallet!();
//! // create a TxBuilder from a wallet
//! let mut tx_builder = wallet.build_tx();
Expand All @@ -36,7 +37,7 @@
//! // Turn on RBF signaling
//! .enable_rbf();
//! let psbt = tx_builder.finish()?;
//! # Ok::<(), CreateTxError<<() as PersistBackend<ChangeSet>>::WriteError>>(())
//! # Ok::<(), anyhow::Error>(())
//! ```
use crate::collections::BTreeMap;
Expand Down Expand Up @@ -87,6 +88,7 @@ impl TxBuilderContext for BumpFee {}
/// # use bdk::wallet::ChangeSet;
/// # use bdk::wallet::error::CreateTxError;
/// # use bdk_chain::PersistBackend;
/// # use anyhow::Error;
/// # let mut wallet = doctest_wallet!();
/// # let addr1 = Address::from_str("2N4eQYCbKUHCCTUjBJeHcJp9ok6J2GZsTDt").unwrap().assume_checked();
/// # let addr2 = addr1.clone();
Expand All @@ -111,7 +113,7 @@ impl TxBuilderContext for BumpFee {}
/// };
///
/// assert_eq!(psbt1.unsigned_tx.output[..2], psbt2.unsigned_tx.output[..2]);
/// # Ok::<(), CreateTxError<<() as PersistBackend<ChangeSet>>::WriteError>>(())
/// # Ok::<(), anyhow::Error>(())
/// ```
///
/// At the moment [`coin_selection`] is an exception to the rule as it consumes `self`.
Expand Down Expand Up @@ -655,6 +657,7 @@ impl<'a, D, Cs: CoinSelectionAlgorithm> TxBuilder<'a, D, Cs, CreateTx> {
/// # use bdk::wallet::error::CreateTxError;
/// # use bdk::wallet::tx_builder::CreateTx;
/// # use bdk_chain::PersistBackend;
/// # use anyhow::Error;
/// # let to_address =
/// Address::from_str("2N4eQYCbKUHCCTUjBJeHcJp9ok6J2GZsTDt")
/// .unwrap()
Expand All @@ -670,7 +673,7 @@ impl<'a, D, Cs: CoinSelectionAlgorithm> TxBuilder<'a, D, Cs, CreateTx> {
/// .fee_rate(bdk::FeeRate::from_sat_per_vb(5.0))
/// .enable_rbf();
/// let psbt = tx_builder.finish()?;
/// # Ok::<(), CreateTxError<<() as PersistBackend<ChangeSet>>::WriteError>>(())
/// # Ok::<(), anyhow::Error>(())
/// ```
///
/// [`allow_shrinking`]: Self::allow_shrinking
Expand Down
3 changes: 0 additions & 3 deletions crates/bdk/tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,6 @@ fn test_create_tx_with_default_rbf_csv() {
}

#[test]
// #[should_panic(
// expected = "Cannot enable RBF with nSequence `Sequence(3)` given a required OP_CSV of `Sequence(6)`"
// )]
fn test_create_tx_with_custom_rbf_csv() {
let (mut wallet, _) = get_funded_wallet(get_test_single_sig_csv());
let addr = wallet.get_address(New);
Expand Down

0 comments on commit acc9ed2

Please sign in to comment.