55
66ByteSize is an utility for human-readable byte count representation.
77
8+ Features:
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+
814[ API Documentation] ( https://docs.rs/bytesize/ )
915
1016## Usage
@@ -13,7 +19,7 @@ Add this to your Cargo.toml:
1319
1420``` toml
1521[dependencies ]
16- bytesize = " 1.0.1"
22+ bytesize = { version = " 1.0.1" , features = [ " serde " ]}
1723```
1824
1925and this to your crate root:
@@ -49,7 +55,7 @@ fn assert_display(expected: &str, b: ByteSize) {
4955 fn test_to_string () {
5056 assert_to_string (" 215 B" , ByteSize (215 ), true );
5157 assert_to_string (" 215 B" , ByteSize (215 ), false );
52-
58+
5359 assert_to_string (" 215 B" , ByteSize :: b (215 ), true );
5460 assert_to_string (" 215 B" , ByteSize :: b (215 ), false );
5561
@@ -73,7 +79,33 @@ fn assert_display(expected: &str, b: ByteSize) {
7379
7480 assert_to_string (" 540.9 PiB" , ByteSize :: pb (609 ), true );
7581 assert_to_string (" 609.0 PB" , ByteSize :: pb (609 ), false );
76- }
82+ }
83+
84+ #[test]
85+ fn test_parsing_from_str () {
86+ // shortcut for writing test cases
87+ fn parse (s : & str ) -> u64 {
88+ s . parse :: <ByteSize >(). unwrap (). 0
89+ }
90+
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 );
108+ }
77109```
78110
79111### Arithmetic operations
0 commit comments