-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a little newtype, TariFormat that formats the balance values in a human-friendly way. De- and Serialization is auto-unwrapped back to a u64 (muT) Balances now looks like: Available 974,845.997 T Incoming 0 μT Outgoing 0 μT Timelocked 970,590.372 T rather than: Available 974845997000000 Incoming 0 Outgoing 0 Timelocked 970590372000
- Loading branch information
Showing
6 changed files
with
99 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use std::{fmt, fmt::Display}; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)] | ||
#[serde(transparent)] | ||
pub struct TariFormat { | ||
value: u64, | ||
} | ||
|
||
impl TariFormat { | ||
pub fn as_u64(&self) -> u64 { | ||
self.value | ||
} | ||
} | ||
|
||
impl From<u64> for TariFormat { | ||
fn from(value: u64) -> Self { | ||
Self { value } | ||
} | ||
} | ||
|
||
impl Display for TariFormat { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
let mut value = self.value; | ||
let unit = if value < 1_000_000 { "μT" } else { "T" }; | ||
let mut decimals = None; | ||
if value >= 1_000_000 { | ||
decimals = Some((value % 1_000_000).to_string()); | ||
value /= 1_000_000; | ||
} | ||
let val_str = value | ||
.to_string() | ||
.as_bytes() | ||
.rchunks(3) | ||
.rev() | ||
.map(std::str::from_utf8) | ||
.collect::<Result<Vec<&str>, _>>() | ||
.unwrap() | ||
.join(","); | ||
|
||
let dec_str = match decimals { | ||
Some(dec) => format!(".{:0<03.3}", dec), | ||
None => String::new(), | ||
}; | ||
write!(f, "{val_str}{dec_str} {unit}") | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn format_whole_number() { | ||
let value = 1_234_567_891_000_000; | ||
let tari = TariFormat::from(value); | ||
assert_eq!(tari.to_string(), "1,234,567,891.000 T"); | ||
} | ||
|
||
#[test] | ||
fn format_small_number() { | ||
let value = 123_456; | ||
let tari = TariFormat::from(value); | ||
assert_eq!(tari.to_string(), "123,456 μT"); | ||
} | ||
#[test] | ||
fn format_big_number_w_frac() { | ||
let value = 1_234_567_890_222_333; | ||
let tari = TariFormat::from(value); | ||
assert_eq!(tari.to_string(), "1,234,567,890.222 T"); | ||
} | ||
|
||
#[test] | ||
fn format_zero() { | ||
let value = 0; | ||
let tari = TariFormat::from(value); | ||
assert_eq!(tari.to_string(), "0 μT"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters