11## ByteSize
2- [ ![ Build Status] ( https://travis-ci.org/hyunsik/bytesize.svg?branch=master )] ( https://travis-ci.org/hyunsik/bytesize )
3- [ ![ Crates.io Version] ( https://img.shields.io/crates/v/bytesize.svg )] ( https://crates.io/crates/bytesize )
42
3+ [ ![ CI] ( https://github.com/hyunsik/bytesize/actions/workflows/ci.yml/badge.svg )] ( https://github.com/hyunsik/bytesize/actions/workflows/ci.yml )
4+ [ ![ Crates.io Version] ( https://img.shields.io/crates/v/bytesize.svg )] ( https://crates.io/crates/bytesize )
55
6- ByteSize is an utility for human-readable byte count representation .
6+ ` ByteSize ` is a utility for human-readable byte count representations .
77
88Features:
9- * Pre-defined constants for various size units (e.g., B, Kb, kib, Mb, Mib, Gb, Gib, ... PB)
10- * ` ByteSize ` type which presents size units convertible to different size units.
11- * Artimetic operations for ` ByteSize `
12- * FromStr impl for ` ByteSize ` , allowing to parse from string size representations like 1.5KiB and 521TiB.
13- * Serde support for binary and human-readable deserializers like JSON
149
15- [ API Documentation] ( https://docs.rs/bytesize/ )
10+ - Pre-defined constants for various size units (e.g., B, KB, KiB, MB, MiB, GB, GiB, ... PiB).
11+ - ` ByteSize ` type which presents size units convertible to different size units.
12+ - Arithmetic operations for ` ByteSize ` .
13+ - FromStr impl for ` ByteSize ` , allowing to parse from string size representations like 1.5KiB and 521TiB.
14+ - Serde support for binary and human-readable deserializers like JSON.
1615
17- ## Usage
16+ [ API Documentation ] ( https://docs.rs/bytesize )
1817
19- Add this to your Cargo.toml:
18+ ## Example
2019
21- ``` toml
22- [dependencies ]
23- bytesize = {version = " 1.2.0" , features = [" serde" ]}
24- ```
20+ ### Human readable representations (SI unit and Binary unit)
2521
26- and this to your crate root:
2722``` rust
28- extern crate bytesize;
29- ```
30-
31- ## Example
32- ### Human readable representations (SI units and Binary units)
33- ``` rust
34- #[allow(dead_code)]
3523fn assert_display (expected : & str , b : ByteSize ) {
3624 assert_eq! (expected , format! (" {}" , b ));
3725}
@@ -47,26 +35,42 @@ fn test_display() {
4735 assert_display (" 609.0 PiB" , ByteSize :: pib (609 ));
4836}
4937
38+ #[test]
39+ fn test_display_alignment () {
40+ assert_eq! (" |357 B |" , format! (" |{:10}|" , ByteSize (357 )));
41+ assert_eq! (" | 357 B|" , format! (" |{:>10}|" , ByteSize (357 )));
42+ assert_eq! (" |357 B |" , format! (" |{:<10}|" , ByteSize (357 )));
43+ assert_eq! (" | 357 B |" , format! (" |{:^10}|" , ByteSize (357 )));
44+
45+ assert_eq! (" |-----357 B|" , format! (" |{:->10}|" , ByteSize (357 )));
46+ assert_eq! (" |357 B-----|" , format! (" |{:-<10}|" , ByteSize (357 )));
47+ assert_eq! (" |--357 B---|" , format! (" |{:-^10}|" , ByteSize (357 )));
48+ }
49+
5050fn assert_to_string (expected : & str , b : ByteSize , si : bool ) {
5151 assert_eq! (expected . to_string (), b . to_string_as (si ));
5252}
5353
5454#[test]
5555fn test_to_string_as () {
56+ assert_to_string (" 215 B" , ByteSize :: b (215 ), true );
5657 assert_to_string (" 215 B" , ByteSize :: b (215 ), false );
5758 assert_to_string (" 215 B" , ByteSize :: b (215 ), true );
5859
59- assert_to_string (" 1.0 KiB" , ByteSize :: kib (1 ), false );
60- assert_to_string (" 1.0 kB " , ByteSize :: kib (1 ), true );
60+ assert_to_string (" 1.0 KiB" , ByteSize :: kib (1 ), true );
61+ assert_to_string (" 1.0 KB " , ByteSize :: kib (1 ), false );
6162
62- assert_to_string (" 293.9 KiB" , ByteSize :: kb (301 ), false );
63- assert_to_string (" 301.0 kB " , ByteSize :: kb (301 ), true );
63+ assert_to_string (" 293.9 KiB" , ByteSize :: kb (301 ), true );
64+ assert_to_string (" 301.0 KB " , ByteSize :: kb (301 ), false );
6465
6566 assert_to_string (" 1.0 MiB" , ByteSize :: mib (1 ), false );
6667 assert_to_string (" 1048.6 kB" , ByteSize :: mib (1 ), true );
6768
68- assert_to_string (" 1.9 GiB" , ByteSize :: mib (1907 ), false );
69- assert_to_string (" 2.0 GB" , ByteSize :: mib (1908 ), true );
69+ assert_to_string (" 1.9 GiB" , ByteSize :: mib (1907 ), true );
70+ assert_to_string (" 2.0 GB" , ByteSize :: mib (1908 ), false );
71+
72+ assert_to_string (" 399.6 MiB" , ByteSize :: mb (419 ), true );
73+ assert_to_string (" 419.0 MB" , ByteSize :: mb (419 ), false );
7074
7175 assert_to_string (" 399.6 MiB" , ByteSize :: mb (419 ), false );
7276 assert_to_string (" 419.0 MB" , ByteSize :: mb (419 ), true );
@@ -88,30 +92,14 @@ fn test_parsing_from_str() {
8892 s . parse :: <ByteSize >(). unwrap (). 0
8993 }
9094
91- assert_eq! (" 0" . parse :: <ByteSize >(). unwrap (). 0 , 0 );
92- assert_eq! (parse (" 0" ), 0 );
93- assert_eq! (parse (" 500" ), 500 );
94- assert_eq! (parse (" 1K" ), Unit :: KiloByte * 1 );
95- assert_eq! (parse (" 1Ki" ), Unit :: KibiByte * 1 );
96- assert_eq! (parse (" 1.5Ki" ), (1.5 * Unit :: KibiByte ) as u64 );
97- assert_eq! (parse (" 1KiB" ), 1 * Unit :: KibiByte );
98- assert_eq! (parse (" 1.5KiB" ), (1.5 * Unit :: KibiByte ) as u64 );
99- assert_eq! (parse (" 3 MB" ), Unit :: MegaByte * 3 );
100- assert_eq! (parse (" 4 MiB" ), Unit :: MebiByte * 4 );
101- assert_eq! (parse (" 6 GB" ), 6 * Unit :: GigaByte );
102- assert_eq! (parse (" 4 GiB" ), 4 * Unit :: GibiByte );
103- assert_eq! (parse (" 88TB" ), 88 * Unit :: TeraByte );
104- assert_eq! (parse (" 521TiB" ), 521 * Unit :: TebiByte );
105- assert_eq! (parse (" 8 PB" ), 8 * Unit :: PetaByte );
106- assert_eq! (parse (" 8P" ), 8 * Unit :: PetaByte );
107- assert_eq! (parse (" 12 PiB" ), 12 * Unit :: PebiByte );
95+ assert_to_string (" 540.9 PiB" , ByteSize :: pb (609 ), false );
96+ assert_to_string (" 609.0 PB" , ByteSize :: pb (609 ), true );
10897}
10998```
11099
111100### Arithmetic operations
112- ``` rust
113- extern crate bytesize;
114101
102+ ``` rust
115103use bytesize :: ByteSize ;
116104
117105fn byte_arithmetic_operator () {
0 commit comments