Skip to content

Commit df03091

Browse files
authored
Merge branch 'master' into rm-arb-derive
2 parents b7e3ea6 + 81d19ce commit df03091

File tree

10 files changed

+197
-137
lines changed

10 files changed

+197
-137
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

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
## Unreleased
44

5-
- Use SI format by default with `Display`.
6-
- Use "KiB" for SI unit.
5+
- Use IEC (binary) format by default with `Display`.
6+
- Use "kB" for SI unit.
77
- Implement `Sub<ByteSize>` for `ByteSize`.
88
- Implement `Sub<impl Into<u64>>` for `ByteSize`.
99
- Implement `SubAssign<ByteSize>` for `ByteSize`.
1010
- Implement `SubAssign<impl Into<u64>>` for `ByteSize`.
11+
- Reject parsing non-unit characters after whitespace.

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
name = "bytesize"
33
description = "A utility for human-readable byte count representations"
44
version = "1.3.0"
5-
authors = ["Hyunsik Choi <[email protected]>", "MrCroxx <[email protected]>"]
5+
authors = [
6+
"Hyunsik Choi <[email protected]>",
7+
"MrCroxx <[email protected]>",
8+
"Rob Ede <[email protected]>",
9+
]
610
keywords = ["byte", "byte-size", "utility", "human-readable", "format"]
711
categories = ["development-tools", "filesystem"]
812
repository = "https://github.com/bytesize-rs/bytesize"

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 -->

codecov.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
comment: false
2+
3+
coverage:
4+
status:
5+
project:
6+
default:
7+
threshold: 100% # make CI green
8+
patch:
9+
default:
10+
threshold: 100% # make CI green
11+
12+
# ignore code coverage on following paths
13+
ignore:
14+
- "**/tests"
15+
- "**/benches"
16+
- "**/examples"

examples/ls.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//! Emulates running `ls -la` in the terminal, showing human-readable file sizes and file names.
2+
3+
use std::{fs, io};
4+
5+
fn main() -> io::Result<()> {
6+
let dir = fs::read_dir(".")?;
7+
8+
for entry in dir {
9+
let entry = entry?;
10+
11+
let md = entry.metadata()?;
12+
13+
let file_name = entry.file_name();
14+
let file_name = file_name.to_string_lossy();
15+
16+
if md.is_file() {
17+
let file_size = md.len();
18+
let file_size = bytesize::ByteSize::b(file_size);
19+
20+
println!("{file_size}\t{file_name}");
21+
} else {
22+
println!("-\t{file_name}");
23+
}
24+
}
25+
26+
Ok(())
27+
}

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:

0 commit comments

Comments
 (0)