Skip to main content

wowlab_common/
fmt.rs

1use std::time::Duration;
2
3use humanly::{HumanNumber, HumanPercent, HumanSize, HumanTime};
4
5/// Format a byte count as a concise human-readable string (e.g. `"5 MiB"`).
6#[must_use]
7pub fn format_bytes(bytes: u64) -> String {
8    HumanSize::from(bytes).concise()
9}
10
11/// Format a `Duration` as a concise string (e.g. `"1h 2m 3s"`).
12#[must_use]
13pub fn format_duration(d: Duration) -> String {
14    HumanTime::from(d).concise()
15}
16
17/// Format seconds as a concise duration string.
18#[must_use]
19pub fn format_duration_secs(secs: u64) -> String {
20    format_duration(Duration::from_secs(secs))
21}
22
23/// Format a number in concise K/M/B/T notation (e.g. `"1.2k"`).
24pub fn format_number_concise(n: impl Into<f64>) -> String {
25    HumanNumber::from(n.into()).concise()
26}
27
28/// Format a float as a percentage with the given decimal places (e.g. `"12.3%"`).
29#[must_use]
30pub fn format_percent(value: f64, decimals: usize) -> String {
31    HumanPercent::from(value, decimals).concise()
32}
33
34#[cfg(test)]
35mod tests;