Skip to content

Commit

Permalink
feat: fix fork, add clippy test in CI (#4)
Browse files Browse the repository at this point in the history
Signed-off-by: MrCroxx <[email protected]>
  • Loading branch information
MrCroxx committed Sep 10, 2024
1 parent bfe2430 commit 54525bc
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 88 deletions.
150 changes: 72 additions & 78 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
## ByteSize

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

Forked from https://github.com/hyunsik/bytesize .

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

Features:
* Pre-defined constants for various size units (e.g., B, Kb, kib, Mb, Mib, Gb, Gib, ... PB)
* 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.
* Artimetic operations for `ByteSize`
* FromStr impl for `ByteSize`, allowing to parse from string size representations like 1.5KiB and 521TiB.
* Serde support for binary and human-readable deserializers like JSON

[API Documentation](https://docs.rs/bytesize/)
[API Documentation](https://docs.rs/foyer-bytesize/)

### Differences from the original `bytesize`

- Use SI format by default with `Display`.
- Use "KiB" for SI unit.

## Usage

Expand All @@ -24,84 +31,71 @@ bytesize = { package = "foyer-bytesize", version = "1", features = ["serde"]}
```

## Example

### Human readable representations (SI unit and Binary unit)
```rust
#[allow(dead_code)]
fn assert_display(expected: &str, b: ByteSize) {
assert_eq!(expected, format!("{}", b));
}

#[test]
fn test_display() {
assert_display("215 B", ByteSize(215));
assert_display("215 B", ByteSize::b(215));
assert_display("1.0 KB", ByteSize::kb(1));
assert_display("301.0 KB", ByteSize::kb(301));
assert_display("419.0 MB", ByteSize::mb(419));
assert_display("518.0 GB", ByteSize::gb(518));
assert_display("815.0 TB", ByteSize::tb(815));
assert_display("609.0 PB", ByteSize::pb(609));
}

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() {
assert_to_string("215 B", ByteSize(215), true);
assert_to_string("215 B", ByteSize(215), false);

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

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);
}

#[test]
fn test_parsing_from_str() {
// shortcut for writing test cases
fn parse(s: &str) -> u64 {
s.parse::<ByteSize>().unwrap().0
}

assert_eq!("0".parse::<ByteSize>().unwrap().0, 0);
assert_eq!(parse("0"), 0);
assert_eq!(parse("500"), 500);
assert_eq!(parse("1K"), Unit::KiloByte * 1);
assert_eq!(parse("1Ki"), Unit::KibiByte * 1);
assert_eq!(parse("1.5Ki"), (1.5 * Unit::KibiByte) as u64);
assert_eq!(parse("1KiB"), 1 * Unit::KibiByte);
assert_eq!(parse("1.5KiB"), (1.5 * Unit::KibiByte) as u64);
assert_eq!(parse("3 MB"), Unit::MegaByte * 3);
assert_eq!(parse("4 MiB"), Unit::MebiByte * 4);
assert_eq!(parse("6 GB"), 6 * Unit::GigaByte);
assert_eq!(parse("4 GiB"), 4 * Unit::GibiByte);
assert_eq!(parse("88TB"), 88 * Unit::TeraByte);
assert_eq!(parse("521TiB"), 521 * Unit::TebiByte);
assert_eq!(parse("8 PB"), 8 * Unit::PetaByte);
assert_eq!(parse("8P"), 8 * Unit::PetaByte);
assert_eq!(parse("12 PiB"), 12 * Unit::PebiByte);
}
```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);
}
```

### Arithmetic operations
Expand Down
20 changes: 10 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub const TIB: u64 = 1_099_511_627_776;
pub const PIB: u64 = 1_125_899_906_842_624;

static UNITS: &str = "KMGTPE";
static UNITS_SI: &str = "kMGTPE";
static UNITS_SI: &str = "KMGTPE";
static LN_KB: f64 = 6.931471806; // ln 1024
static LN_KIB: f64 = 6.907755279; // ln 1000

Expand Down Expand Up @@ -212,7 +212,7 @@ pub fn to_string(bytes: u64, si_prefix: bool) -> String {

impl Display for ByteSize {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.pad(&to_string(self.0, false))
f.pad(&to_string(self.0, true))
}
}

Expand Down Expand Up @@ -425,12 +425,12 @@ mod tests {
#[test]
fn test_display() {
assert_display("215 B", ByteSize::b(215));
assert_display("1.0 KB", ByteSize::kb(1));
assert_display("301.0 KB", ByteSize::kb(301));
assert_display("419.0 MB", ByteSize::mb(419));
assert_display("518.0 GB", ByteSize::gb(518));
assert_display("815.0 TB", ByteSize::tb(815));
assert_display("609.0 PB", ByteSize::pb(609));
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]
Expand All @@ -454,10 +454,10 @@ mod tests {
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 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("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);
Expand Down

0 comments on commit 54525bc

Please sign in to comment.