Skip to main content

dysk_cli/
units.rs

1use core::str::FromStr;
2
3/// The Units system used for sizes
4#[derive(Debug, Clone, Copy, Default, PartialEq)]
5pub enum Units {
6    #[default]
7    Si, // Units according to the SI system, based on multiples of 1000
8    Binary, // Old binary based units, based on multiples of 1024
9    Bytes,  // Just the raw byte counts, with commas separating thousands
10}
11
12impl FromStr for Units {
13    type Err = String;
14    fn from_str(value: &str) -> Result<Self, String> {
15        match value.to_lowercase().as_ref() {
16            "si" => Ok(Self::Si),
17            "binary" => Ok(Self::Binary),
18            "bytes" => Ok(Self::Bytes),
19            _ => Err(format!(
20                "Illegal value: {:?} - valid values are 'SI', 'binary', and 'bytes'",
21                value
22            )),
23        }
24    }
25}
26
27static PREFIXES: &[char] = &['K', 'M', 'G', 'T', 'P'];
28
29impl Units {
30    pub fn fmt(
31        self,
32        size: u64,
33    ) -> String {
34        match self {
35            Self::Si => file_size::fit_4(size),
36            Self::Binary => {
37                if size < 10_000 {
38                    size.to_string()
39                } else {
40                    let i = size.ilog2() / 10u32;
41                    let idx = i as usize - 1;
42                    let size = size as f64;
43                    if idx >= PREFIXES.len() {
44                        "huge".to_string()
45                    } else {
46                        let v = size / (1024u64.pow(i) as f64);
47                        if v >= 10f64 {
48                            format!("{:.0}{}i", v.round(), PREFIXES[idx])
49                        } else {
50                            format!("{:.1}{}i", v, PREFIXES[idx])
51                        }
52                    }
53                }
54            }
55            Self::Bytes => {
56                let mut rev: Vec<char> = Vec::new();
57                for (i, c) in size.to_string().chars().rev().enumerate() {
58                    if i > 0 && i % 3 == 0 {
59                        rev.push(',');
60                    }
61                    rev.push(c);
62                }
63                rev.drain(..).rev().collect()
64            }
65        }
66    }
67}
68
69#[test]
70fn test_fmt_binary() {
71    fn check(
72        v: u64,
73        s: &str,
74    ) {
75        assert_eq!(&Units::Binary.fmt(v), s);
76    }
77    check(0, "0");
78    check(1, "1");
79    check(456, "456");
80    check(1456, "1456");
81    check(9_999, "9999");
82    check(10_000, "9.8Ki");
83    check(12_345, "12Ki");
84    check(123_456, "121Ki");
85    check(1_000_000_000, "954Mi");
86    check(1_073_741_824, "1.0Gi");
87    check(1_234_567_890, "1.1Gi");
88}
89
90#[test]
91fn test_fmt_bytes() {
92    fn check(
93        v: u64,
94        s: &str,
95    ) {
96        assert_eq!(&Units::Bytes.fmt(v), s);
97    }
98    check(0, "0");
99    check(1, "1");
100    check(456, "456");
101    check(1456, "1,456");
102    check(9_999, "9,999");
103    check(10_000, "10,000");
104    check(12_345, "12,345");
105    check(123_456, "123,456");
106    check(1_234_567, "1,234,567");
107    check(1_000_000_000, "1,000,000,000");
108    check(1_234_567_890, "1,234,567,890");
109}