near_gas/trait_impls/
display.rs

1use crate::{NearGas, NearGasError, ONE_GIGA_GAS};
2
3/// NearGas 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 NearGas {
10    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
11        if *self == NearGas::from_gas(0) {
12            write!(f, "0 Tgas")
13        } else if *self < NearGas::from_ggas(1) {
14            write!(f, "<0.001 Tgas")
15        } else if *self <= NearGas::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 NearGasError {
32    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33        match self {
34            NearGasError::IncorrectNumber(err) => write!(f, "Incorrect number: {:?}", err),
35            NearGasError::IncorrectUnit(err) => write!(f, "Incorrect unit: {}", err),
36        }
37    }
38}
39
40#[cfg(test)]
41mod test {
42    use crate::NearGas;
43
44    #[test]
45    fn test_display() {
46        for (near_gas, expected_display) in [
47            (NearGas::from_gas(0), "0 Tgas"),
48            (NearGas::from_gas(1), "<0.001 Tgas"),
49            (NearGas::from_gas(999_999_999), "<0.001 Tgas"),
50            (NearGas::from_gas(1_000_000_000), "0.001 Tgas"),
51            (NearGas::from_gas(1_000_000_001), "0.002 Tgas"),
52            (NearGas::from_gas(2_000_000_000), "0.002 Tgas"),
53            (NearGas::from_gas(200_000_000_000), "0.200 Tgas"),
54            (NearGas::from_gas(999_000_000_000), "0.999 Tgas"),
55            (NearGas::from_gas(999_000_000_001), "1.0 Tgas"),
56            (NearGas::from_gas(999_999_999_999), "1.0 Tgas"),
57            (NearGas::from_gas(1_000_000_000_000), "1.0 Tgas"),
58            (NearGas::from_gas(1_000_000_000_001), "1.1 Tgas"),
59            (NearGas::from_gas(1_234_567_000_000), "1.3 Tgas"),
60            (NearGas::from_gas(1_500_000_000_000), "1.5 Tgas"),
61            (NearGas::from_gas(10_000_000_000_000), "10.0 Tgas"),
62            (NearGas::from_gas(10_500_000_000_000), "10.5 Tgas"),
63            (NearGas::from_gas(99_999_999_999_999), "100.0 Tgas"),
64            (NearGas::from_gas(100_000_000_000_000), "100.0 Tgas"),
65            (NearGas::from_gas(100_500_000_000_000), "100.5 Tgas"),
66            (NearGas::from_gas(1_000_500_000_000_000), "1000.5 Tgas"),
67            (
68                NearGas::from_gas(1_000_000_500_000_000_000),
69                "1000000.5 Tgas",
70            ),
71        ] {
72            assert_eq!(
73                near_gas.to_string(),
74                expected_display,
75                "gas: {}",
76                near_gas.as_gas()
77            );
78        }
79    }
80}