#bytes #size #units #formatting #prettysize

size

Units and formatting for file sizes in base2 and base10

3 releases

0.1.2 Dec 7, 2018
0.1.1 Aug 16, 2018
0.1.0 Aug 11, 2018

#29 in Value formatting

Download history 26/week @ 2019-12-15 5/week @ 2019-12-22 18/week @ 2019-12-29 22/week @ 2020-01-05 516/week @ 2020-01-12 365/week @ 2020-01-19 280/week @ 2020-01-26 435/week @ 2020-02-02 296/week @ 2020-02-09 375/week @ 2020-02-16 511/week @ 2020-02-23 367/week @ 2020-03-01 532/week @ 2020-03-08 596/week @ 2020-03-15 546/week @ 2020-03-22 319/week @ 2020-03-29

78 downloads per month
Used in 3 crates

MIT license

18KB
393 lines

PrettySize, rust edition

crates.io docs.rs

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.

Features

PrettySize provides

  • definitions for the base-two and base-ten file size units defined as pub const in the size namespace, available both in abbreviated and unabridged forms (i.e. size::EiB and size::EXBIBYTE or size::GB and size::GIGABYTE),
  • a Unit enum that defines the base-two and base-ten units,
  • a Size<T> enum that can be used to hold a typed file size (e.g. let size = Size::Terabytes(4);),
  • an std::Display impl for Size to display sizes in a human-readable format,
  • a Size.to_string(..) method that allows you to specify the base of the human-readable units and their style (smart, abbreviated, or full and their lowercase variants)

Usage

Cargo.toml:

[dependencies]
size = "0.1"

and in your code:

extern crate size;
use size::{Base, Size, Style};

fn main() {
        let byte_count = 42 * size::KiB;
        assert_eq!(43__008, byte_count);

        let byte_count = Size::Kilobytes(42);
        assert_eq!(42__000, byte_count.bytes());

        // `Size` can take any numeric type you throw at it
        let byte_count2 = Size::Mebibytes(0.040055);
        assert_eq!(byte_count.bytes(), byte_count2.bytes());

        // And for those of you that haven't yet drunk the base-two Kool-Aid:
        let byte_count = Size::Kilobytes(42);
        assert_eq!(byte_count.bytes(), 42_000);

        println!("{}, I say!", byte_count);
        // prints "41 KiB, I say!"

        println!("{}, I meant!", byte_count.to_string(Base::Base10, Style::Abbreviated));
        // prints "42 KB, I meant!"
}

About

This project started off as a port of the 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.

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,
  • Implementing direct unit-to-unit conversion,
  • Implementing format specifiers to allow using format!(..) directly to obtain output in the desired base and style,
  • Bypassing double conversion when formatting Size<T> for integral T types,

Pull requests are welcomed!

Dependencies

~135KB