5
5
6
6
ByteSize is an utility for human-readable byte count representation.
7
7
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
+
8
14
[ API Documentation] ( https://docs.rs/bytesize/ )
9
15
10
16
## Usage
@@ -13,7 +19,7 @@ Add this to your Cargo.toml:
13
19
14
20
``` toml
15
21
[dependencies ]
16
- bytesize = " 1.0.1"
22
+ bytesize = { version = " 1.0.1" , features = [ " serde " ]}
17
23
```
18
24
19
25
and this to your crate root:
@@ -49,7 +55,7 @@ fn assert_display(expected: &str, b: ByteSize) {
49
55
fn test_to_string () {
50
56
assert_to_string (" 215 B" , ByteSize (215 ), true );
51
57
assert_to_string (" 215 B" , ByteSize (215 ), false );
52
-
58
+
53
59
assert_to_string (" 215 B" , ByteSize :: b (215 ), true );
54
60
assert_to_string (" 215 B" , ByteSize :: b (215 ), false );
55
61
@@ -73,7 +79,33 @@ fn assert_display(expected: &str, b: ByteSize) {
73
79
74
80
assert_to_string (" 540.9 PiB" , ByteSize :: pb (609 ), true );
75
81
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
+ }
77
109
```
78
110
79
111
### Arithmetic operations
0 commit comments