PrettySize, rust edition
A comprehensive file size crate for rust applications, meant to be light and effective. Includes utilities for human-readable formatting of file sizes as well as converting between different base-two and base-ten size units and performing both mathematical and logical operations on strongly-typed file sizes.
See the crate documentation for a more complete summary of what this crate can do and how to use it.
Features
PrettySize
provides
- a
Size
type that can be used to hold a strongly-typed size (e.g.let size = Size::from_gigabytes(4)
) and perform operations on it, - definitions for the base-two and base-ten file size units defined as
pub const
in thesize::consts
namespace, available both in abbreviated and unabridged forms (i.e.consts::KiB
andconsts::KIBIBYTE
orconsts::GB
andconsts::GIGABYTE
), - an
std::Display
impl forSize
to automatically display sizes in a human-readable format, automatically choosing the best size unit and numeric precision to give the nicest results (you can also useSize::to_string()
instead). - a
Size.format()
method that gives you more control over how sizes are converted to a textual representation, letting you to specify the base of the human-readable units and their style (smart, abbreviated, or full; plus their lowercase variants). - mathematical and logical operations on strongly-typed
Size
values, - full support for expressing negative sizes (e.g. the difference between two sizes, or the amount of space reclaimed on a disk)
This crate can also be used in no_std
mode (by compiling with default features
disabled). This disables string conversion/formatting but keeps all the strongly-typed
size conversion and mathematical/logical operations available.
This crate is free of any dependencies.
Usage
Cargo.toml:
[]
= "0.4"
and in your code:
use consts;
use ;
About
This project started off as a port of Mahmoud's
PrettySize.NET library from C# to Rust. Like
the C# edition of this project. Rust's richer enum
types and powerful generics made
implementing a custom Size
generic over the number type without verbosity additionally
possible. Its scope has since grown considerably.
License
PrettySize
is written and maintained by Mahmoud Al-Qudsi of NeoSmart Technologies and
released to the general public under the terms of the MIT public license.
To-Do
- Providing a
FromStr
impl to parse file sizes (coming soon!)
Pull requests are welcome!