From e2014cba1b1d11316753cfbf5cf3b2b16b0bd7f9 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Fri, 4 Aug 2023 10:12:45 +1000 Subject: [PATCH] Import error variants within dislay impl In an effort to reduce the number of lines of code import the error variants locally within the `Display` impl on `Error`. Refactor only, no logic changes. --- bitcoin/src/address/error.rs | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/bitcoin/src/address/error.rs b/bitcoin/src/address/error.rs index c82189a9..bb46cf0c 100644 --- a/bitcoin/src/address/error.rs +++ b/bitcoin/src/address/error.rs @@ -49,28 +49,29 @@ pub enum Error { impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use Error::*; + match *self { - Error::Base58(ref e) => write_err!(f, "base58 address encoding error"; e), - Error::Bech32(ref e) => write_err!(f, "bech32 address encoding error"; e), - Error::EmptyBech32Payload => write!(f, "the bech32 payload was empty"), - Error::InvalidBech32Variant { expected, found } => write!( + Base58(ref e) => write_err!(f, "base58 address encoding error"; e), + Bech32(ref e) => write_err!(f, "bech32 address encoding error"; e), + EmptyBech32Payload => write!(f, "the bech32 payload was empty"), + InvalidBech32Variant { expected, found } => write!( f, "invalid bech32 checksum variant found {:?} when {:?} was expected", found, expected ), - Error::WitnessVersion(ref e) => write_err!(f, "witness version construction error"; e), - Error::WitnessProgram(ref e) => write_err!(f, "witness program error"; e), - Error::UncompressedPubkey => + WitnessVersion(ref e) => write_err!(f, "witness version construction error"; e), + WitnessProgram(ref e) => write_err!(f, "witness program error"; e), + UncompressedPubkey => write!(f, "an uncompressed pubkey was used where it is not allowed"), - Error::ExcessiveScriptSize => write!(f, "script size exceed 520 bytes"), - Error::UnrecognizedScript => - write!(f, "script is not a p2pkh, p2sh or witness program"), - Error::UnknownAddressType(ref s) => write!( + ExcessiveScriptSize => write!(f, "script size exceed 520 bytes"), + UnrecognizedScript => write!(f, "script is not a p2pkh, p2sh or witness program"), + UnknownAddressType(ref s) => write!( f, "unknown address type: '{}' is either invalid or not supported in rust-bitcoin", s ), - Error::NetworkValidation { required, found, ref address } => { + NetworkValidation { required, found, ref address } => { write!(f, "address ")?; address.fmt_internal(f)?; // Using fmt_internal in order to remove the "Address(..)" wrapper write!(