Skip to content

Commit 0ef5bb9

Browse files
committed
Add an Error variant for fmt::Error
This simplifies the return type of encode_to_fmt, which used a nested Result type.
1 parent 1735cb3 commit 0ef5bb9

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/lib.rs

+18-11
Original file line numberDiff line numberDiff line change
@@ -400,28 +400,25 @@ fn check_hrp(hrp: &str) -> Result<Case, Error> {
400400
///
401401
/// # Errors
402402
/// * If [check_hrp] returns an error for the given HRP.
403+
/// * If `fmt` fails on write
403404
/// # Deviations from standard
404405
/// * No length limits are enforced for the data part
405406
pub fn encode_to_fmt<T: AsRef<[u5]>>(
406407
fmt: &mut fmt::Write,
407408
hrp: &str,
408409
data: T,
409410
variant: Variant,
410-
) -> Result<fmt::Result, Error> {
411+
) -> Result<(), Error> {
411412
let hrp_lower = match check_hrp(hrp)? {
412413
Case::Upper => Cow::Owned(hrp.to_lowercase()),
413414
Case::Lower | Case::None => Cow::Borrowed(hrp),
414415
};
415416

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

427424
/// Used for encode/decode operations for the two variants of Bech32
@@ -462,7 +459,7 @@ impl Variant {
462459
/// * No length limits are enforced for the data part
463460
pub fn encode<T: AsRef<[u5]>>(hrp: &str, data: T, variant: Variant) -> Result<String, Error> {
464461
let mut buf = String::new();
465-
encode_to_fmt(&mut buf, hrp, data, variant)?.unwrap();
462+
encode_to_fmt(&mut buf, hrp, data, variant)?;
466463
Ok(buf)
467464
}
468465

@@ -624,6 +621,14 @@ pub enum Error {
624621
InvalidPadding,
625622
/// The whole string must be of one case
626623
MixedCase,
624+
/// Writing UTF-8 data failed
625+
WriteFailure(fmt::Error),
626+
}
627+
628+
impl From<fmt::Error> for Error {
629+
fn from(error: fmt::Error) -> Self {
630+
Self::WriteFailure(error)
631+
}
627632
}
628633

629634
impl fmt::Display for Error {
@@ -636,6 +641,7 @@ impl fmt::Display for Error {
636641
Error::InvalidData(n) => write!(f, "invalid data point ({})", n),
637642
Error::InvalidPadding => write!(f, "invalid padding"),
638643
Error::MixedCase => write!(f, "mixed-case strings not allowed"),
644+
Error::WriteFailure(_) => write!(f, "failed writing utf-8 data"),
639645
}
640646
}
641647
}
@@ -651,6 +657,7 @@ impl std::error::Error for Error {
651657
Error::InvalidData(_) => "invalid data point",
652658
Error::InvalidPadding => "invalid padding",
653659
Error::MixedCase => "mixed-case strings not allowed",
660+
Error::WriteFailure(_) => "failed writing utf-8 data",
654661
}
655662
}
656663
}

0 commit comments

Comments
 (0)