Skip to content

Commit

Permalink
f use the new error trait in a non-generic error type
Browse files Browse the repository at this point in the history
  • Loading branch information
apoelstra committed Feb 4, 2024
1 parent e484fca commit 99cb287
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
17 changes: 8 additions & 9 deletions bitcoin/src/address/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use core::fmt;

use internals::write_err;
use internals::{InternalsToken, impl_error_traits, error::StdError, error::StdSource};

use crate::address::{Address, NetworkUnchecked};
use crate::blockdata::script::{witness_program, witness_version};
Expand Down Expand Up @@ -32,28 +33,25 @@ pub enum Error {
UnknownHrp(UnknownHrpError),
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
impl StdError for Error {
fn fmt_without_source(&self, f: &mut fmt::Formatter, _: &InternalsToken) -> fmt::Result {
use Error::*;

match *self {
WitnessVersion(ref e) => write_err!(f, "witness version construction error"; e),
WitnessProgram(ref e) => write_err!(f, "witness program error"; e),
WitnessVersion(..) => write!(f, "witness version construction error"),
WitnessProgram(..) => write!(f, "witness program error"),
ExcessiveScriptSize => write!(f, "script size exceed 520 bytes"),
UnrecognizedScript => write!(f, "script is not a p2pkh, p2sh or witness program"),
NetworkValidation { required, ref address } => {
write!(f, "address ")?;
fmt::Display::fmt(&address.0, f)?;
write!(f, " is not valid on {}", required)
}
Error::UnknownHrp(ref e) => write_err!(f, "unknown hrp"; e),
Error::UnknownHrp(..) => write!(f, "unknown hrp"),
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
fn source(&self, _: &InternalsToken) -> Option<&StdSource> {
use Error::*;

match self {
Expand All @@ -64,6 +62,7 @@ impl std::error::Error for Error {
}
}
}
impl_error_traits!(Error);

impl From<witness_version::TryFromError> for Error {
fn from(e: witness_version::TryFromError) -> Error { Error::WitnessVersion(e) }
Expand Down
31 changes: 31 additions & 0 deletions internals/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,34 @@ pub fn fmt_generic_source<T: fmt::Display + ?Sized>(source: Option<&T>, f: &mut
}
}

/// Helper function for the `impl_error_traits` macro. Should not be called directly.
#[cfg(feature = "std")]
pub fn std_source<T: StdError>(err: &T) -> Option<&(dyn std::error::Error + 'static)> {
StdError::source(err, &InternalsToken::private())
}

/// Helper function for the `impl_error_traits` macro. Should not be called directly.
pub fn fmt_error<T: StdError>(err: &T, f: &mut fmt::Formatter) -> fmt::Result {
StdError::fmt(err, f, &InternalsToken::private())
}

/// Annoyingly this can't be done automatically with a blanket-impl because Rust
/// doesn't allow it.
#[macro_export]
macro_rules! impl_error_traits {
($obj:ty) => {
#[cfg(feature = "std")]
impl ::std::error::Error for $obj {
fn source(&self) -> Option<&(dyn ::std::error::Error + 'static)> {
$crate::error::std_source(self)
}
}

impl ::core::fmt::Display for $obj {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
$crate::error::fmt_error(self, f)
}
}
}
}

0 comments on commit 99cb287

Please sign in to comment.