sizefilter
Human-readable size string parsing, formatting, arithmetic, and filtering with comparison operators — ">=1GB", "<500KB", "=0".
Installation
Features
- Parse:
"1GB","500KB","1024","1.5MB"→i64bytes - Format:
1073741824→"1.0GB",-1024→"-1.0KB" - Filter:
SizeFilter::ge(1_073_741_824)or">=1GB".parse::<SizeFilter>() - Arithmetic:
+,-,*,/,Negon [Size] — all byte-level - Serde (optional): serialize/deserialize [
Size] & [SizeFilter] as strings i64(notu64) — negative sizes supported- Zero heap allocation in parsing and error paths —
SizeErroris a ZST in.rodata - Binary units (1 KB = 1024 B), matching filesystem convention
#[must_use]annotations throughout — no silently ignored results
Testing
58 tests (47 unit + 11 doc-tests) covering parsing, formatting, filtering, arithmetic, edge cases (whitespace, case sensitivity, negative values, sub-byte decimals, extreme ranges, invalid inputs), round-trip consistency, and serde round-trip.
Quick example
use *;
// Parse a human-readable size to bytes
let bytes = parse_size.unwrap;
assert_eq!;
// Using the Size newtype
let s: Size = "2GB".parse.unwrap;
assert_eq!;
assert_eq!;
// Parse a filter expression
let f: SizeFilter = ">=500MB".parse.unwrap;
assert!;
assert!;
// Use convenience constructors
let f = lt;
assert!;
// Arithmetic
assert_eq!;
Modules
src/
├── lib.rs — Crate root, prelude module
└── size.rs — Size, SizeOp, SizeFilter, SizeError, constants, all ops
Prelude
use sizefilter::prelude::* brings in the most common types and functions:
use *;
// Now available:
// Size, SizeFilter, SizeOp, SizeError, SizeResult
// KB, MB, GB, TB, PB, EB
// parse_size(), format_size(), parse_size_filter()
Error handling
All fallible operations return Result<T, SizeError>. The SizeError enum is
#[non_exhaustive] — new variants may be added in minor releases.
use ;
match parse_size