Skip to content

Commit 5de80fb

Browse files
committed
Merge branch 'master' into fix-inverted-si-conditions
2 parents 4d7c8d9 + f26df42 commit 5de80fb

File tree

10 files changed

+316
-96
lines changed

10 files changed

+316
-96
lines changed

.github/workflows/ci.yml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: CI
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened]
6+
merge_group:
7+
types: [checks_requested]
8+
push:
9+
branches: [master]
10+
11+
permissions:
12+
contents: read
13+
14+
concurrency:
15+
group: ${{ github.workflow }}-${{ github.ref }}
16+
cancel-in-progress: true
17+
18+
env:
19+
CARGO_TERM_COLOR: always
20+
21+
jobs:
22+
read_msrv:
23+
name: Read MSRV
24+
uses: actions-rust-lang/msrv/.github/workflows/[email protected]
25+
26+
rust:
27+
needs: read_msrv
28+
29+
strategy:
30+
fail-fast: false
31+
matrix:
32+
os:
33+
- { name: Linux, runner: ubuntu-latest }
34+
- { name: macOS, runner: macos-latest }
35+
- { name: Windows, runner: windows-latest }
36+
toolchain:
37+
- { name: stable, version: stable }
38+
- { name: msrv, version: "${{ needs.read_msrv.outputs.msrv }}" }
39+
40+
name: ${{ matrix.os.name }} / ${{ matrix.toolchain.name }}
41+
runs-on: ${{ matrix.os.runner }}
42+
43+
steps:
44+
- uses: actions/checkout@v4
45+
46+
- name: Install Rust (${{ matrix.toolchain.name }})
47+
uses: actions-rust-lang/[email protected]
48+
with:
49+
toolchain: ${{ matrix.toolchain.version }}
50+
51+
- name: Install just, nextest
52+
uses: taiki-e/[email protected]
53+
with:
54+
tool: just,nextest
55+
56+
- name: Test
57+
run: just test

.github/workflows/coverage.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Coverage
2+
3+
on:
4+
push:
5+
branches: [master]
6+
7+
permissions:
8+
contents: read
9+
10+
concurrency:
11+
group: ${{ github.workflow }}-${{ github.ref }}
12+
cancel-in-progress: true
13+
14+
jobs:
15+
coverage:
16+
runs-on: ubuntu-latest
17+
steps:
18+
- uses: actions/checkout@v4
19+
20+
- name: Install Rust
21+
uses: actions-rust-lang/[email protected]
22+
with:
23+
components: llvm-tools-preview
24+
25+
- name: Install just & cargo-llvm-cov
26+
uses: taiki-e/[email protected]
27+
with:
28+
tool: just,cargo-llvm-cov
29+
30+
- name: Generate code coverage
31+
run: just test-coverage-codecov
32+
33+
- name: Upload coverage to Codecov
34+
uses: codecov/[email protected]
35+
with:
36+
files: codecov.json
37+
fail_ci_if_error: true
38+
env:
39+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

.github/workflows/lint.yml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
name: Lint
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened]
6+
7+
permissions:
8+
contents: read
9+
10+
concurrency:
11+
group: ${{ github.workflow }}-${{ github.ref }}
12+
cancel-in-progress: true
13+
14+
jobs:
15+
clippy:
16+
runs-on: ubuntu-latest
17+
18+
permissions:
19+
contents: read
20+
checks: write
21+
22+
steps:
23+
- uses: actions/checkout@v4
24+
25+
- name: Install Rust
26+
uses: actions-rust-lang/[email protected]
27+
with:
28+
components: clippy
29+
30+
- name: Install just, cargo-hack
31+
uses: taiki-e/[email protected]
32+
with:
33+
tool: just,cargo-hack
34+
35+
- name: Check with Clippy
36+
run: just clippy
37+
38+
rustfmt:
39+
runs-on: ubuntu-latest
40+
steps:
41+
- uses: actions/checkout@v4
42+
43+
- name: Install Rust (nightly)
44+
uses: actions-rust-lang/[email protected]
45+
with:
46+
toolchain: nightly
47+
components: rustfmt
48+
49+
- name: Check with Rustfmt
50+
run: cargo fmt -- --check
51+
52+
docs:
53+
runs-on: ubuntu-latest
54+
steps:
55+
- uses: actions/checkout@v4
56+
57+
- name: Install Rust (nightly)
58+
uses: actions-rust-lang/[email protected]
59+
with:
60+
toolchain: nightly
61+
components: rust-docs
62+
63+
- name: Check for broken intra-doc links
64+
env:
65+
RUSTDOCFLAGS: -D warnings
66+
run: cargo doc --workspace --no-deps --all-features
67+
68+
sorted-deps:
69+
runs-on: ubuntu-latest
70+
steps:
71+
- uses: actions/checkout@v4
72+
73+
- name: Install Rust
74+
uses: actions-rust-lang/[email protected]
75+
with:
76+
components: clippy
77+
78+
- name: Install just, cargo-sort
79+
uses: taiki-e/[email protected]
80+
with:
81+
tool: just,cargo-sort
82+
83+
- name: Check dependency requirements are sorted lexicographically
84+
run: cargo sort --workspace --check

.github/workflows/rust.yml

Lines changed: 0 additions & 22 deletions
This file was deleted.

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Changelog
2+
3+
## Unreleased
4+
- Use SI format by default with `Display`.
5+
- Use "KiB" for SI unit.

