Skip to content

Commit

Permalink
Add From and TryFrom impl between decimals
Browse files Browse the repository at this point in the history
  • Loading branch information
chipshort committed Sep 14, 2023
1 parent 355447b commit 0f6ba5b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 11 deletions.
18 changes: 14 additions & 4 deletions packages/std/src/math/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::errors::{
CheckedFromRatioError, CheckedMultiplyRatioError, DivideByZeroError, OverflowError,
OverflowOperation, RoundUpOverflowError, StdError,
};
use crate::{forward_ref_partial_eq, Decimal256, SignedDecimal};
use crate::{forward_ref_partial_eq, Decimal256, SignedDecimal, SignedDecimal256};

use super::Fraction;
use super::Isqrt;
Expand Down Expand Up @@ -516,9 +516,19 @@ impl TryFrom<SignedDecimal> for Decimal {
fn try_from(value: SignedDecimal) -> Result<Self, Self::Error> {
value
.atomics()
.i128()
.try_into() // convert to u128
.map(Uint128::new)
.try_into()
.map(Decimal)
.map_err(|_| DecimalRangeExceeded)
}
}

impl TryFrom<SignedDecimal256> for Decimal {
type Error = DecimalRangeExceeded;

fn try_from(value: SignedDecimal256) -> Result<Self, Self::Error> {
value
.atomics()
.try_into()
.map(Decimal)
.map_err(|_| DecimalRangeExceeded)
}
Expand Down
26 changes: 25 additions & 1 deletion packages/std/src/math/decimal256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::errors::{
CheckedFromRatioError, CheckedMultiplyRatioError, DivideByZeroError, OverflowError,
OverflowOperation, RoundUpOverflowError, StdError,
};
use crate::{forward_ref_partial_eq, Decimal, Uint512};
use crate::{forward_ref_partial_eq, Decimal, SignedDecimal, SignedDecimal256, Uint512};

use super::Fraction;
use super::Isqrt;
Expand Down Expand Up @@ -525,6 +525,30 @@ impl From<Decimal> for Decimal256 {
}
}

impl TryFrom<SignedDecimal> for Decimal256 {
type Error = Decimal256RangeExceeded;

fn try_from(value: SignedDecimal) -> Result<Self, Self::Error> {
value
.atomics()
.try_into()
.map(Decimal256)
.map_err(|_| Decimal256RangeExceeded)
}
}

impl TryFrom<SignedDecimal256> for Decimal256 {
type Error = Decimal256RangeExceeded;

fn try_from(value: SignedDecimal256) -> Result<Self, Self::Error> {
value
.atomics()
.try_into()
.map(Decimal256)
.map_err(|_| Decimal256RangeExceeded)
}
}

impl FromStr for Decimal256 {
type Err = StdError;

Expand Down
18 changes: 14 additions & 4 deletions packages/std/src/math/signed_decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::errors::{
CheckedFromRatioError, CheckedMultiplyRatioError, DivideByZeroError, OverflowError,
OverflowOperation, RoundDownOverflowError, RoundUpOverflowError, StdError,
};
use crate::{forward_ref_partial_eq, Decimal, Int256, SignedDecimal256};
use crate::{forward_ref_partial_eq, Decimal, Decimal256, Int256, SignedDecimal256};

use super::Fraction;
use super::Int128;
Expand Down Expand Up @@ -631,9 +631,19 @@ impl TryFrom<Decimal> for SignedDecimal {
fn try_from(value: Decimal) -> Result<Self, Self::Error> {
value
.atomics()
.u128()
.try_into() // convert to i128
.map(Int128::new)
.try_into()
.map(SignedDecimal)
.map_err(|_| SignedDecimalRangeExceeded)
}
}

impl TryFrom<Decimal256> for SignedDecimal {
type Error = SignedDecimalRangeExceeded;

fn try_from(value: Decimal256) -> Result<Self, Self::Error> {
value
.atomics()
.try_into()
.map(SignedDecimal)
.map_err(|_| SignedDecimalRangeExceeded)
}
Expand Down
22 changes: 20 additions & 2 deletions packages/std/src/math/signed_decimal_256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::errors::{
CheckedFromRatioError, CheckedMultiplyRatioError, DivideByZeroError, OverflowError,
OverflowOperation, RoundDownOverflowError, RoundUpOverflowError, StdError,
};
use crate::{forward_ref_partial_eq, Decimal256, Int512, SignedDecimal};
use crate::{forward_ref_partial_eq, Decimal, Decimal256, Int512, SignedDecimal};

use super::Fraction;
use super::Int256;
Expand Down Expand Up @@ -633,7 +633,25 @@ impl Neg for SignedDecimal256 {

impl From<SignedDecimal> for SignedDecimal256 {
fn from(value: SignedDecimal) -> Self {
Self::from_atomics(value.atomics(), SignedDecimal::DECIMAL_PLACES).unwrap()
Self::new(value.atomics().into())
}
}

impl From<Decimal> for SignedDecimal256 {
fn from(value: Decimal) -> Self {
Self::new(value.atomics().into())
}
}

impl TryFrom<Decimal256> for SignedDecimal256 {
type Error = SignedDecimal256RangeExceeded;

fn try_from(value: Decimal256) -> Result<Self, Self::Error> {
value
.atomics()
.try_into()
.map(SignedDecimal256)
.map_err(|_| SignedDecimal256RangeExceeded)
}
}

Expand Down

0 comments on commit 0f6ba5b

Please sign in to comment.