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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// License: see LICENSE file at root directory of `master` branch

//! # Project
//!
//! - Repository: <https://bitbucket.org/haibison/kib>
//! - License: [Free Public License 1.0.0](https://opensource.org/licenses/FPL-1.0.0)
//! - _This project follows [Semantic Versioning 2.0.0]_
//!
//! [Semantic Versioning 2.0.0]: https://semver.org/spec/v2.0.0.html

// ╔═════════════════╗
// ║   IDENTIFIERS   ║
// ╚═════════════════╝

macro_rules! crate_code_name    { () => { "kib" }}
macro_rules! crate_version      { () => { "1.1.0" }}

/// # Crate name
pub const CRATE_NAME: &'static str = "KiB";

/// # Crate code name
pub const CRATE_CODE_NAME: &'static str = crate_code_name!();

/// # Crate version
pub const CRATE_VERSION: &'static str = crate_version!();

/// # Crate release date (year/month/day)
pub const CRATE_RELEASE_DATE: (u16, u8, u8) = (2019, 1, 8);

/// Unique universally identifier of this crate. Its CRC-32 is `1b50d15b`.
pub const UUID: &'static str = "b8d5bfc5-806f-41d3-b2d6-478381b63188";

/// Tag, which can be used for logging...
pub const TAG: &'static str = concat!(crate_code_name!(), "::1b50d15b::", crate_version!());

// ╔════════════════════╗
// ║   IMPLEMENTATION   ║
// ╚════════════════════╝

/// One KiB in bytes.
pub const KIB: u16 = 1024;

/// One MiB in bytes.
pub const MIB: u32 = 1024 * KIB as u32;

/// One GiB in bytes.
pub const GIB: u32 = 1024 * MIB;

/// One TiB in bytes.
pub const TIB: u64 = 1024 * GIB as u64;

/// One PiB in bytes.
pub const PIB: u64 = 1024 * TIB;

/// One EiB in bytes.
pub const EIB: u64 = 1024 * PIB;

/// One ZiB in bytes.
pub const ZIB: u128 = 1024 * EIB as u128;

/// One YiB in bytes.
pub const YIB: u128 = 1024 * ZIB;

/// Units.
const UNITS: [&'static str; 8] = ["KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];

/// # Formats bytes into string.
///
/// ## Parameters
///
/// - `bytes`: size in bytes.
///
/// ## Returns
///
/// Human-readable string, e.g: "1.9 KiB", "9.9 TiB"...
pub fn format(bytes: impl Into<u128>) -> String {
    let (bytes, unit) = format2(bytes.into());
    format!("{}{}{}", bytes, ' ', unit)
}

/// # Formats bytes into 2 strings
///
/// - The first one is size.
/// - The second one is unit.
pub fn format2(bytes: impl Into<u128>) -> (String, String) {
    let bytes = bytes.into();

    if bytes < KIB as u128 {
        return (
            bytes.to_string(),
            format!("byte{}", match bytes { 1 => "", _ => "s" }),
        );
    }

    let nearest_power = ((bytes as f64).log2() / 10.0).floor() as usize;
    // Bytes might exceed max unit that we have. So ignore the overflowed value.
    let unit_index = nearest_power.min(UNITS.len()).saturating_sub(1);
    (
        format!("{:.2}", bytes as f64 / (KIB as f64).powf(unit_index as f64 + 1.0)),
        UNITS[unit_index].into()
    )
}