feat: implement TryFrom trait to SignedAmount and Amount

This commit is contained in:
Sumit Kumar 2024-02-09 18:15:37 +05:30
parent 53461f71c9
commit 251579f4ef
No known key found for this signature in database
GPG Key ID: 5FF7BE789B5BA99B
1 changed files with 53 additions and 3 deletions

View File

@ -865,9 +865,9 @@ impl Amount {
pub fn checked_rem(self, rhs: u64) -> Option<Amount> { self.0.checked_rem(rhs).map(Amount) }
/// Convert to a signed amount.
pub fn to_signed(self) -> Result<SignedAmount, ParseAmountError> {
pub fn to_signed(self) -> Result<SignedAmount, OutOfRangeError> {
if self.to_sat() > SignedAmount::MAX.to_sat() as u64 {
Err(ParseAmountError::OutOfRange(OutOfRangeError::too_big(true)))
Err(OutOfRangeError::too_big(true))
} else {
Ok(SignedAmount::from_sat(self.to_sat() as i64))
}
@ -957,6 +957,14 @@ impl FromStr for Amount {
fn from_str(s: &str) -> Result<Self, Self::Err> { Amount::from_str_with_denomination(s) }
}
impl TryFrom<SignedAmount> for Amount {
type Error = OutOfRangeError;
fn try_from(value: SignedAmount) -> Result<Self, Self::Error> {
value.to_unsigned()
}
}
impl core::iter::Sum for Amount {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
let sats: u64 = iter.map(|amt| amt.0).sum();
@ -1334,6 +1342,14 @@ impl FromStr for SignedAmount {
fn from_str(s: &str) -> Result<Self, Self::Err> { SignedAmount::from_str_with_denomination(s) }
}
impl TryFrom<Amount> for SignedAmount {
type Error = OutOfRangeError;
fn try_from(value: Amount) -> Result<Self, Self::Error> {
value.to_signed()
}
}
impl core::iter::Sum for SignedAmount {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
let sats: i64 = iter.map(|amt| amt.0).sum();
@ -1704,7 +1720,7 @@ mod verification {
if n1 <= i64::MAX as u64 {
Ok(SignedAmount::from_sat(n1.try_into().unwrap()))
} else {
Err(ParseAmountError::OutOfRange(OutOfRangeError::too_big(true)))
Err(OutOfRangeError::too_big(true))
},
);
}
@ -1835,6 +1851,40 @@ mod tests {
#[test]
fn from_int_btc_panic() { Amount::from_int_btc(u64::MAX); }
#[test]
fn test_signed_amount_try_from_amount() {
let ua_positive = Amount::from_sat(123);
let sa_positive = SignedAmount::try_from(ua_positive).unwrap();
assert_eq!(sa_positive, SignedAmount(123));
let ua_max = Amount::MAX;
let result = SignedAmount::try_from(ua_max);
assert_eq!(
result,
Err(OutOfRangeError {
is_signed: true,
is_greater_than_max: true
})
);
}
#[test]
fn test_amount_try_from_signed_amount() {
let sa_positive = SignedAmount(123);
let ua_positive = Amount::try_from(sa_positive).unwrap();
assert_eq!(ua_positive, Amount::from_sat(123));
let sa_negative = SignedAmount(-123);
let result = Amount::try_from(sa_negative);
assert_eq!(
result,
Err(OutOfRangeError {
is_signed: false,
is_greater_than_max: false
})
);
}
#[test]
fn mul_div() {
let sat = Amount::from_sat;