Skip to content

Commit

Permalink
Revert encode_to_fmt API change
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jkczyz committed Aug 12, 2022
1 parent 3e93f95 commit 3693935
Showing 1 changed file with 23 additions and 25 deletions.
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 3693935

Please sign in to comment.