Skip to content

Commit

Permalink
ManagedDecimal - SCDisplay, Display, Debug
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Jun 12, 2024
1 parent f2a3463 commit fada5b3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
33 changes: 31 additions & 2 deletions framework/base/src/types/managed/wrapped/managed_decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ use crate::{
const_handles, use_raw_handle, BigFloatApiImpl, BigIntApiImpl, ManagedTypeApi,
StaticVarApiImpl,
},
formatter::{FormatBuffer, FormatByteReceiver, SCDisplay},
types::{BigFloat, BigUint},
};

use alloc::string::ToString;
use multiversx_sc_codec::{
DecodeError, DecodeErrorHandler, EncodeErrorHandler, NestedDecode, NestedDecodeInput,
NestedEncode, NestedEncodeOutput, TopDecode, TopDecodeInput, TopEncode, TopEncodeOutput,
Expand All @@ -17,7 +19,7 @@ use core::{
ops::{Add, Deref, Div, Mul, Sub},
};

use super::ManagedRef;
use super::{ManagedBufferCachedBuilder, ManagedRef};

fn scaling_factor<M: ManagedTypeApi>(
num_decimals: NumDecimals,
Expand Down Expand Up @@ -71,7 +73,7 @@ impl<const DECIMALS: NumDecimals> Decimals for ConstDecimals<DECIMALS> {
}
}

#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct ManagedDecimal<M: ManagedTypeApi, D: Decimals> {
data: BigUint<M>,
decimals: D,
Expand Down Expand Up @@ -402,3 +404,30 @@ impl<M: ManagedTypeApi, const DECIMALS: NumDecimals> TypeAbi
false
}
}
impl<M: ManagedTypeApi, D: Decimals> SCDisplay for ManagedDecimal<M, D> {
fn fmt<F: FormatByteReceiver>(&self, f: &mut F) {
let sf = self.decimals.scaling_factor();
let temp = &self.data / sf.deref();
temp.fmt(f);
f.append_bytes(b".");
let temp = &self.data % sf.deref();
temp.fmt(f);
}
}

impl<M: ManagedTypeApi, D: Decimals> core::fmt::Display for ManagedDecimal<M, D> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let mut result = ManagedBufferCachedBuilder::<M>::new_from_slice(&[]);
result.append_display(self);
core::fmt::Display::fmt(&result.into_managed_buffer(), f)
}
}

impl<M: ManagedTypeApi, D: Decimals> core::fmt::Debug for ManagedDecimal<M, D> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("ManagedDecimal")
.field("handle", &self.data.handle.clone())
.field("number", &self.to_string())
.finish()
}
}
6 changes: 2 additions & 4 deletions framework/scenario/tests/managed_decimal_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,8 @@ pub fn test_managed_decimal() {
);

let float_1 = BigFloat::<StaticApi>::from_frac(3i64, 2i64);
let fixed_float_1 = ManagedDecimal::<StaticApi, ConstDecimals<1>>::from_big_float(
&float_1,
ConstDecimals::<1>,
);
let fixed_float_1 =
ManagedDecimal::<StaticApi, ConstDecimals<1>>::from_big_float(&float_1, ConstDecimals::<1>);
let fixed_float_2 = ManagedDecimal::<StaticApi, NumDecimals>::from_big_float(&float_1, 1usize);

assert_eq!(
Expand Down

0 comments on commit fada5b3

Please sign in to comment.