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
// 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_name {
    () => {
        "0.5.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 name.
pub const CRATE_VERSION_NAME: &'static str = crate_version_name!();

/// Crate release date (year/month/day).
pub const CRATE_RELEASE_DATE: (u16, u8, u8) = (2018, 5, 26);

/// 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_name!());

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

use std::convert::Into;

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

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

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

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

/// 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"];

/// ## Converts bytes to 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 = bytes.into();

    if bytes < KIB as u128 {
        return format!("{} bytes", bytes);
    }

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