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
use crate::regex::Regex;
use crate::ByteError;
use std::fmt::{self, Display, Formatter};
lazy_static! {
static ref BYTE_UNIT_RE: Regex = {
Regex::new(r"^(?i)((([ptgmk])(i)?)?b?)$").unwrap()
};
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ByteUnit {
B,
KB,
KiB,
MB,
MiB,
GB,
GiB,
TB,
TiB,
PB,
PiB,
}
impl Display for ByteUnit {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
match self {
ByteUnit::B => f.write_str("B"),
ByteUnit::KB => f.write_str("KB"),
ByteUnit::KiB => f.write_str("KiB"),
ByteUnit::MB => f.write_str("MB"),
ByteUnit::MiB => f.write_str("MiB"),
ByteUnit::GB => f.write_str("GB"),
ByteUnit::GiB => f.write_str("GiB"),
ByteUnit::TB => f.write_str("TB"),
ByteUnit::TiB => f.write_str("TiB"),
ByteUnit::PB => f.write_str("PB"),
ByteUnit::PiB => f.write_str("PiB"),
}
}
}
impl ByteUnit {
pub fn from_str<S: AsRef<str>>(unit: S) -> Result<ByteUnit, ByteError> {
let captures = BYTE_UNIT_RE.captures(unit.as_ref()).ok_or(ByteError::UnitIncorrect)?;
match captures.get(1) {
Some(_) => {
match captures.get(3) {
Some(m) => {
let u: String = m.as_str().to_lowercase();
match captures.get(4) {
Some(_) => {
match u.as_str() {
"k" => Ok(ByteUnit::KiB),
"m" => Ok(ByteUnit::MiB),
"g" => Ok(ByteUnit::GiB),
"t" => Ok(ByteUnit::TiB),
"p" => Ok(ByteUnit::PiB),
_ => unreachable!()
}
}
None => {
match u.as_str() {
"k" => Ok(ByteUnit::KB),
"m" => Ok(ByteUnit::MB),
"g" => Ok(ByteUnit::GB),
"t" => Ok(ByteUnit::TB),
"p" => Ok(ByteUnit::PB),
_ => unreachable!()
}
}
}
}
None => {
Ok(ByteUnit::B)
}
}
}
None => Ok(ByteUnit::B)
}
}
}