-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc(1st): the first edition document
- Loading branch information
Showing
6 changed files
with
173 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
name: check | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: [main] | ||
|
||
env: | ||
CARGO_TERM_COLOR: always | ||
|
||
jobs: | ||
fmt: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: dtolnay/rust-toolchain@stable | ||
- uses: Swatinem/rust-cache@v2 | ||
- run: cargo fmt --check | ||
env: | ||
RUSTFLAGS: "-Dwarnings" | ||
|
||
lint: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: dtolnay/rust-toolchain@stable | ||
with: | ||
components: clippy | ||
- uses: Swatinem/rust-cache@v2 | ||
- run: cargo clippy --all-features --all-targets | ||
env: | ||
RUSTFLAGS: "-Dwarnings" | ||
|
||
doc: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: dtolnay/rust-toolchain@stable | ||
- run: cargo install cargo-deadlinks | ||
- run: cargo deadlinks | ||
- run: cargo doc --all-features --no-deps |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
name: test | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: [main] | ||
|
||
env: | ||
CARGO_TERM_COLOR: always | ||
|
||
jobs: | ||
test: | ||
strategy: | ||
matrix: | ||
include: | ||
- os: ubuntu-latest | ||
target: x86_64-unknown-linux-gnu | ||
runs-on: ${{ matrix.os }} | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: dtolnay/rust-toolchain@stable | ||
with: | ||
target: ${{ matrix.target }} | ||
- uses: Swatinem/rust-cache@v2 | ||
- run: cargo test --all-features |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,55 @@ | ||
# Typed Bytesize | ||
|
||
[![crates.io](https://img.shields.io/crates/v/typed-bytesize.svg)](https://crates.io/crates/typed-bytesize) | ||
[![docs.rs](https://docs.rs/typed-bytesize/badge.svg)](https://docs.rs/typed-bytesize/) | ||
[![check](https://github.com/TD-Sky/typed-bytesize/actions/workflows/check.yml/badge.svg)](https://github.com/TD-Sky/typed-bytesize/actions/workflows/check.yml) | ||
[![test](https://github.com/TD-Sky/typed-bytesize/actions/workflows/test.yml/badge.svg)](https://github.com/TD-Sky/typed-bytesize/actions/workflows/test.yml) | ||
|
||
The library provides two types to represent bytesize: | ||
- [ByteSizeSi](https://docs.rs/typed-bytesize/latest/typed_bytesize/struct.ByteSizeSi.html) for **decimal** prefix unit ([SI](https://en.wikipedia.org/wiki/International_System_of_Units)) | ||
- [ByteSizeIec](https://docs.rs/typed-bytesize/latest/typed_bytesize/struct.ByteSizeIec.html) for **binary** prefix unit (IEC 80000-13) | ||
|
||
## Functions | ||
|
||
- Bytesize types can parse each other's units (e.g. `ByteSizeIec` can parse SI values like `114514GB`); | ||
- Bytesize values will only be formatted as the unit has their owned prefix; | ||
- Bytesize types can be converted to each other; | ||
- Supporting *addition*, *subtraction*, *scalar multiplication* arithmetic operations; | ||
- Optional [serde](https://crates.io/crates/serde) support. | ||
|
||
## Example | ||
|
||
```rust | ||
use typed_bytesize::{ByteSizeIec, ByteSizeSi}; | ||
|
||
// SI | ||
assert_eq!(ByteSizeSi::b(114u64), "114".parse().unwrap()); | ||
assert_eq!(ByteSizeSi::mb(114), "114MB".parse().unwrap()); | ||
print!("{}", ByteSizeSi::kb(310)); // 310.0kB | ||
|
||
// IEC | ||
assert_eq!(ByteSizeIec::b(514u64), "514".parse().unwrap()); | ||
assert_eq!(ByteSizeIec::mib(514), "514MiB".parse().unwrap()); | ||
print!("{}", ByteSizeIec::gib(93696)); // 91.5GiB | ||
``` | ||
|
||
For more detailed examples, refer to [tests](https://docs.rs/crate/typed-bytesize/latest/source/src/tests.rs). | ||
|
||
|
||
## BNF | ||
|
||
Parsing follows the rule: | ||
|
||
```ignore | ||
expr ::= num | term | ||
term ::= decimal " "* unit | ||
decimal ::= num | float | ||
float ::= num "." num | ||
num ::= [0-9]+ | ||
``` | ||
|
||
|
||
## Features | ||
|
||
- `serde`: enable [serde](https://crates.io/crates/serde) on `ByteSizeSi` and `ByteSizeIec`. | ||
- `u128`: use `u128` instead of `u64` as inner numeric type to support larger units. (TODO) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters