Skip to content

Commit

Permalink
chore: add hex and display formatting for int field
Browse files Browse the repository at this point in the history
  • Loading branch information
ya7on committed Feb 15, 2025
1 parent 90c6dc8 commit a3ab1ed
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/types/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::to_cell::ToCell;
use crate::utils::CastErrorToAnyhow;
use num_bigint::BigInt;
use num_bigint::Sign::Minus;
use std::fmt::Debug;
use std::fmt::{Debug, Display, Formatter, LowerHex, UpperHex};
use tonlib_core::cell::{CellBuilder, CellParser};

#[derive(Debug, PartialEq, Default)]
Expand All @@ -21,6 +21,24 @@ impl<const SIZE: usize> From<Int<SIZE>> for BigInt {
}
}

impl<const SIZE: usize> Display for Int<SIZE> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

impl<const SIZE: usize> LowerHex for Int<SIZE> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "0x{:x}", self.0)
}
}

impl<const SIZE: usize> UpperHex for Int<SIZE> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "0x{:X}", self.0)
}
}

impl<const SIZE: usize> ToCell for Int<SIZE> {
fn store<'a>(&self, builder: &'a mut CellBuilder) -> anyhow::Result<&'a mut CellBuilder> {
let (sign, value) = self.0.clone().into_parts();
Expand Down Expand Up @@ -103,4 +121,13 @@ mod tests {

assert_eq!(first_iter, second_iter);
}

#[test]
fn test_fmt() {
let value = Int::<32>::from(BigInt::from(0x1234abcdi32));
assert_eq!("Int(305441741)", format!("{:?}", value));
assert_eq!("305441741", format!("{}", value));
assert_eq!("0x1234abcd", format!("{:x}", value));
assert_eq!("0x1234ABCD", format!("{:X}", value));
}
}

0 comments on commit a3ab1ed

Please sign in to comment.