unc_gas/trait_impls/
display.rs

1use crate::{UncGas, UncGasError, ONE_GIGA_GAS};
2
3/// UncGas Display implementation rounds up the gas usage to the relevant precision point.
4/// There are 4 breakpoints:
5/// 1. exactly 0 Tgas
6/// 2. <0.001 Tgas
7/// 3. 0.001 - 0.999 Tgas (uses 3 digits after the floating point)
8/// 4. >1 Tgas (uses 1 digit after the floating point)
9impl std::fmt::Display for UncGas {
10    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
11        if *self == UncGas::from_gas(0) {
12            write!(f, "0 Tgas")
13        } else if *self < UncGas::from_ggas(1) {
14            write!(f, "<0.001 Tgas")
15        } else if *self <= UncGas::from_ggas(999) {
16            let gigagas_rounded_up = self.as_gas().saturating_add(ONE_GIGA_GAS - 1) / ONE_GIGA_GAS;
17            write!(f, "0.{:03} Tgas", gigagas_rounded_up)
18        } else {
19            let terragas_rounded_up =
20                self.as_gas().saturating_add(100 * ONE_GIGA_GAS - 1) / ONE_GIGA_GAS / 100;
21            write!(
22                f,
23                "{}.{} Tgas",
24                terragas_rounded_up / 10,
25                terragas_rounded_up % 10
26            )
27        }
28    }
29}
30
31impl std::fmt::Display for UncGasError {
32    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33        match self {
34            UncGasError::IncorrectNumber(err) => write!(f, "Incorrect number: {:?}", err),
35            UncGasError::IncorrectUnit(err) => write!(f, "Incorrect unit: {}", err),
36        }
37    }
38}
39
40#[cfg(test)]
41mod test {
42    use crate::UncGas;
43
44    #[test]
45    fn test_display() {
46        for (unc_gas, expected_display) in [
47            (UncGas::from_gas(0), "0 Tgas"),
48            (UncGas::from_gas(1), "<0.001 Tgas"),
49            (UncGas::from_gas(999_999_999), "<0.001 Tgas"),
50            (UncGas::from_gas(1_000_000_000), "0.001 Tgas"),
51            (UncGas::from_gas(1_000_000_001), "0.002 Tgas"),
52            (UncGas::from_gas(2_000_000_000), "0.002 Tgas"),
53            (UncGas::from_gas(200_000_000_000), "0.200 Tgas"),
54            (UncGas::from_gas(999_000_000_000), "0.999 Tgas"),
55            (UncGas::from_gas(999_000_000_001), "1.0 Tgas"),
56            (UncGas::from_gas(999_999_999_999), "1.0 Tgas"),
57            (UncGas::from_gas(1_000_000_000_000), "1.0 Tgas"),
58            (UncGas::from_gas(1_000_000_000_001), "1.1 Tgas"),
59            (UncGas::from_gas(1_234_567_000_000), "1.3 Tgas"),
60            (UncGas::from_gas(1_500_000_000_000), "1.5 Tgas"),
61            (UncGas::from_gas(10_000_000_000_000), "10.0 Tgas"),
62            (UncGas::from_gas(10_500_000_000_000), "10.5 Tgas"),
63            (UncGas::from_gas(99_999_999_999_999), "100.0 Tgas"),
64            (UncGas::from_gas(100_000_000_000_000), "100.0 Tgas"),
65            (UncGas::from_gas(100_500_000_000_000), "100.5 Tgas"),
66            (UncGas::from_gas(1_000_500_000_000_000), "1000.5 Tgas"),
67            (
68                UncGas::from_gas(1_000_000_500_000_000_000),
69                "1000000.5 Tgas",
70            ),
71        ] {
72            assert_eq!(
73                unc_gas.to_string(),
74                expected_display,
75                "gas: {}",
76                unc_gas.as_gas()
77            );
78        }
79    }
80}