From d692972844e3d8de2d5d5f40b92e0f3bd36a83cf Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 9 Jan 2024 12:24:28 +1100 Subject: [PATCH 1/2] Add rustfmt::skip to Display impl `Display` imlps often include long lines but the code is simple and having it split over 4 lines is a waist of vertical space. --- src/primitives/decode.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/primitives/decode.rs b/src/primitives/decode.rs index 386078685..f3c6ff0ca 100644 --- a/src/primitives/decode.rs +++ b/src/primitives/decode.rs @@ -589,6 +589,7 @@ pub enum SegwitHrpstringError { Checksum(ChecksumError), } +#[rustfmt::skip] impl fmt::Display for SegwitHrpstringError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { use SegwitHrpstringError::*; @@ -596,12 +597,8 @@ impl fmt::Display for SegwitHrpstringError { match *self { Unchecked(ref e) => write_err!(f, "parsing unchecked hrpstring failed"; e), NoData => write!(f, "no data found after removing the checksum"), - TooLong(len) => write!( - f, - "encoded length {} exceeds spec limit {} chars", - len, - segwit::MAX_STRING_LENGTH - ), + TooLong(len) => + write!(f, "encoded length {} exceeds spec limit {} chars", len, segwit::MAX_STRING_LENGTH), InvalidWitnessVersion(fe) => write!(f, "invalid segwit witness version: {}", fe), Padding(ref e) => write_err!(f, "invalid padding on the witness data"; e), WitnessLength(ref e) => write_err!(f, "invalid witness length"; e), From e988f3462f2df36a13ea89a44f5298c0cf6266cf Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 8 Jan 2024 09:17:40 +1100 Subject: [PATCH 2/2] Improve error display for invalid witness version We have two errors that cover invalid witness version, for one we print the field element which can be confusing for some values and for the other we omit the invalid version all together - we can do better. Print the field element as well as the integer value when displaying the two invalid witness version errors. Fix: #162 --- src/primitives/decode.rs | 3 ++- src/primitives/segwit.rs | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/primitives/decode.rs b/src/primitives/decode.rs index f3c6ff0ca..5aaab0375 100644 --- a/src/primitives/decode.rs +++ b/src/primitives/decode.rs @@ -599,7 +599,8 @@ impl fmt::Display for SegwitHrpstringError { NoData => write!(f, "no data found after removing the checksum"), TooLong(len) => write!(f, "encoded length {} exceeds spec limit {} chars", len, segwit::MAX_STRING_LENGTH), - InvalidWitnessVersion(fe) => write!(f, "invalid segwit witness version: {}", fe), + InvalidWitnessVersion(fe) => + write!(f, "invalid segwit witness version: {} (bech32 character: '{}')", fe.to_u8(), fe), Padding(ref e) => write_err!(f, "invalid padding on the witness data"; e), WitnessLength(ref e) => write_err!(f, "invalid witness length"; e), Checksum(ref e) => write_err!(f, "invalid checksum"; e), diff --git a/src/primitives/segwit.rs b/src/primitives/segwit.rs index 94f5cd7a6..bf4ddba74 100644 --- a/src/primitives/segwit.rs +++ b/src/primitives/segwit.rs @@ -65,9 +65,10 @@ pub fn validate_witness_program_length( #[non_exhaustive] pub struct InvalidWitnessVersionError(pub Fe32); +#[rustfmt::skip] impl fmt::Display for InvalidWitnessVersionError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "field element does not represent a valid witness version") + write!(f, "invalid segwit witness version: {} (bech32 character: '{}')", self.0.to_u8(), self.0) } }