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
64
65
66
67
68
69
70
71
use super::{scale_base, ParsedValue};
use std::fmt::Debug;

/// Format a quantity of bytes.
#[derive(Debug, Clone, Copy)]
pub struct Formatter {
    scale_base: u64,
}

impl Formatter {
    /// Create a new formatter.
    pub const fn new(scale_base: u64) -> Self {
        Formatter { scale_base }
    }

    /// Multiplication factor.
    pub const fn scale_base(self) -> u64 {
        self.scale_base
    }

    /// Get scale in number.
    pub const fn scale(self, exp: u32) -> u64 {
        self.scale_base().pow(exp)
    }

    /// Parse a value according to the prefixing rule.
    pub const fn parse_value(self, value: u64) -> ParsedValue {
        macro_rules! check {
            ($exp:literal => $unit:literal) => {{
                let scale = self.scale($exp);
                if value >= scale {
                    return ParsedValue {
                        coefficient: rounded_div::u64(value, scale),
                        unit: $unit,
                        exponent: $exp,
                        scale,
                    };
                }
            }};
        }

        check!(5 => 'P');
        check!(4 => 'T');
        check!(3 => 'G');
        check!(2 => 'M');
        check!(1 => 'K');
        ParsedValue {
            coefficient: value,
            unit: 'B',
            scale: 1,
            exponent: 0,
        }
    }
}

macro_rules! variant {
    ($(#[$attributes:meta])* $name:ident) => {
        $(#[$attributes])*
        pub const $name: Formatter = Formatter::new(scale_base::$name);
    };
}

variant! {
    /// Format a quantity of bytes in [metric system](scale_base::METRIC).
    METRIC
}

variant! {
    /// Format a quantity of bytes in [binary system](scale_base::BINARY).
    BINARY
}