1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//! # size-display
//!
//! Display human readable file sizes.
//!
//! ## Limitation
//!
//! Displayed units go up to Exabyte (2^60).
//!
//! ## Example
//!
//! ```
//! use size_display::Size;
//!
//! assert_eq!("24", format!("{}", Size(24)));
//! assert_eq!("4.2G", format!("{:.1}", Size(4509715660)));
//! ```

use std::fmt;
use std::fmt::Debug;

pub const K: u64 = 1024;
pub const M: u64 = K * K;
pub const G: u64 = K * K * K;
pub const T: u64 = K * K * K * K;
pub const P: u64 = K * K * K * K * K;
pub const E: u64 = K * K * K * K * K * K;

/// File size type with human-readable `Debug`.
pub struct Size(pub u64);

impl fmt::Debug for Size {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        <Self as fmt::Display>::fmt(self, f)
    }
}

impl fmt::Display for Size {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let bytes = self.0;

        let (unit, char) = match bytes {
            e if e >= E => (E, "E"),
            p if p >= P => (P, "P"),
            t if t >= T => (T, "T"),
            g if g >= G => (G, "G"),
            m if m >= M => (M, "M"),
            k if k >= K => (K, "K"),
            b => (1, ""),
        };

        format_size(f, self.0, unit)?;
        write!(f, "{}", char)?;
        Ok(())
    }
}

fn format_size(f: &mut fmt::Formatter, size: u64, unit: u64) -> fmt::Result {
    match size % unit {
        0 => <u64 as fmt::Display>::fmt(&(size / unit), f),
        _ => <f64 as fmt::Display>::fmt(&(size as f64 / unit as f64), f),
    }
}