Skip to content

Commit 2bdd45f

Browse files
authored
docs: use cargo-rdme to match readme to tested docs (#68)
1 parent 65d8c89 commit 2bdd45f

File tree

4 files changed

+67
-91
lines changed

4 files changed

+67
-91
lines changed

.prettierrc.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
overrides:
2+
- files: '*.md'
3+
options:
4+
proseWrap: never
5+
printWidth: 9999

README.md

Lines changed: 29 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +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

1016
- 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-
## Examples
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-
59-
assert_to_string("1.0 KiB", ByteSize::kib(1), true);
60-
assert_to_string("1.0 KB", ByteSize::kib(1), false);
61-
62-
assert_to_string("293.9 KiB", ByteSize::kb(301), true);
63-
assert_to_string("301.0 KB", ByteSize::kb(301), false);
64-
65-
assert_to_string("1.0 MiB", ByteSize::mib(1), true);
66-
assert_to_string("1048.6 KB", ByteSize::mib(1), false);
67-
68-
// a bug case: https://github.com/flang-project/bytesize/issues/8
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("482.4 GiB", ByteSize::gb(518), true);
76-
assert_to_string("518.0 GB", ByteSize::gb(518), false);
77-
78-
assert_to_string("741.2 TiB", ByteSize::tb(815), true);
79-
assert_to_string("815.0 TB", ByteSize::tb(815), false);
80-
81-
assert_to_string("540.9 PiB", ByteSize::pb(609), true);
82-
assert_to_string("609.0 PB", ByteSize::pb(609), false);
83-
}
27+
use bytesize::ByteSize;
28+
29+
assert!(ByteSize::kib(4) > ByteSize::kb(4));
8430
```
8531

86-
### Arithmetic Operations
32+
Display as human-readable string.
8733

8834
```rust
8935
use bytesize::ByteSize;
9036

91-
fn byte_arithmetic_operator() {
92-
let x = ByteSize::mb(1);
93-
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;
9445

95-
let plus = x + y;
96-
print!("{}", plus);
46+
let plus = ByteSize::mb(1) + ByteSize::kb(100);
47+
println!("{plus}");
9748

98-
let minus = ByteSize::tb(100) + ByteSize::gb(4);
99-
print!("{}", minus);
100-
}
49+
let minus = ByteSize::tb(1) - ByteSize::gb(4);
50+
assert_eq!(ByteSize::gb(996), minus);
10151
```
52+
53+
<!-- cargo-rdme end -->

justfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ fmt:
2525
# fd --hidden --type=file -e=md -e=yml --exec-batch prettier --write
2626
cargo +nightly fmt
2727

28+
# Update READMEs from crate root documentation.
29+
[group("lint")]
30+
update-readme:
31+
cargo rdme --force
32+
npx -y prettier --write README.md
33+
2834
# Lint workspace with Clippy.
2935
[group("lint")]
3036
clippy:

src/lib.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,44 @@
1-
//! ByteSize is an utility that easily makes bytes size representation
2-
//! and helps its arithmetic operations.
1+
//! `ByteSize` is a semantic wrapper for byte count representations.
32
//!
4-
//! ## Example
3+
//! Features:
54
//!
6-
//! ```ignore
7-
//! use bytesize::ByteSize;
5+
//! - Pre-defined constants for various size units (e.g., B, Kb, Kib, Mb, Mib, Gb, Gib, ... PB).
6+
//! - `ByteSize` type which presents size units convertible to different size units.
7+
//! - Arithmetic operations for `ByteSize`.
8+
//! - `FromStr` impl for `ByteSize`, allowing for parsing string size representations like "1.5KiB"
9+
//! and "521TiB".
10+
//! - Serde support for binary and human-readable deserializers like JSON.
11+
//!
12+
//! # Examples
813
//!
9-
//! fn byte_arithmetic_operator() {
10-
//! let x = ByteSize::mb(1);
11-
//! let y = ByteSize::kb(100);
14+
//! Construction using SI or IEC helpers.
1215
//!
13-
//! let plus = x + y;
14-
//! print!("{} bytes", plus.as_u64());
16+
//! ```
17+
//! use bytesize::ByteSize;
1518
//!
16-
//! let minus = ByteSize::tb(100) - ByteSize::gb(4);
17-
//! print!("{} bytes", minus.as_u64());
18-
//! }
19+
//! assert!(ByteSize::kib(4) > ByteSize::kb(4));
1920
//! ```
2021
//!
21-
//! It also provides its human readable string as follows:
22+
//! Display as human-readable string.
2223
//!
2324
//! ```
2425
//! use bytesize::ByteSize;
2526
//!
2627
//! assert_eq!("482.4 GiB", ByteSize::gb(518).to_string_as(true));
2728
//! assert_eq!("518.0 GB", ByteSize::gb(518).to_string_as(false));
2829
//! ```
30+
//!
31+
//! Arithmetic operations are supported.
32+
//!
33+
//! ```
34+
//! use bytesize::ByteSize;
35+
//!
36+
//! let plus = ByteSize::mb(1) + ByteSize::kb(100);
37+
//! println!("{plus}");
38+
//!
39+
//! let minus = ByteSize::tb(1) - ByteSize::gb(4);
40+
//! assert_eq!(ByteSize::gb(996), minus);
41+
//! ```
2942
3043
mod parse;
3144
#[cfg(feature = "serde")]

0 commit comments

Comments
 (0)