-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsize.rs
126 lines (110 loc) · 2.99 KB
/
size.rs
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//! Defines types that represent the size of a directory item.
#[cfg(test)]
#[path = "./size_test.rs"]
mod size_test;
use std::fmt::Display;
#[cfg(feature = "cli")]
use clap::ValueEnum;
/// The format to use when a size value is displayed.
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "cli", derive(ValueEnum))]
pub enum SizeDisplayFormat {
/// 1KB = 1000 bytes.
Metric,
/// 1KiB = 1024 bytes.
Binary,
}
#[derive(Debug, Eq, PartialEq)]
struct SizeDisplayData {
divisor: u64,
unit: &'static str,
}
const BINARY_DISPLAY_DATA: [&SizeDisplayData; 3] = [
&SizeDisplayData {
divisor: 1024 * 1024 * 1024,
unit: "GiB",
},
&SizeDisplayData {
divisor: 1024 * 1024,
unit: "MiB",
},
&SizeDisplayData {
divisor: 1024,
unit: "KiB",
},
];
const METRIC_DISPLAY_DATA: [&SizeDisplayData; 3] = [
&SizeDisplayData {
divisor: 1000 * 1000 * 1000,
unit: "GB",
},
&SizeDisplayData {
divisor: 1000 * 1000,
unit: "MB",
},
&SizeDisplayData {
divisor: 1000,
unit: "KB",
},
];
/// A directory item size.
#[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd)]
pub struct Size {
value: u64,
}
impl Size {
/// Creates a new instance.
#[inline(always)]
pub fn new(value: u64) -> Self {
Size { value }
}
/// Subtracts the specified number of bytes from this size.
pub fn subtract(&mut self, value: u64) {
if value <= self.value {
self.value -= value;
} else {
self.value = 0;
}
}
/// Gets the current size in bytes.
#[inline(always)]
pub fn get_value(&self) -> u64 {
self.value
}
/// Given the total size in bytes, returns the fraction of that total that this size represents.
pub fn get_fraction(&self, total_size_in_bytes: u64) -> f32 {
if total_size_in_bytes == 0 {
0f32
} else {
self.value as f32 / total_size_in_bytes as f32
}
}
/// Converts the size to string, using the specified format.
pub fn to_string(&self, format: SizeDisplayFormat) -> String {
let best_format = Self::get_best_format(self.value, format);
format!("{} {}", self.value / best_format.divisor, best_format.unit)
}
fn get_best_format(size_in_bytes: u64, format: SizeDisplayFormat) -> &'static SizeDisplayData {
let config = match format {
SizeDisplayFormat::Binary => BINARY_DISPLAY_DATA,
SizeDisplayFormat::Metric => METRIC_DISPLAY_DATA,
};
for data in config {
if size_in_bytes > data.divisor {
return data;
}
}
config[config.len() - 1]
}
}
impl Display for Size {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} bytes", self.value)
}
}
impl From<Size> for u64 {
#[inline]
fn from(value: Size) -> Self {
value.get_value()
}
}