Skip to content

Commit 9ee304c

Browse files
authored
Merge pull request #1602 from CosmWasm/decimal-debug
Debug impl for Decimal/Decimal256 uses decimal output (Simon)
2 parents 1ddba30 + 28e6905 commit 9ee304c

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ and this project adheres to
1313

1414
[#1597]: https://github.com/CosmWasm/cosmwasm/issues/1597
1515

16+
### Changed
17+
18+
- cosmwasm-std: Improve readability of `Debug` output for `Decimal` and
19+
`Decimal256` ([#1600]).
20+
21+
[#1600]: https://github.com/CosmWasm/cosmwasm/pull/1600
22+
1623
## [1.2.0] - 2023-01-24
1724

1825
### Added

packages/std/src/math/decimal.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use super::{Uint128, Uint256};
1919
/// A fixed-point decimal value with 18 fractional digits, i.e. Decimal(1_000_000_000_000_000_000) == 1.0
2020
///
2121
/// The greatest possible value that can be represented is 340282366920938463463.374607431768211455 (which is (2^128 - 1) / 10^18)
22-
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, JsonSchema)]
22+
#[derive(Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, JsonSchema)]
2323
pub struct Decimal(#[schemars(with = "String")] Uint128);
2424

2525
#[derive(Error, Debug, PartialEq, Eq)]
@@ -457,6 +457,12 @@ impl fmt::Display for Decimal {
457457
}
458458
}
459459

460+
impl fmt::Debug for Decimal {
461+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
462+
write!(f, "Decimal({})", self)
463+
}
464+
}
465+
460466
impl Add for Decimal {
461467
type Output = Self;
462468

@@ -2015,4 +2021,17 @@ mod tests {
20152021
assert_eq!(&lhs == &rhs, expected);
20162022
}
20172023
}
2024+
2025+
#[test]
2026+
fn decimal_implements_debug() {
2027+
let decimal = Decimal::from_str("123.45").unwrap();
2028+
assert_eq!(format!("{:?}", decimal), "Decimal(123.45)");
2029+
2030+
let test_cases = ["5", "5.01", "42", "0", "2"];
2031+
for s in test_cases {
2032+
let decimal = Decimal::from_str(s).unwrap();
2033+
let expected = format!("Decimal({})", s);
2034+
assert_eq!(format!("{:?}", decimal), expected);
2035+
}
2036+
}
20182037
}

packages/std/src/math/decimal256.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use super::Uint256;
2222
/// The greatest possible value that can be represented is
2323
/// 115792089237316195423570985008687907853269984665640564039457.584007913129639935
2424
/// (which is (2^256 - 1) / 10^18)
25-
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, JsonSchema)]
25+
#[derive(Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, JsonSchema)]
2626
pub struct Decimal256(#[schemars(with = "String")] Uint256);
2727

2828
#[derive(Error, Debug, PartialEq, Eq)]
@@ -482,6 +482,12 @@ impl fmt::Display for Decimal256 {
482482
}
483483
}
484484

485+
impl fmt::Debug for Decimal256 {
486+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
487+
write!(f, "Decimal256({})", self)
488+
}
489+
}
490+
485491
impl Add for Decimal256 {
486492
type Output = Self;
487493

@@ -2162,4 +2168,17 @@ mod tests {
21622168
assert_eq!(&lhs == &rhs, expected);
21632169
}
21642170
}
2171+
2172+
#[test]
2173+
fn decimal256_implements_debug() {
2174+
let decimal = Decimal256::from_str("123.45").unwrap();
2175+
assert_eq!(format!("{:?}", decimal), "Decimal256(123.45)");
2176+
2177+
let test_cases = ["5", "5.01", "42", "0", "2"];
2178+
for s in test_cases {
2179+
let decimal256 = Decimal256::from_str(s).unwrap();
2180+
let expected = format!("Decimal256({})", s);
2181+
assert_eq!(format!("{:?}", decimal256), expected);
2182+
}
2183+
}
21652184
}

0 commit comments

Comments
 (0)