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
use std::fmt::{Debug, Display};
#[derive(Copy, Debug, Clone, PartialEq)]
pub struct ConfigMemory(pub i64);
impl ConfigMemory {}
static KIB: i64 = 1024;
static MIB: i64 = 1024 * KIB;
static GIB: i64 = 1024 * MIB;
static TIB: i64 = 1024 * GIB;
impl Display for ConfigMemory {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let v = self.0;
if v >= TIB && v % TIB == 0 {
write!(f, "{}TiB", v / TIB)
}
else if v >= GIB && v % GIB == 0 {
write!(f, "{}GiB", v / GIB)
}
else if v >= MIB && v % MIB == 0 {
write!(f, "{}MiB", v / MIB)
}
else if v >= KIB && v % KIB == 0 {
write!(f, "{}KiB", v / KIB)
}
else {
write!(f, "{}B", v)
}
}
}