Skip to content

Commit d12513f

Browse files
committed
Merge #77: Revert encode_to_fmt API change
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
2 parents 3e93f95 + 0d01fad commit d12513f

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bech32"
3-
version = "0.9.0"
3+
version = "0.9.1"
44
authors = ["Clark Moody"]
55
repository = "https://github.com/rust-bitcoin/rust-bech32"
66
description = "Encodes and decodes the Bech32 format"

src/lib.rs

+23-25
Original file line numberDiff line numberDiff line change
@@ -401,51 +401,59 @@ fn check_hrp(hrp: &str) -> Result<Case, Error> {
401401
///
402402
/// # Errors
403403
/// * If [check_hrp] returns an error for the given HRP.
404-
/// * If `fmt` fails on write
405404
/// # Deviations from standard
406405
/// * No length limits are enforced for the data part
407406
pub fn encode_to_fmt<T: AsRef<[u5]>>(
408407
fmt: &mut fmt::Write,
409408
hrp: &str,
410409
data: T,
411410
variant: Variant,
412-
) -> Result<(), Error> {
411+
) -> Result<fmt::Result, Error> {
413412
let hrp_lower = match check_hrp(hrp)? {
414413
Case::Upper => Cow::Owned(hrp.to_lowercase()),
415414
Case::Lower | Case::None => Cow::Borrowed(hrp),
416415
};
417416

418-
let mut writer = Bech32Writer::new(&hrp_lower, variant, fmt)?;
419-
Ok(writer.write(data.as_ref()).and_then(|_| {
420-
// Finalize manually to avoid panic on drop if write fails
421-
writer.finalize()
422-
})?)
417+
match Bech32Writer::new(&hrp_lower, variant, fmt) {
418+
Ok(mut writer) => {
419+
Ok(writer.write(data.as_ref()).and_then(|_| {
420+
// Finalize manually to avoid panic on drop if write fails
421+
writer.finalize()
422+
}))
423+
}
424+
Err(e) => Ok(Err(e)),
425+
}
423426
}
424427

425428
/// Encode a bech32 payload without a checksum to an [fmt::Write].
426429
/// This method is intended for implementing traits from [std::fmt].
427430
///
428431
/// # Errors
429432
/// * If [check_hrp] returns an error for the given HRP.
430-
/// * If `fmt` fails on write
431433
/// # Deviations from standard
432434
/// * No length limits are enforced for the data part
433435
pub fn encode_without_checksum_to_fmt<T: AsRef<[u5]>>(
434436
fmt: &mut fmt::Write,
435437
hrp: &str,
436438
data: T,
437-
) -> Result<(), Error> {
439+
) -> Result<fmt::Result, Error> {
438440
let hrp = match check_hrp(hrp)? {
439441
Case::Upper => Cow::Owned(hrp.to_lowercase()),
440442
Case::Lower | Case::None => Cow::Borrowed(hrp),
441443
};
442444

443-
fmt.write_str(&hrp)?;
444-
fmt.write_char(SEP)?;
445+
if let Err(e) = fmt.write_str(&hrp) {
446+
return Ok(Err(e));
447+
}
448+
if let Err(e) = fmt.write_char(SEP) {
449+
return Ok(Err(e));
450+
}
445451
for b in data.as_ref() {
446-
fmt.write_char(b.to_char())?;
452+
if let Err(e) = fmt.write_char(b.to_char()) {
453+
return Ok(Err(e));
454+
}
447455
}
448-
Ok(())
456+
Ok(Ok(()))
449457
}
450458

451459
/// Used for encode/decode operations for the two variants of Bech32
@@ -486,7 +494,7 @@ impl Variant {
486494
/// * No length limits are enforced for the data part
487495
pub fn encode<T: AsRef<[u5]>>(hrp: &str, data: T, variant: Variant) -> Result<String, Error> {
488496
let mut buf = String::new();
489-
encode_to_fmt(&mut buf, hrp, data, variant)?;
497+
encode_to_fmt(&mut buf, hrp, data, variant)?.unwrap();
490498
Ok(buf)
491499
}
492500

@@ -498,7 +506,7 @@ pub fn encode<T: AsRef<[u5]>>(hrp: &str, data: T, variant: Variant) -> Result<St
498506
/// * No length limits are enforced for the data part
499507
pub fn encode_without_checksum<T: AsRef<[u5]>>(hrp: &str, data: T) -> Result<String, Error> {
500508
let mut buf = String::new();
501-
encode_without_checksum_to_fmt(&mut buf, hrp, data)?;
509+
encode_without_checksum_to_fmt(&mut buf, hrp, data)?.unwrap();
502510
Ok(buf)
503511
}
504512

@@ -668,14 +676,6 @@ pub enum Error {
668676
InvalidPadding,
669677
/// The whole string must be of one case
670678
MixedCase,
671-
/// Writing UTF-8 data failed
672-
WriteFailure(fmt::Error),
673-
}
674-
675-
impl From<fmt::Error> for Error {
676-
fn from(error: fmt::Error) -> Self {
677-
Self::WriteFailure(error)
678-
}
679679
}
680680

681681
impl fmt::Display for Error {
@@ -688,7 +688,6 @@ impl fmt::Display for Error {
688688
Error::InvalidData(n) => write!(f, "invalid data point ({})", n),
689689
Error::InvalidPadding => write!(f, "invalid padding"),
690690
Error::MixedCase => write!(f, "mixed-case strings not allowed"),
691-
Error::WriteFailure(_) => write!(f, "failed writing utf-8 data"),
692691
}
693692
}
694693
}
@@ -704,7 +703,6 @@ impl std::error::Error for Error {
704703
Error::InvalidData(_) => "invalid data point",
705704
Error::InvalidPadding => "invalid padding",
706705
Error::MixedCase => "mixed-case strings not allowed",
707-
Error::WriteFailure(_) => "failed writing utf-8 data",
708706
}
709707
}
710708
}

0 commit comments

Comments
 (0)