Cargo.toml

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
[package]
22
name = "bytesize"
3-
description = "an utility for human-readable bytes representations"
4-
version = "1.4.0-dev"
5-
authors = ["Hyunsik Choi <[email protected]>"]
6-
7-
homepage = "https://github.com/hyunsik/bytesize/"
8-
documentation = "https://docs.rs/bytesize/"
9-
repository = "https://github.com/hyunsik/bytesize/"
10-
readme = "README.md"
3+
description = "A utility for human-readable byte count representations"
4+
version = "1.3.0"
5+
authors = ["Hyunsik Choi <[email protected]>", "MrCroxx <[email protected]>"]
116
keywords = ["byte", "byte-size", "utility", "human-readable", "format"]
7+
categories = ["development-tools", "filesystem"]
8+
repository = "https://github.com/hyunsik/bytesize"
129
license = "Apache-2.0"
10+
edition = "2021"
11+
rust-version = "1.65"
1312

1413
[dependencies]
15-
arbitrary = { version = "1.3.0", optional = true }
16-
serde = { version = "1.0.185", optional = true }
14+
arbitrary = { version = "1", features = ["derive"], optional = true }
15+
serde = { version = "1", optional = true }
1716

1817
[dev-dependencies]
19-
serde = { version = "1.0.185", features = ["derive"] }
20-
serde_json = "1.0.105"
21-
toml = "0.7.6"
18+
serde = { version = "1", features = ["derive"] }
19+
serde_json = "1"
20+
toml = "0.8"
2221

2322
[features]
2423
arbitrary = ["dep:arbitrary"]

README.md

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,25 @@
11
## ByteSize
2-
[![Build Status](https://travis-ci.org/hyunsik/bytesize.svg?branch=master)](https://travis-ci.org/hyunsik/bytesize)
3-
[![Crates.io Version](https://img.shields.io/crates/v/bytesize.svg)](https://crates.io/crates/bytesize)
42

3+
[![CI](https://github.com/hyunsik/bytesize/actions/workflows/ci.yml/badge.svg)](https://github.com/hyunsik/bytesize/actions/workflows/ci.yml)
4+
[![Crates.io Version](https://img.shields.io/crates/v/bytesize.svg)](https://crates.io/crates/bytesize)
55

6-
ByteSize is an utility for human-readable byte count representation.
6+
`ByteSize` is a utility for human-readable byte count representations.
77

88
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-
* Serde support for binary and human-readable deserializers like JSON
149

15-
[API Documentation](https://docs.rs/bytesize/)
10+
- Pre-defined constants for various size units (e.g., B, KB, KiB, MB, MiB, GB, GiB, ... PiB).
11+
- `ByteSize` type which presents size units convertible to different size units.
12+
- Arithmetic operations for `ByteSize`.
13+
- FromStr impl for `ByteSize`, allowing to parse from string size representations like 1.5KiB and 521TiB.
14+
- Serde support for binary and human-readable deserializers like JSON.
1615

17-
## Usage
16+
[API Documentation](https://docs.rs/bytesize)
1817

19-
Add this to your Cargo.toml:
18+
## Example
2019

21-
```toml
22-
[dependencies]
23-
bytesize = {version = "1.2.0", features = ["serde"]}
24-
```
20+
### Human readable representations (SI unit and Binary unit)
2521

26-
and this to your crate root:
2722
```rust
28-
extern crate bytesize;
29-
```
30-
31-
## Example
32-
### Human readable representations (SI units and Binary units)
33-
```rust
34-
#[allow(dead_code)]
3523
fn assert_display(expected: &str, b: ByteSize) {
3624
assert_eq!(expected, format!("{}", b));
3725
}
@@ -47,26 +35,42 @@ fn test_display() {
4735
assert_display("609.0 PiB", ByteSize::pib(609));
4836
}
4937

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+
5050
fn assert_to_string(expected: &str, b: ByteSize, si: bool) {
5151
assert_eq!(expected.to_string(), b.to_string_as(si));
5252
}
5353

5454
#[test]
5555
fn test_to_string_as() {
56+
assert_to_string("215 B", ByteSize::b(215), true);
5657
assert_to_string("215 B", ByteSize::b(215), false);
5758
assert_to_string("215 B", ByteSize::b(215), true);
5859

59-
assert_to_string("1.0 KiB", ByteSize::kib(1), false);
60-
assert_to_string("1.0 kB", ByteSize::kib(1), true);
60+
assert_to_string("1.0 KiB", ByteSize::kib(1), true);
61+
assert_to_string("1.0 KB", ByteSize::kib(1), false);
6162

62-
assert_to_string("293.9 KiB", ByteSize::kb(301), false);
63-
assert_to_string("301.0 kB", ByteSize::kb(301), true);
63+
assert_to_string("293.9 KiB", ByteSize::kb(301), true);
64+
assert_to_string("301.0 KB", ByteSize::kb(301), false);
6465

6566
assert_to_string("1.0 MiB", ByteSize::mib(1), false);
6667
assert_to_string("1048.6 kB", ByteSize::mib(1), true);
6768

68-
assert_to_string("1.9 GiB", ByteSize::mib(1907), false);
69-
assert_to_string("2.0 GB", ByteSize::mib(1908), true);
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);
7074

7175
assert_to_string("399.6 MiB", ByteSize::mb(419), false);
7276
assert_to_string("419.0 MB", ByteSize::mb(419), true);
@@ -88,30 +92,14 @@ fn test_parsing_from_str() {
8892
s.parse::<ByteSize>().unwrap().0
8993
}
9094

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);
95+
assert_to_string("540.9 PiB", ByteSize::pb(609), false);
96+
assert_to_string("609.0 PB", ByteSize::pb(609), true);
10897
}
10998
```
11099

111100
### Arithmetic operations
112-
```rust
113-
extern crate bytesize;
114101

102+
```rust
115103
use bytesize::ByteSize;
116104

117105
fn byte_arithmetic_operator() {

0 commit comments

Comments
 (0)