Skip to content

Commit 9f938b5

Browse files
authored
feat: merge foyer-rs/bytesize back to the OG repo (#50)
1 parent 6630ad4 commit 9f938b5

File tree

6 files changed

+161
-119
lines changed

6 files changed

+161
-119
lines changed

.github/workflows/rust.yml

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,63 @@ on:
88

99
env:
1010
CARGO_TERM_COLOR: always
11+
CACHE_KEY_SUFFIX: 20240821
1112

1213
jobs:
13-
build:
14-
15-
runs-on: ubuntu-latest
16-
14+
rust:
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
os: [ubuntu-latest, macos-latest, windows-latest]
19+
rust_toolchain: [stable, 1.81.0]
20+
runs-on: ${{ matrix.os }}
1721
steps:
18-
- uses: actions/checkout@v2
19-
- name: Build
20-
run: cargo build --verbose
21-
- name: Run tests
22-
run: cargo test --verbose
22+
- uses: actions/checkout@v4
23+
- name: Install rust toolchain
24+
uses: dtolnay/rust-toolchain@master
25+
with:
26+
toolchain: ${{ matrix.rust_toolchain }}
27+
components: rustfmt, clippy, llvm-tools-preview
28+
- name: Cache Cargo home
29+
uses: actions/cache@v4
30+
id: cache
31+
with:
32+
path: |
33+
~/.cargo/bin/
34+
~/.cargo/registry/index/
35+
~/.cargo/registry/cache/
36+
~/.cargo/git/db/
37+
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.toml') }}-${{ env.CACHE_KEY_SUFFIX }}-rust-test
38+
- name: Install cargo tools
39+
if: steps.cache.outputs.cache-hit != 'true'
40+
run: |
41+
cargo install cargo-sort --locked
42+
- name: Run rust cargo-sort check
43+
# https://github.com/DevinR528/cargo-sort/issues/56
44+
if: matrix.os != 'windows-latest'
45+
run: |
46+
cargo sort -w -c
47+
- name: Run rust format check
48+
run: |
49+
cargo fmt --all -- --check
50+
- name: Run rust clippy check
51+
run: |
52+
cargo clippy --all-features -- -D warnings
53+
- if: steps.cache.outputs.cache-hit != 'true'
54+
uses: taiki-e/install-action@cargo-llvm-cov
55+
- name: Run rust test with coverage
56+
env:
57+
RUST_BACKTRACE: 1
58+
run: |
59+
cargo llvm-cov test --all-features
60+
- name: Run rust doc test
61+
env:
62+
RUST_BACKTRACE: 1
63+
run: |
64+
cargo test --doc --all-features
65+
- uses: codecov/codecov-action@v4
66+
if: runner.os == 'Linux' && matrix.rust_toolchain == 'stable'
67+
env:
68+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
69+
with:
70+
verbose: true

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: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
[package]
22
name = "bytesize"
3-
description = "an utility for human-readable bytes representations"
3+
description = "A utility for human-readable bytes representations. Forked from https://github.com/hyunsik/bytesize ."
44
version = "1.4.0-dev"
5-
authors = ["Hyunsik Choi <[email protected]>"]
5+
authors = ["Hyunsik Choi <[email protected]>", "MrCroxx <[email protected]>"]
6+
edition = "2021"
67

78
homepage = "https://github.com/hyunsik/bytesize/"
89
documentation = "https://docs.rs/bytesize/"
@@ -12,13 +13,13 @@ keywords = ["byte", "byte-size", "utility", "human-readable", "format"]
1213
license = "Apache-2.0"
1314

1415
[dependencies]
15-
arbitrary = { version = "1.3.0", optional = true }
16-
serde = { version = "1.0.185", optional = true }
16+
arbitrary = { version = "1", features = ["derive"], optional = true }
17+
serde = { version = "1", optional = true }
1718

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

2324
[features]
2425
arbitrary = ["dep:arbitrary"]

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
SHELL := /bin/bash
2+
.PHONY: all
3+
4+
all:
5+
cargo sort -w
6+
cargo fmt --all
7+
cargo clippy --all-features
8+
RUST_BACKTRACE=1 cargo test --all-features

README.md

Lines changed: 68 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
## ByteSize
2-
[![Build Status](https://travis-ci.org/hyunsik/bytesize.svg?branch=master)](https://travis-ci.org/hyunsik/bytesize)
2+
3+
[![CI](https://github.com/hyunsik/bytesize/actions/workflows/rust.yml/badge.svg)](https://github.com/hyunsik/bytesize/actions/workflows/rust.yml)
34
[![Crates.io Version](https://img.shields.io/crates/v/bytesize.svg)](https://crates.io/crates/bytesize)
45

6+
Forked from https://github.com/hyunsik/bytesize .
57

68
ByteSize is an utility for human-readable byte count representation.
79

810
Features:
9-
* Pre-defined constants for various size units (e.g., B, Kb, kib, Mb, Mib, Gb, Gib, ... PB)
11+
* Pre-defined constants for various size units (e.g., B, Kb, Kib, Mb, Mib, Gb, Gib, ... PB)
1012
* `ByteSize` type which presents size units convertible to different size units.
1113
* Artimetic operations for `ByteSize`
1214
* FromStr impl for `ByteSize`, allowing to parse from string size representations like 1.5KiB and 521TiB.
@@ -20,99 +22,79 @@ Add this to your Cargo.toml:
2022

2123
```toml
2224
[dependencies]
23-
bytesize = {version = "1.2.0", features = ["serde"]}
24-
```
25-
26-
and this to your crate root:
27-
```rust
28-
extern crate bytesize;
25+
bytesize = { version = "2", features = ["serde"]}
2926
```
3027

3128
## Example
29+
3230
### Human readable representations (SI unit and Binary unit)
33-
```rust
34-
#[allow(dead_code)]
35-
fn assert_display(expected: &str, b: ByteSize) {
36-
assert_eq!(expected, format!("{}", b));
37-
}
3831

39-
#[test]
40-
fn test_display() {
41-
assert_display("215 B", ByteSize(215));
42-
assert_display("215 B", ByteSize::b(215));
43-
assert_display("1.0 KB", ByteSize::kb(1));
44-
assert_display("301.0 KB", ByteSize::kb(301));
45-
assert_display("419.0 MB", ByteSize::mb(419));
46-
assert_display("518.0 GB", ByteSize::gb(518));
47-
assert_display("815.0 TB", ByteSize::tb(815));
48-
assert_display("609.0 PB", ByteSize::pb(609));
49-
}
50-
51-
fn assert_to_string(expected: &str, b: ByteSize, si: bool) {
52-
assert_eq!(expected.to_string(), b.to_string_as(si));
53-
}
54-
55-
#[test]
56-
fn test_to_string() {
57-
assert_to_string("215 B", ByteSize(215), true);
58-
assert_to_string("215 B", ByteSize(215), false);
59-
60-
assert_to_string("215 B", ByteSize::b(215), true);
61-
assert_to_string("215 B", ByteSize::b(215), false);
62-
63-
assert_to_string("1.0 kiB", ByteSize::kib(1), true);
64-
assert_to_string("1.0 KB", ByteSize::kib(1), false);
65-
66-
assert_to_string("293.9 kiB", ByteSize::kb(301), true);
67-
assert_to_string("301.0 KB", ByteSize::kb(301), false);
68-
69-
assert_to_string("1.0 MiB", ByteSize::mib(1), true);
70-
assert_to_string("1048.6 KB", ByteSize::mib(1), 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-
}
84-
85-
#[test]
86-
fn test_parsing_from_str() {
87-
// shortcut for writing test cases
88-
fn parse(s: &str) -> u64 {
89-
s.parse::<ByteSize>().unwrap().0
90-
}
91-
92-
assert_eq!("0".parse::<ByteSize>().unwrap().0, 0);
93-
assert_eq!(parse("0"), 0);
94-
assert_eq!(parse("500"), 500);
95-
assert_eq!(parse("1K"), Unit::KiloByte * 1);
96-
assert_eq!(parse("1Ki"), Unit::KibiByte * 1);
97-
assert_eq!(parse("1.5Ki"), (1.5 * Unit::KibiByte) as u64);
98-
assert_eq!(parse("1KiB"), 1 * Unit::KibiByte);
99-
assert_eq!(parse("1.5KiB"), (1.5 * Unit::KibiByte) as u64);
100-
assert_eq!(parse("3 MB"), Unit::MegaByte * 3);
101-
assert_eq!(parse("4 MiB"), Unit::MebiByte * 4);
102-
assert_eq!(parse("6 GB"), 6 * Unit::GigaByte);
103-
assert_eq!(parse("4 GiB"), 4 * Unit::GibiByte);
104-
assert_eq!(parse("88TB"), 88 * Unit::TeraByte);
105-
assert_eq!(parse("521TiB"), 521 * Unit::TebiByte);
106-
assert_eq!(parse("8 PB"), 8 * Unit::PetaByte);
107-
assert_eq!(parse("8P"), 8 * Unit::PetaByte);
108-
assert_eq!(parse("12 PiB"), 12 * Unit::PebiByte);
109-
}
32+
```rust
33+
fn assert_display(expected: &str, b: ByteSize) {
34+
assert_eq!(expected, format!("{}", b));
35+
}
36+
37+
#[test]
38+
fn test_display() {
39+
assert_display("215 B", ByteSize::b(215));
40+
assert_display("1.0 KiB", ByteSize::kib(1));
41+
assert_display("301.0 KiB", ByteSize::kib(301));
42+
assert_display("419.0 MiB", ByteSize::mib(419));
43+
assert_display("518.0 GiB", ByteSize::gib(518));
44+
assert_display("815.0 TiB", ByteSize::tib(815));
45+
assert_display("609.0 PiB", ByteSize::pib(609));
46+
}
47+
48+
#[test]
49+
fn test_display_alignment() {
50+
assert_eq!("|357 B |", format!("|{:10}|", ByteSize(357)));
51+
assert_eq!("| 357 B|", format!("|{:>10}|", ByteSize(357)));
52+
assert_eq!("|357 B |", format!("|{:<10}|", ByteSize(357)));
53+
assert_eq!("| 357 B |", format!("|{:^10}|", ByteSize(357)));
54+
55+
assert_eq!("|-----357 B|", format!("|{:->10}|", ByteSize(357)));
56+
assert_eq!("|357 B-----|", format!("|{:-<10}|", ByteSize(357)));
57+
assert_eq!("|--357 B---|", format!("|{:-^10}|", ByteSize(357)));
58+
}
59+
60+
fn assert_to_string(expected: &str, b: ByteSize, si: bool) {
61+
assert_eq!(expected.to_string(), b.to_string_as(si));
62+
}
63+
64+
#[test]
65+
fn test_to_string_as() {
66+
assert_to_string("215 B", ByteSize::b(215), true);
67+
assert_to_string("215 B", ByteSize::b(215), false);
68+
69+
assert_to_string("1.0 KiB", ByteSize::kib(1), true);
70+
assert_to_string("1.0 KB", ByteSize::kib(1), false);
71+
72+
assert_to_string("293.9 KiB", ByteSize::kb(301), true);
73+
assert_to_string("301.0 KB", ByteSize::kb(301), false);
74+
75+
assert_to_string("1.0 MiB", ByteSize::mib(1), true);
76+
assert_to_string("1048.6 KB", ByteSize::mib(1), false);
77+
78+
// a bug case: https://github.com/flang-project/bytesize/issues/8
79+
assert_to_string("1.9 GiB", ByteSize::mib(1907), true);
80+
assert_to_string("2.0 GB", ByteSize::mib(1908), false);
81+
82+
assert_to_string("399.6 MiB", ByteSize::mb(419), true);
83+
assert_to_string("419.0 MB", ByteSize::mb(419), false);
84+
85+
assert_to_string("482.4 GiB", ByteSize::gb(518), true);
86+
assert_to_string("518.0 GB", ByteSize::gb(518), false);
87+
88+
assert_to_string("741.2 TiB", ByteSize::tb(815), true);
89+
assert_to_string("815.0 TB", ByteSize::tb(815), false);
90+
91+
assert_to_string("540.9 PiB", ByteSize::pb(609), true);
92+
assert_to_string("609.0 PB", ByteSize::pb(609), false);
93+
}
11094
```
11195

11296
### Arithmetic operations
11397
```rust
114-
extern crate bytesize;
115-
11698
use bytesize::ByteSize;
11799

118100
fn byte_arithmetic_operator() {

0 commit comments

Comments
 (0)