Skip to content

Commit cf581a0

Browse files
committed
chore: merge master readme
1 parent 5de80fb commit cf581a0

File tree

1 file changed

+30
-92
lines changed

1 file changed

+30
-92
lines changed

README.md

Lines changed: 30 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,53 @@
11
## ByteSize
22

3-
[![CI](https://github.com/hyunsik/bytesize/actions/workflows/ci.yml/badge.svg)](https://github.com/hyunsik/bytesize/actions/workflows/ci.yml)
3+
<!-- prettier-ignore-start -->
4+
5+
[![CI](https://github.com/bytesize-rs/bytesize/actions/workflows/ci.yml/badge.svg)](https://github.com/bytesize-rs/bytesize/actions/workflows/ci.yml)
46
[![Crates.io Version](https://img.shields.io/crates/v/bytesize.svg)](https://crates.io/crates/bytesize)
57

6-
`ByteSize` is a utility for human-readable byte count representations.
8+
<!-- prettier-ignore-end -->
9+
10+
<!-- cargo-rdme start -->
11+
12+
`ByteSize` is a semantic wrapper for byte count representations.
713

814
Features:
915

10-
- Pre-defined constants for various size units (e.g., B, KB, KiB, MB, MiB, GB, GiB, ... PiB).
16+
- Pre-defined constants for various size units (e.g., B, Kb, Kib, Mb, Mib, Gb, Gib, ... PB).
1117
- `ByteSize` type which presents size units convertible to different size units.
1218
- Arithmetic operations for `ByteSize`.
13-
- FromStr impl for `ByteSize`, allowing to parse from string size representations like 1.5KiB and 521TiB.
19+
- `FromStr` impl for `ByteSize`, allowing for parsing string size representations like "1.5KiB" and "521TiB".
1420
- Serde support for binary and human-readable deserializers like JSON.
1521

16-
[API Documentation](https://docs.rs/bytesize)
22+
### Examples
1723

18-
## Example
19-
20-
### Human readable representations (SI unit and Binary unit)
24+
Construction using SI or IEC helpers.
2125

2226
```rust
23-
fn assert_display(expected: &str, b: ByteSize) {
24-
assert_eq!(expected, format!("{}", b));
25-
}
26-
27-
#[test]
28-
fn test_display() {
29-
assert_display("215 B", ByteSize::b(215));
30-
assert_display("1.0 KiB", ByteSize::kib(1));
31-
assert_display("301.0 KiB", ByteSize::kib(301));
32-
assert_display("419.0 MiB", ByteSize::mib(419));
33-
assert_display("518.0 GiB", ByteSize::gib(518));
34-
assert_display("815.0 TiB", ByteSize::tib(815));
35-
assert_display("609.0 PiB", ByteSize::pib(609));
36-
}
37-
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-
50-
fn assert_to_string(expected: &str, b: ByteSize, si: bool) {
51-
assert_eq!(expected.to_string(), b.to_string_as(si));
52-
}
53-
54-
#[test]
55-
fn test_to_string_as() {
56-
assert_to_string("215 B", ByteSize::b(215), true);
57-
assert_to_string("215 B", ByteSize::b(215), false);
58-
assert_to_string("215 B", ByteSize::b(215), true);
59-
60-
assert_to_string("1.0 KiB", ByteSize::kib(1), true);
61-
assert_to_string("1.0 KB", ByteSize::kib(1), false);
62-
63-
assert_to_string("293.9 KiB", ByteSize::kb(301), true);
64-
assert_to_string("301.0 KB", ByteSize::kb(301), false);
65-
66-
assert_to_string("1.0 MiB", ByteSize::mib(1), false);
67-
assert_to_string("1048.6 kB", ByteSize::mib(1), true);
68-
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);
74-
75-
assert_to_string("399.6 MiB", ByteSize::mb(419), false);
76-
assert_to_string("419.0 MB", ByteSize::mb(419), true);
77-
78-
assert_to_string("482.4 GiB", ByteSize::gb(518), false);
79-
assert_to_string("518.0 GB", ByteSize::gb(518), true);
80-
81-
assert_to_string("741.2 TiB", ByteSize::tb(815), false);
82-
assert_to_string("815.0 TB", ByteSize::tb(815), true);
83-
84-
assert_to_string("540.9 PiB", ByteSize::pb(609), false);
85-
assert_to_string("609.0 PB", ByteSize::pb(609), true);
86-
}
87-
88-
#[test]
89-
fn test_parsing_from_str() {
90-
// shortcut for writing test cases
91-
fn parse(s: &str) -> u64 {
92-
s.parse::<ByteSize>().unwrap().0
93-
}
94-
95-
assert_to_string("540.9 PiB", ByteSize::pb(609), false);
96-
assert_to_string("609.0 PB", ByteSize::pb(609), true);
97-
}
27+
use bytesize::ByteSize;
28+
29+
assert!(ByteSize::kib(4) > ByteSize::kb(4));
9830
```
9931

100-
### Arithmetic operations
32+
Display as human-readable string.
10133

10234
```rust
10335
use bytesize::ByteSize;
10436

105-
fn byte_arithmetic_operator() {
106-
let x = ByteSize::mb(1);
107-
let y = ByteSize::kb(100);
37+
assert_eq!("482.4 GiB", ByteSize::gb(518).to_string_as(true));
38+
assert_eq!("518.0 GB", ByteSize::gb(518).to_string_as(false));
39+
```
40+
41+
Arithmetic operations are supported.
42+
43+
```rust
44+
use bytesize::ByteSize;
10845

109-
let plus = x + y;
110-
println!("{}", plus);
46+
let plus = ByteSize::mb(1) + ByteSize::kb(100);
47+
println!("{plus}");
11148

112-
let minus = ByteSize::tb(100) + ByteSize::gb(4);
113-
println!("{}", minus);
114-
}
49+
let minus = ByteSize::tb(1) - ByteSize::gb(4);
50+
assert_eq!(ByteSize::gb(996), minus);
11551
```
52+
53+
<!-- cargo-rdme end -->

0 commit comments

Comments
 (0)