Skip to content

Commit 11811e0

Browse files
committed
units: Hide amount decoder error internals
Introduces new policy on how we implement decoder error types. - All internals are hidden - No `From<OtherError>` impls - Private constructors - Inner enum is private (does not need non_exhaustive).
1 parent eb17995 commit 11811e0

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

units/src/amount/error.rs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,24 @@ impl std::error::Error for PossiblyConfusingDenominationError {
394394
/// An error consensus decoding an `Amount`.
395395
#[cfg(feature = "encoding")]
396396
#[derive(Debug, Clone, PartialEq, Eq)]
397-
#[non_exhaustive]
398-
pub enum AmountDecoderError {
397+
pub struct AmountDecoderError(pub(super) AmountDecoderErrorInner);
398+
399+
#[cfg(feature = "encoding")]
400+
impl AmountDecoderError {
401+
/// Constructs an EOF error.
402+
pub(super) fn eof(e: encoding::UnexpectedEofError) -> Self {
403+
Self(AmountDecoderErrorInner::UnexpectedEof(e))
404+
}
405+
406+
/// Constructs an out of range (`Amount::from_sat`) error.
407+
pub(super) fn out_of_range(e: OutOfRangeError) -> Self {
408+
Self(AmountDecoderErrorInner::OutOfRange(e))
409+
}
410+
}
411+
412+
#[cfg(feature = "encoding")]
413+
#[derive(Debug, Clone, PartialEq, Eq)]
414+
pub(super) enum AmountDecoderErrorInner {
399415
/// Not enough bytes given to decoder.
400416
UnexpectedEof(encoding::UnexpectedEofError),
401417
/// Decoded amount is too big.
@@ -407,27 +423,26 @@ impl From<Infallible> for AmountDecoderError {
407423
fn from(never: Infallible) -> Self { match never {} }
408424
}
409425

410-
#[cfg(feature = "encoding")]
411-
impl From<encoding::UnexpectedEofError> for AmountDecoderError {
412-
fn from(e: encoding::UnexpectedEofError) -> Self { Self::UnexpectedEof(e) }
413-
}
414-
415426
#[cfg(feature = "encoding")]
416427
impl fmt::Display for AmountDecoderError {
417428
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
418-
match *self {
419-
Self::UnexpectedEof(ref e) => write_err!(f, "decode error"; e),
420-
Self::OutOfRange(ref e) => write_err!(f, "decode error"; e),
429+
use AmountDecoderErrorInner as E;
430+
431+
match self.0 {
432+
E::UnexpectedEof(ref e) => write_err!(f, "decode error"; e),
433+
E::OutOfRange(ref e) => write_err!(f, "decode error"; e),
421434
}
422435
}
423436
}
424437

425438
#[cfg(all(feature = "std", feature = "encoding"))]
426439
impl std::error::Error for AmountDecoderError {
427440
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
428-
match *self {
429-
Self::UnexpectedEof(ref e) => Some(e),
430-
Self::OutOfRange(ref e) => Some(e),
441+
use AmountDecoderErrorInner as E;
442+
443+
match self.0 {
444+
E::UnexpectedEof(ref e) => Some(e),
445+
E::OutOfRange(ref e) => Some(e),
431446
}
432447
}
433448
}

units/src/amount/unsigned.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -589,13 +589,13 @@ impl encoding::Decoder for AmountDecoder {
589589

590590
#[inline]
591591
fn push_bytes(&mut self, bytes: &mut &[u8]) -> Result<bool, Self::Error> {
592-
Ok(self.0.push_bytes(bytes)?)
592+
self.0.push_bytes(bytes).map_err(AmountDecoderError::eof)
593593
}
594594

595595
#[inline]
596596
fn end(self) -> Result<Self::Output, Self::Error> {
597-
let a = u64::from_le_bytes(self.0.end()?);
598-
Ok(Amount::from_sat(a).map_err(AmountDecoderError::OutOfRange)?)
597+
let a = u64::from_le_bytes(self.0.end().map_err(AmountDecoderError::eof)?);
598+
Amount::from_sat(a).map_err(AmountDecoderError::out_of_range)
599599
}
600600
}
601601

0 commit comments

Comments
 (0)