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 15, 2024
1 parent 3d4b297 commit f4fede3
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion api/units/all-features.txt
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ pub const fn bitcoin_units::SignedAmount::checked_mul(self, rhs: i64) -> core::o
pub const fn bitcoin_units::SignedAmount::checked_rem(self, rhs: i64) -> core::option::Option<bitcoin_units::SignedAmount>
pub const fn bitcoin_units::SignedAmount::checked_sub(self, rhs: bitcoin_units::SignedAmount) -> core::option::Option<bitcoin_units::SignedAmount>
pub const fn bitcoin_units::SignedAmount::from_int_btc_const(btc: i64) -> bitcoin_units::SignedAmount
pub const fn bitcoin_units::SignedAmount::from_sat(satoshi: i64) -> bitcoin_units::SignedAmount
pub const fn bitcoin_units::SignedAmount::from_sat(satoshi: i64) -> core::result::Result<bitcoin_units::SignedAmount, bitcoin_units::amount::error::OutOfRangeError>
pub const fn bitcoin_units::SignedAmount::from_sat_unchecked(satoshi: i64) -> bitcoin_units::SignedAmount
pub const fn bitcoin_units::SignedAmount::to_sat(self) -> i64
pub const fn bitcoin_units::block::BlockHeight::from_u32(inner: u32) -> Self
Expand Down
2 changes: 1 addition & 1 deletion api/units/alloc-only.txt
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ pub const fn bitcoin_units::SignedAmount::checked_mul(self, rhs: i64) -> core::o
pub const fn bitcoin_units::SignedAmount::checked_rem(self, rhs: i64) -> core::option::Option<bitcoin_units::SignedAmount>
pub const fn bitcoin_units::SignedAmount::checked_sub(self, rhs: bitcoin_units::SignedAmount) -> core::option::Option<bitcoin_units::SignedAmount>
pub const fn bitcoin_units::SignedAmount::from_int_btc_const(btc: i64) -> bitcoin_units::SignedAmount
pub const fn bitcoin_units::SignedAmount::from_sat(satoshi: i64) -> bitcoin_units::SignedAmount
pub const fn bitcoin_units::SignedAmount::from_sat(satoshi: i64) -> core::result::Result<bitcoin_units::SignedAmount, bitcoin_units::amount::error::OutOfRangeError>
pub const fn bitcoin_units::SignedAmount::from_sat_unchecked(satoshi: i64) -> bitcoin_units::SignedAmount
pub const fn bitcoin_units::SignedAmount::to_sat(self) -> i64
pub const fn bitcoin_units::block::BlockHeight::from_u32(inner: u32) -> Self
Expand Down
2 changes: 1 addition & 1 deletion api/units/no-features.txt
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ pub const fn bitcoin_units::SignedAmount::checked_mul(self, rhs: i64) -> core::o
pub const fn bitcoin_units::SignedAmount::checked_rem(self, rhs: i64) -> core::option::Option<bitcoin_units::SignedAmount>
pub const fn bitcoin_units::SignedAmount::checked_sub(self, rhs: bitcoin_units::SignedAmount) -> core::option::Option<bitcoin_units::SignedAmount>
pub const fn bitcoin_units::SignedAmount::from_int_btc_const(btc: i64) -> bitcoin_units::SignedAmount
pub const fn bitcoin_units::SignedAmount::from_sat(satoshi: i64) -> bitcoin_units::SignedAmount
pub const fn bitcoin_units::SignedAmount::from_sat(satoshi: i64) -> core::result::Result<bitcoin_units::SignedAmount, bitcoin_units::amount::error::OutOfRangeError>
pub const fn bitcoin_units::SignedAmount::from_sat_unchecked(satoshi: i64) -> bitcoin_units::SignedAmount
pub const fn bitcoin_units::SignedAmount::to_sat(self) -> i64
pub const fn bitcoin_units::block::BlockHeight::from_u32(inner: u32) -> Self
Expand Down
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
23 changes: 20 additions & 3 deletions units/src/amount/signed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,19 @@ impl SignedAmount {
pub const MAX: Self = SignedAmount::MAX_MONEY;

/// 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`]
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 @@ -97,7 +109,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 @@ -111,7 +123,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 @@ -415,7 +415,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 f4fede3

Please sign in to comment.