From 36939353de20abdd65744e87ca4e365b0f4278cc Mon Sep 17 00:00:00 2001 From: Jeffrey Czyz Date: Fri, 12 Aug 2022 08:27:11 -0500 Subject: [PATCH 1/2] Revert encode_to_fmt API change Maintaining the original error type allows for making a minor release since it won't break the API. This lets indirect consumers of the crate through rust-bitcoin's module re-export to use new functions (e.g., encode_without_checksum) without a new rust-bitcoin release. --- src/lib.rs | 48 +++++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 293d5b1bb..50cf64d35 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -401,7 +401,6 @@ fn check_hrp(hrp: &str) -> Result { /// /// # Errors /// * If [check_hrp] returns an error for the given HRP. -/// * If `fmt` fails on write /// # Deviations from standard /// * No length limits are enforced for the data part pub fn encode_to_fmt>( @@ -409,17 +408,21 @@ pub fn encode_to_fmt>( hrp: &str, data: T, variant: Variant, -) -> Result<(), Error> { +) -> Result { let hrp_lower = match check_hrp(hrp)? { Case::Upper => Cow::Owned(hrp.to_lowercase()), Case::Lower | Case::None => Cow::Borrowed(hrp), }; - let mut writer = Bech32Writer::new(&hrp_lower, variant, fmt)?; - Ok(writer.write(data.as_ref()).and_then(|_| { - // Finalize manually to avoid panic on drop if write fails - writer.finalize() - })?) + match Bech32Writer::new(&hrp_lower, variant, fmt) { + Ok(mut writer) => { + Ok(writer.write(data.as_ref()).and_then(|_| { + // Finalize manually to avoid panic on drop if write fails + writer.finalize() + })) + } + Err(e) => Ok(Err(e)), + } } /// Encode a bech32 payload without a checksum to an [fmt::Write]. @@ -427,25 +430,30 @@ pub fn encode_to_fmt>( /// /// # Errors /// * If [check_hrp] returns an error for the given HRP. -/// * If `fmt` fails on write /// # Deviations from standard /// * No length limits are enforced for the data part pub fn encode_without_checksum_to_fmt>( fmt: &mut fmt::Write, hrp: &str, data: T, -) -> Result<(), Error> { +) -> Result { let hrp = match check_hrp(hrp)? { Case::Upper => Cow::Owned(hrp.to_lowercase()), Case::Lower | Case::None => Cow::Borrowed(hrp), }; - fmt.write_str(&hrp)?; - fmt.write_char(SEP)?; + if let Err(e) = fmt.write_str(&hrp) { + return Ok(Err(e)); + } + if let Err(e) = fmt.write_char(SEP) { + return Ok(Err(e)); + } for b in data.as_ref() { - fmt.write_char(b.to_char())?; + if let Err(e) = fmt.write_char(b.to_char()) { + return Ok(Err(e)); + } } - Ok(()) + Ok(Ok(())) } /// Used for encode/decode operations for the two variants of Bech32 @@ -486,7 +494,7 @@ impl Variant { /// * No length limits are enforced for the data part pub fn encode>(hrp: &str, data: T, variant: Variant) -> Result { let mut buf = String::new(); - encode_to_fmt(&mut buf, hrp, data, variant)?; + encode_to_fmt(&mut buf, hrp, data, variant)?.unwrap(); Ok(buf) } @@ -498,7 +506,7 @@ pub fn encode>(hrp: &str, data: T, variant: Variant) -> Result>(hrp: &str, data: T) -> Result { let mut buf = String::new(); - encode_without_checksum_to_fmt(&mut buf, hrp, data)?; + encode_without_checksum_to_fmt(&mut buf, hrp, data)?.unwrap(); Ok(buf) } @@ -668,14 +676,6 @@ pub enum Error { InvalidPadding, /// The whole string must be of one case MixedCase, - /// Writing UTF-8 data failed - WriteFailure(fmt::Error), -} - -impl From for Error { - fn from(error: fmt::Error) -> Self { - Self::WriteFailure(error) - } } impl fmt::Display for Error { @@ -688,7 +688,6 @@ impl fmt::Display for Error { Error::InvalidData(n) => write!(f, "invalid data point ({})", n), Error::InvalidPadding => write!(f, "invalid padding"), Error::MixedCase => write!(f, "mixed-case strings not allowed"), - Error::WriteFailure(_) => write!(f, "failed writing utf-8 data"), } } } @@ -704,7 +703,6 @@ impl std::error::Error for Error { Error::InvalidData(_) => "invalid data point", Error::InvalidPadding => "invalid padding", Error::MixedCase => "mixed-case strings not allowed", - Error::WriteFailure(_) => "failed writing utf-8 data", } } } From 0d01fadfc4e32853ab6aff18a51b062d33630179 Mon Sep 17 00:00:00 2001 From: Jeffrey Czyz Date: Fri, 12 Aug 2022 12:33:22 -0500 Subject: [PATCH 2/2] Bump version to 0.9.1 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 0c9700de8..d4f2391e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bech32" -version = "0.9.0" +version = "0.9.1" authors = ["Clark Moody"] repository = "https://github.com/rust-bitcoin/rust-bech32" description = "Encodes and decodes the Bech32 format"