Skip to content

Commit

Permalink
Add documentation for Size::from_str()
Browse files Browse the repository at this point in the history
  • Loading branch information
mqudsi committed Apr 26, 2024
1 parent 2547a6d commit 99e6bde
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ fn main() {
}
```

## Parsing and formatting

The `size` crate supports parsing textual representations of file sizes into strongly typed `Size` objects, both via the `Size::from_str()` function and its `FromStr` implementation that lets you call `"1234 kilobytes".parse()`.

The `Size` type implements `std::fmt::Display` (in addition to many other traits), which provides a facility to generate properly formatted textual representations of file sizes via the `Size::to_string()` impl of the `ToString` trait or when used in a `format!(..., Size)` context.

By default, `Size` objects are formatted as base-2 (KiB, MiB, etc) with heuristically chosen precision and units. The member function `Size::format()` can be used to override the unit base (e.g. MB vs MiB) and whether or not abbreviated unit names are used (e.g. KiB vs Kebibyte).

Feel free to open a GitHub issue or PR if you need further control over formatting (precision, case, etc)!

## `no_std` usage

Add the crate to `Cargo.toml` with `default-features` disabled for `no_std` support:
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@
//!
//! See the documentation of the [`ops`] module for more on this topic.
//!
//! ## Parsing sizes from text
//!
//! The [`Size::from_str()`] function can be used to convert the most commonly encountered textual
//! representations of file sizes into properly typed `Size` objects, with flexible support for
//! various input whitespace formatting, abbreviated/full unit names, mixed upper/lower-case
//! representation, etc.
//!
//! ## Crate features
//!
//! This crate currently has one feature (`std`), enabled by default. If compiled with
Expand Down

0 comments on commit 99e6bde

Please sign in to comment.