|
24 | 24 | //! ``` |
25 | 25 | //! use bytesize::ByteSize; |
26 | 26 | //! |
27 | | -//! assert_eq!("482.4 GiB", ByteSize::gb(518).to_string_as(false)); |
28 | | -//! assert_eq!("518.0 GB", ByteSize::gb(518).to_string_as(true)); |
| 27 | +//! assert_eq!("518.0 GiB", ByteSize::gib(518).display().iec().to_string()); |
| 28 | +//! assert_eq!("556.2 GB", ByteSize::gib(518).display().si().to_string()); |
| 29 | +//! assert_eq!("518.0G", ByteSize::gib(518).display().short().to_string()); |
29 | 30 | //! ``` |
30 | 31 | //! |
31 | 32 | //! Arithmetic operations are supported. |
|
40 | 41 | //! assert_eq!(ByteSize::gb(996), minus); |
41 | 42 | //! ``` |
42 | 43 |
|
| 44 | +use std::fmt::{self, Debug, Display, Formatter}; |
| 45 | +use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign}; |
| 46 | + |
43 | 47 | #[cfg(feature = "arbitrary")] |
44 | 48 | mod arbitrary; |
| 49 | +mod display; |
45 | 50 | mod parse; |
46 | 51 | #[cfg(feature = "serde")] |
47 | 52 | mod serde; |
48 | 53 |
|
49 | | -use std::fmt::{self, Debug, Display, Formatter}; |
50 | | -use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign}; |
51 | | - |
52 | 54 | /// byte size for 1 byte |
53 | 55 | pub const B: u64 = 1; |
54 | 56 | /// bytes size for 1 kilobyte |
| 57 | +/// Number of bytes in 1 kilobyte. |
55 | 58 | pub const KB: u64 = 1_000; |
56 | 59 | /// bytes size for 1 megabyte |
57 | 60 | pub const MB: u64 = 1_000_000; |
@@ -89,13 +92,6 @@ const LN_KIB: f64 = 6.931_471_805_599_453; |
89 | 92 | /// `ln(1000) ~= 6.908` |
90 | 93 | const LN_KB: f64 = 6.907_755_278_982_137; |
91 | 94 |
|
92 | | -#[derive(Debug, Clone, Default)] |
93 | | -pub enum Format { |
94 | | - #[default] |
95 | | - IEC, |
96 | | - SI, |
97 | | -} |
98 | | - |
99 | 95 | pub fn kb<V: Into<u64>>(size: V) -> u64 { |
100 | 96 | size.into() * KB |
101 | 97 | } |
@@ -136,8 +132,8 @@ pub fn pib<V: Into<u64>>(size: V) -> u64 { |
136 | 132 | size.into() * PIB |
137 | 133 | } |
138 | 134 |
|
139 | | -/// Byte size representation |
140 | | -#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Default)] |
| 135 | +/// Byte size representation. |
| 136 | +#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
141 | 137 | pub struct ByteSize(pub u64); |
142 | 138 |
|
143 | 139 | impl ByteSize { |
@@ -201,56 +197,17 @@ impl ByteSize { |
201 | 197 | self.0 |
202 | 198 | } |
203 | 199 |
|
204 | | - #[inline(always)] |
205 | | - pub fn to_string_as(&self, si_unit: bool) -> String { |
206 | | - to_string(self.0, si_unit) |
207 | | - } |
208 | | -} |
209 | | - |
210 | | -pub fn to_string(bytes: u64, si_unit: bool) -> String { |
211 | | - to_string_format(bytes, if si_unit { Format::SI } else { Format::IEC }) |
212 | | -} |
213 | | - |
214 | | -pub fn to_string_format(bytes: u64, format: Format) -> String { |
215 | | - let unit = match format { |
216 | | - Format::IEC => KIB, |
217 | | - Format::SI => KB, |
218 | | - }; |
219 | | - let unit_base = match format { |
220 | | - Format::IEC => LN_KIB, |
221 | | - Format::SI => LN_KB, |
222 | | - }; |
223 | | - |
224 | | - let unit_prefix = match format { |
225 | | - Format::IEC => UNITS_IEC.as_bytes(), |
226 | | - Format::SI => UNITS_SI.as_bytes(), |
227 | | - }; |
228 | | - let unit_suffix = match format { |
229 | | - Format::IEC => "iB", |
230 | | - Format::SI => "B", |
231 | | - }; |
232 | | - |
233 | | - if bytes < unit { |
234 | | - format!("{} B", bytes) |
235 | | - } else { |
236 | | - let size = bytes as f64; |
237 | | - let exp = match (size.ln() / unit_base) as usize { |
238 | | - 0 => 1, |
239 | | - e => e, |
240 | | - }; |
241 | | - |
242 | | - format!( |
243 | | - "{:.1} {}{}", |
244 | | - (size / unit.pow(exp as u32) as f64), |
245 | | - unit_prefix[exp - 1] as char, |
246 | | - unit_suffix |
247 | | - ) |
| 200 | + pub fn display(&self) -> crate::display::Display { |
| 201 | + crate::display::Display { |
| 202 | + byte_size: *self, |
| 203 | + format: crate::display::Format::Iec, |
| 204 | + } |
248 | 205 | } |
249 | 206 | } |
250 | 207 |
|
251 | 208 | impl Display for ByteSize { |
252 | 209 | fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
253 | | - f.pad(&to_string_format(self.0, Format::IEC)) |
| 210 | + f.pad(&self.display().to_string()) |
254 | 211 | } |
255 | 212 | } |
256 | 213 |
|
@@ -499,48 +456,8 @@ mod tests { |
499 | 456 | assert_eq!("|--357 B---|", format!("|{:-^10}|", ByteSize(357))); |
500 | 457 | } |
501 | 458 |
|
502 | | - #[track_caller] |
503 | | - fn assert_to_string(expected: &str, b: ByteSize, format: Format) { |
504 | | - assert_eq!(expected.to_string(), to_string_format(b.0, format)); |
505 | | - } |
506 | | - |
507 | | - #[test] |
508 | | - fn test_to_string_as() { |
509 | | - assert_to_string("215 B", ByteSize::b(215), Format::IEC); |
510 | | - assert_to_string("215 B", ByteSize::b(215), Format::SI); |
511 | | - |
512 | | - assert_to_string("1.0 KiB", ByteSize::kib(1), Format::IEC); |
513 | | - assert_to_string("1.0 kB", ByteSize::kib(1), Format::SI); |
514 | | - |
515 | | - assert_to_string("293.9 KiB", ByteSize::kb(301), Format::IEC); |
516 | | - assert_to_string("301.0 kB", ByteSize::kb(301), Format::SI); |
517 | | - |
518 | | - assert_to_string("1.0 MiB", ByteSize::mib(1), Format::IEC); |
519 | | - assert_to_string("1.0 MB", ByteSize::mib(1), Format::SI); |
520 | | - |
521 | | - assert_to_string("1.9 GiB", ByteSize::mib(1907), Format::IEC); |
522 | | - assert_to_string("2.0 GB", ByteSize::mib(1908), Format::SI); |
523 | | - |
524 | | - assert_to_string("399.6 MiB", ByteSize::mb(419), Format::IEC); |
525 | | - assert_to_string("419.0 MB", ByteSize::mb(419), Format::SI); |
526 | | - |
527 | | - assert_to_string("482.4 GiB", ByteSize::gb(518), Format::IEC); |
528 | | - assert_to_string("518.0 GB", ByteSize::gb(518), Format::SI); |
529 | | - |
530 | | - assert_to_string("741.2 TiB", ByteSize::tb(815), Format::IEC); |
531 | | - assert_to_string("815.0 TB", ByteSize::tb(815), Format::SI); |
532 | | - |
533 | | - assert_to_string("540.9 PiB", ByteSize::pb(609), Format::IEC); |
534 | | - assert_to_string("609.0 PB", ByteSize::pb(609), Format::SI); |
535 | | - } |
536 | | - |
537 | 459 | #[test] |
538 | 460 | fn test_default() { |
539 | 461 | assert_eq!(ByteSize::b(0), ByteSize::default()); |
540 | 462 | } |
541 | | - |
542 | | - #[test] |
543 | | - fn test_to_string() { |
544 | | - assert_to_string("609.0 PB", ByteSize::pb(609), Format::SI); |
545 | | - } |
546 | 463 | } |
0 commit comments