PrettySize
Strongly-typed file sizes with human-readable formatting, parsing, and unit math for Rust.
Repository Health
Technical Analysis
PrettySize is a lightweight, dependency-free Rust crate (published as size) for expressing, formatting, and manipulating file sizes. It provides a strongly-typed Size value that can be constructed from any numeric type, formatted into human-readable strings with automatically chosen units and precision, parsed back from text, and used in mathematical and logical operations.
Beyond simple display, the crate supports base-2 (KiB, MiB) and base-10 (KB, MB) units, negative sizes, transparent serde serialization to and from raw byte counts, and a no_std mode that keeps the strongly-typed conversions available without any allocation or string handling.
What You Get
- A strongly-typed
Sizevalue constructible from any numeric type and any base-2 or base-10 unit - Automatic human-readable formatting via
Display/to_string()with a.format()builder for base and unit-style control - String parsing through
FromStr/Size::from_str()accepting many textual size formats - Mathematical and logical operations on typed sizes, including full negative-size support
- Optional transparent serde serialization to and from raw byte counts, plus a dependency-free
no_stdmode
Common Use Cases
- Displaying disk usage, download progress, or file sizes in a user-friendly format
- Parsing size limits or quotas from configuration files and CLI arguments
- Serializing typed sizes to and from JSON or network payloads as plain byte counts
Under The Hood
Architecture - The crate is organized around a single Size type defined in src/lib.rs, with formatting in src/fmt.rs, parsing in src/from_str.rs, operator overloads in src/ops.rs, and optional serde glue in src/serde.rs. The Size value stores an underlying byte count and exposes constructors, a .format() builder that produces a formatter honoring base and unit-style choices, and constants for base-2 and base-10 units in the consts namespace.
Tech Stack - Pure Rust (edition 2018) with zero required runtime dependencies. serde is an optional dependency behind a feature flag, and serde_json is used only as a dev-dependency for tests. The std feature is default-on but can be disabled for no_std builds, which drop floating-point conversions, formatting, and parsing while retaining typed size math.
Code Quality - The source ships with dedicated test modules (src/tests.rs, src/tests_nostd.rs) plus inline tests across from_str.rs, serde.rs, and lib.rs, covering both std and no_std paths. Code is small, focused, and idiomatic, using Rust’s generics and enums to keep the API concise without excessive boilerplate.
API Design - The public surface is ergonomic and discoverable: unit-named constructors (from_kb, from_mib), scalar operator support, a fluent .format() builder, and standard-trait implementations (Display, FromStr, arithmetic ops) so Size behaves like a native numeric type. Documentation on docs.rs and a thorough README make getting started nearly frictionless.