Crates.io | typed-bytesize |
lib.rs | typed-bytesize |
version | 0.1.3 |
source | src |
created_at | 2024-02-25 05:27:07.543988 |
updated_at | 2024-10-04 08:44:10.540416 |
description | Represent bytesize in decimal or binary prefix unit |
homepage | |
repository | https://github.com/TD-Sky/typed-bytesize |
max_upload_size | |
id | 1152200 |
size | 28,519 |
The library provides two types to represent bytesize:
ByteSizeIec
can parse SI values like 114514GB
);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.
Parsing follows the rule:
expr ::= num | term
term ::= decimal " "* unit
decimal ::= num | float
float ::= num "." num
num ::= [0-9]+
serde
: enable serde on ByteSizeSi
and ByteSizeIec
.u128
: use u128
instead of u64
as inner numeric type to support larger units. (TODO)