friendly/lib.rs
1//! Human-friendly display library.
2//!
3//! This module provides a convenient, uniform way to display various types of quantities
4//! in approximate, human-readable format. For example:
5//!
6//! ```
7//! # use friendly::bytes;
8//! let kb = format!("{}", bytes(13200));
9//! assert_eq!(kb.as_str(), "12.89 KiB")
10//! ```
11//!
12//! The various functions provide quick ways to wrap values and types in the appropriate
13//! objects to facilitate their display. Types such as [Quantity] then provide methods to
14//! further customize this presentation.
15//!
16//! ## Features
17//!
18//! This crate supports some features:
19//!
20//! - `chrono` — enables support for types from the Chrono crate (currently just [chrono::Duration])
21
22pub mod quantity;
23pub mod scale;
24pub mod sigfig;
25pub mod temporal;
26
27pub use quantity::Quantity;
28pub use scale::Scale;
29pub use temporal::{duration, seconds};
30
31use quantity::QVal;
32
33/// Display a number of bytes.
34///
35/// By default, this uses binary prefixes:
36///
37/// ```
38/// # use friendly::bytes;
39/// let kb = format!("{}", bytes(13200));
40/// assert_eq!(kb.as_str(), "12.89 KiB")
41/// ```
42///
43/// You can also use decimal prefixes:
44///
45/// ```
46/// # use friendly::bytes;
47/// # use friendly::scale::*;
48/// let kb = format!("{}", bytes(13200).scale(Decimal::AUTO));
49/// assert_eq!(kb.as_str(), "13.20 kB")
50/// ```
51pub fn bytes<V: QVal>(val: V) -> Quantity<V, scale::Binary> {
52 Quantity::binary(val).suffix("B").integral(true)
53}
54
55/// An ordinary auto-scaled value.
56pub fn scalar<V: QVal>(val: V) -> Quantity<V, scale::Decimal> {
57 Quantity::decimal(val)
58}
59
60/// An ordinary auto-scaled integer value.
61pub fn integer<V: QVal>(val: V) -> Quantity<V, scale::Decimal> {
62 Quantity::decimal(val).integral(true)
63}