Skip to content

Commit

Permalink
Return result from method
Browse files Browse the repository at this point in the history
Conditionally returns a result depending on the input.  This is in
preparation for changing the MAX and MIN values of SignedAmount.
  • Loading branch information
yancyribbens committed Dec 11, 2024
1 parent f9daef6 commit 8e4e6b1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
4 changes: 3 additions & 1 deletion units/src/amount/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ impl SerdeAmount for SignedAmount {
i64::serialize(&self.to_sat(), s)
}
fn des_sat<'d, D: Deserializer<'d>>(d: D, _: private::Token) -> Result<Self, D::Error> {
Ok(SignedAmount::from_sat(i64::deserialize(d)?))
use serde::de::Error;
SignedAmount::from_sat(i64::deserialize(d)?)
.map_err(D::Error::custom)
}
#[cfg(feature = "alloc")]
fn ser_btc<S: Serializer>(self, s: S, _: private::Token) -> Result<S::Ok, S::Error> {
Expand Down
24 changes: 21 additions & 3 deletions units/src/amount/signed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,20 @@ impl SignedAmount {
pub const MAX: SignedAmount = SignedAmount(i64::MAX);

/// Constructs a new [`SignedAmount`] with satoshi precision and the given number of satoshis.
pub const fn from_sat(satoshi: i64) -> SignedAmount { SignedAmount(satoshi) }
///
/// # Errors
///
/// On values exceeding [`SignedAmount::MAX`] or less than [`SignedAmount::MIN`]
#[allow(clippy::absurd_extreme_comparisons)]
pub const fn from_sat(satoshi: i64) -> Result<SignedAmount, OutOfRangeError> {
if satoshi < Self::MIN.0 {
Err(OutOfRangeError { is_signed: true, is_greater_than_max: false })
} else if satoshi > Self::MAX.0 {
Err(OutOfRangeError { is_signed: true, is_greater_than_max: true })
} else {
Ok(SignedAmount(satoshi))
}
}

/// Constructs a new [`SignedAmount`] with satoshi precision and the given number of satoshis.
///
Expand Down Expand Up @@ -98,7 +111,7 @@ impl SignedAmount {
/// per bitcoin overflows an `i64` type.
pub fn from_int_btc(btc: i64) -> Result<SignedAmount, OutOfRangeError> {
match btc.checked_mul(100_000_000) {
Some(amount) => Ok(SignedAmount::from_sat(amount)),
Some(amount) => SignedAmount::from_sat(amount),
None => Err(OutOfRangeError { is_signed: true, is_greater_than_max: true }),
}
}
Expand All @@ -112,7 +125,12 @@ impl SignedAmount {
/// per bitcoin overflows an `i64` type.
pub const fn from_int_btc_const(btc: i64) -> SignedAmount {
match btc.checked_mul(100_000_000) {
Some(amount) => SignedAmount::from_sat(amount),
Some(amount) => {
match SignedAmount::from_sat(amount) {
Ok(sa) => sa,
Err(_) => panic!("checked_mul overflow")
}
}
None => panic!("checked_mul overflowed"),
}
}
Expand Down
2 changes: 1 addition & 1 deletion units/src/amount/unsigned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ impl Amount {
if self.to_sat() > SignedAmount::MAX.to_sat() as u64 {
Err(OutOfRangeError::too_big(true))
} else {
Ok(SignedAmount::from_sat(self.to_sat() as i64))
SignedAmount::from_sat(self.to_sat() as i64)
}
}

Expand Down

0 comments on commit 8e4e6b1

Please sign in to comment.