Skip to content

Commit

Permalink
Merge #77: Revert encode_to_fmt API change
Browse files Browse the repository at this point in the history
0d01fad Bump version to 0.9.1 (Jeffrey Czyz)
3693935 Revert encode_to_fmt API change (Jeffrey Czyz)

Pull request description:

  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.

ACKs for top commit:
  apoelstra:
    ACK 0d01fad

Tree-SHA512: 6793615d5e50286126760e47aa4a0b699d979fb79a248ce61a9497090fa69de97f0fc5bb5b1a2eb7c99a78fc83ecf7dcef9854d9c870ed2e80de413d71134430
  • Loading branch information
apoelstra committed Aug 12, 2022
2 parents 3e93f95 + 0d01fad commit d12513f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
48 changes: 23 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,51 +401,59 @@ fn check_hrp(hrp: &str) -> Result<Case, Error> {
///
/// # 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<T: AsRef<[u5]>>(
fmt: &mut fmt::Write,
hrp: &str,
data: T,
variant: Variant,
) -> Result<(), Error> {
) -> Result<fmt::Result, Error> {
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].
/// This method is intended for implementing traits from [std::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<T: AsRef<[u5]>>(
fmt: &mut fmt::Write,
hrp: &str,
data: T,
) -> Result<(), Error> {
) -> Result<fmt::Result, Error> {
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
Expand Down Expand Up @@ -486,7 +494,7 @@ impl Variant {
/// * No length limits are enforced for the data part
pub fn encode<T: AsRef<[u5]>>(hrp: &str, data: T, variant: Variant) -> Result<String, Error> {
let mut buf = String::new();
encode_to_fmt(&mut buf, hrp, data, variant)?;
encode_to_fmt(&mut buf, hrp, data, variant)?.unwrap();
Ok(buf)
}

Expand All @@ -498,7 +506,7 @@ pub fn encode<T: AsRef<[u5]>>(hrp: &str, data: T, variant: Variant) -> Result<St
/// * No length limits are enforced for the data part
pub fn encode_without_checksum<T: AsRef<[u5]>>(hrp: &str, data: T) -> Result<String, Error> {
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)
}

Expand Down Expand Up @@ -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<fmt::Error> for Error {
fn from(error: fmt::Error) -> Self {
Self::WriteFailure(error)
}
}

impl fmt::Display for Error {
Expand All @@ -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"),
}
}
}
Expand All @@ -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",
}
}
}
Expand Down

0 comments on commit d12513f

Please sign in to comment.