Skip to content

Fixed inverted SI conditions (and related things) #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- name: Upload coverage to Codecov
uses: codecov/[email protected]
with:
files: codecov.json
fail_ci_if_error: true
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
files: codecov.json
slug: bytesize-rs/bytesize
token: ${{ secrets.CODECOV_TOKEN }}
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
Cargo.lock
target
.idea
.vscode
7 changes: 0 additions & 7 deletions .travis.yml

This file was deleted.

6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

- Use SI format by default with `Display`.
- Use "KiB" for SI unit.
- Implement `Sub<ByteSize>` for `ByteSize`.
- Implement `Sub<impl Into<u64>>` for `ByteSize`.
- Implement `SubAssign<ByteSize>` for `ByteSize`.
- Implement `SubAssign<impl Into<u64>>` for `ByteSize`.
- Reject parsing non-unit characters after whitespace.
192 changes: 192 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ version = "1.3.0"
authors = ["Hyunsik Choi <[email protected]>", "MrCroxx <[email protected]>"]
keywords = ["byte", "byte-size", "utility", "human-readable", "format"]
categories = ["development-tools", "filesystem"]
repository = "https://github.com/hyunsik/bytesize"
repository = "https://github.com/bytesize-rs/bytesize"
license = "Apache-2.0"
edition = "2021"
rust-version = "1.65"

[features]
default = []
arbitrary = ["dep:arbitrary"]
serde = ["dep:serde"]

[dependencies]
arbitrary = { version = "1", features = ["derive"], optional = true }
serde = { version = "1", optional = true }
Expand All @@ -18,8 +23,3 @@ serde = { version = "1", optional = true }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
toml = "0.8"

[features]
arbitrary = ["dep:arbitrary"]
default = []
serde = ["dep:serde"]
106 changes: 29 additions & 77 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,101 +1,53 @@
## ByteSize

[![CI](https://github.com/hyunsik/bytesize/actions/workflows/ci.yml/badge.svg)](https://github.com/hyunsik/bytesize/actions/workflows/ci.yml)
<!-- prettier-ignore-start -->

[![CI](https://github.com/bytesize-rs/bytesize/actions/workflows/ci.yml/badge.svg)](https://github.com/bytesize-rs/bytesize/actions/workflows/ci.yml)
[![Crates.io Version](https://img.shields.io/crates/v/bytesize.svg)](https://crates.io/crates/bytesize)

`ByteSize` is a utility for human-readable byte count representations.
<!-- prettier-ignore-end -->

<!-- cargo-rdme start -->

`ByteSize` is a semantic wrapper for byte count representations.

Features:

- Pre-defined constants for various size units (e.g., B, Kb, Kib, Mb, Mib, Gb, Gib, ... PB).
- `ByteSize` type which presents size units convertible to different size units.
- Arithmetic operations for `ByteSize`.
- FromStr impl for `ByteSize`, allowing to parse from string size representations like 1.5KiB and 521TiB.
- `FromStr` impl for `ByteSize`, allowing for parsing string size representations like "1.5KiB" and "521TiB".
- Serde support for binary and human-readable deserializers like JSON.

[API Documentation](https://docs.rs/bytesize)
### Examples

## Example

### Human readable representations (SI unit and Binary unit)
Construction using SI or IEC helpers.

```rust
fn assert_display(expected: &str, b: ByteSize) {
assert_eq!(expected, format!("{}", b));
}

#[test]
fn test_display() {
assert_display("215 B", ByteSize::b(215));
assert_display("1.0 KiB", ByteSize::kib(1));
assert_display("301.0 KiB", ByteSize::kib(301));
assert_display("419.0 MiB", ByteSize::mib(419));
assert_display("518.0 GiB", ByteSize::gib(518));
assert_display("815.0 TiB", ByteSize::tib(815));
assert_display("609.0 PiB", ByteSize::pib(609));
}

#[test]
fn test_display_alignment() {
assert_eq!("|357 B |", format!("|{:10}|", ByteSize(357)));
assert_eq!("| 357 B|", format!("|{:>10}|", ByteSize(357)));
assert_eq!("|357 B |", format!("|{:<10}|", ByteSize(357)));
assert_eq!("| 357 B |", format!("|{:^10}|", ByteSize(357)));

assert_eq!("|-----357 B|", format!("|{:->10}|", ByteSize(357)));
assert_eq!("|357 B-----|", format!("|{:-<10}|", ByteSize(357)));
assert_eq!("|--357 B---|", format!("|{:-^10}|", ByteSize(357)));
}

fn assert_to_string(expected: &str, b: ByteSize, si: bool) {
assert_eq!(expected.to_string(), b.to_string_as(si));
}

#[test]
fn test_to_string_as() {
assert_to_string("215 B", ByteSize::b(215), true);
assert_to_string("215 B", ByteSize::b(215), false);

assert_to_string("1.0 KiB", ByteSize::kib(1), true);
assert_to_string("1.0 KB", ByteSize::kib(1), false);

assert_to_string("293.9 KiB", ByteSize::kb(301), true);
assert_to_string("301.0 KB", ByteSize::kb(301), false);

assert_to_string("1.0 MiB", ByteSize::mib(1), true);
assert_to_string("1048.6 KB", ByteSize::mib(1), false);

// a bug case: https://github.com/flang-project/bytesize/issues/8
assert_to_string("1.9 GiB", ByteSize::mib(1907), true);
assert_to_string("2.0 GB", ByteSize::mib(1908), false);

assert_to_string("399.6 MiB", ByteSize::mb(419), true);
assert_to_string("419.0 MB", ByteSize::mb(419), false);

assert_to_string("482.4 GiB", ByteSize::gb(518), true);
assert_to_string("518.0 GB", ByteSize::gb(518), false);

assert_to_string("741.2 TiB", ByteSize::tb(815), true);
assert_to_string("815.0 TB", ByteSize::tb(815), false);

assert_to_string("540.9 PiB", ByteSize::pb(609), true);
assert_to_string("609.0 PB", ByteSize::pb(609), false);
}
use bytesize::ByteSize;

assert!(ByteSize::kib(4) > ByteSize::kb(4));
```

### Arithmetic operations
Display as human-readable string.

```rust
use bytesize::ByteSize;

fn byte_arithmetic_operator() {
let x = ByteSize::mb(1);
let y = ByteSize::kb(100);
assert_eq!("482.4 GiB", ByteSize::gb(518).to_string_as(true));
assert_eq!("518.0 GB", ByteSize::gb(518).to_string_as(false));
```

Arithmetic operations are supported.

```rust
use bytesize::ByteSize;

let plus = x + y;
print!("{}", plus);
let plus = ByteSize::mb(1) + ByteSize::kb(100);
println!("{plus}");

let minus = ByteSize::tb(100) + ByteSize::gb(4);
print!("{}", minus);
}
let minus = ByteSize::tb(1) - ByteSize::gb(4);
assert_eq!(ByteSize::gb(996), minus);
```

<!-- cargo-rdme end -->
Loading