Skip to content

Revert encode_to_fmt API change #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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