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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
mod defaults;
pub use self::defaults::*;
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum Kilo {
Decimal,
Binary,
}
impl Default for Kilo {
fn default() -> Self {
Self::Decimal
}
}
impl Kilo {
pub(crate) fn value(&self) -> f64 {
match self {
Kilo::Decimal => 1000.0,
Kilo::Binary => 1024.0,
}
}
}
#[derive(Debug, Copy, Clone)]
pub enum FixedAt {
Base,
Kilo,
Mega,
Giga,
Tera,
Peta,
Exa,
Zetta,
Yotta,
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum BaseUnit {
Bit,
Byte,
}
impl Default for BaseUnit {
fn default() -> Self {
Self::Byte
}
}
#[derive(Debug, Clone, Copy, Default)]
#[non_exhaustive]
pub struct FormatSizeOptionsBuilder {
pub base_unit: BaseUnit,
pub kilo: Kilo,
pub units: Kilo,
pub decimal_places: usize,
pub decimal_zeroes: usize,
pub fixed_at: Option<FixedAt>,
pub long_units: bool,
pub space_after_value: bool,
pub suffix: &'static str,
}
#[derive(Debug, Clone, Copy, Default)]
#[non_exhaustive]
pub struct FormatSizeOptions {
pub base_unit: BaseUnit,
pub kilo: Kilo,
pub units: Kilo,
pub decimal_places: usize,
pub decimal_zeroes: usize,
pub fixed_at: Option<FixedAt>,
pub long_units: bool,
pub space_after_value: bool,
pub suffix: &'static str,
}
impl FormatSizeOptions {
pub fn from(from: FormatSizeOptions) -> FormatSizeOptions {
FormatSizeOptions { ..from }
}
pub fn base_unit(mut self, base_unit: BaseUnit) -> FormatSizeOptions {
self.base_unit = base_unit;
self
}
pub fn kilo(mut self, kilo: Kilo) -> FormatSizeOptions {
self.kilo = kilo;
self
}
pub fn units(mut self, units: Kilo) -> FormatSizeOptions {
self.units = units;
self
}
pub fn decimal_places(mut self, decimal_places: usize) -> FormatSizeOptions {
self.decimal_places = decimal_places;
self
}
pub fn decimal_zeroes(mut self, decimal_zeroes: usize) -> FormatSizeOptions {
self.decimal_zeroes = decimal_zeroes;
self
}
pub fn fixed_at(mut self, fixed_at: Option<FixedAt>) -> FormatSizeOptions {
self.fixed_at = fixed_at;
self
}
pub fn long_units(mut self, long_units: bool) -> FormatSizeOptions {
self.long_units = long_units;
self
}
pub fn space_after_value(mut self, insert_space: bool) -> FormatSizeOptions {
self.space_after_value = insert_space;
self
}
pub fn suffix(mut self, suffix: &'static str) -> FormatSizeOptions {
self.suffix = suffix;
self
}
}
impl AsRef<FormatSizeOptions> for FormatSizeOptions {
fn as_ref(&self) -> &FormatSizeOptions {
self
}
}