Expand description
Readable
Human readable strings.
This library:
- Transforms various data types into human-readable strings
- Parses raw string data into human-readable versions
- Provides various string types and utilities
Most of the strings are implemented as fixed length, stack allocated arrays that are Copy-able.
In general, readable types are often used where you need to quickly format some data into a more human-friendly string, display it, then throw it away (although most readable types are perfectly fine to permanently store).
Creation of readable types is relatively performant.
Examples
Unsigned integers:
let a = readable::Unsigned::from(1000_u64);
assert_eq!(a, 1000_u64);
assert_eq!(a, "1,000");Signed integers:
let a = readable::Int::from(-1000);
assert_eq!(a, -1000);
assert_eq!(a, "-1,000");Floats:
let a = readable::Float::from(1000.123);
assert_eq!(a, 1000.123);
assert_eq!(a, "1,000.123");Percents:
let a = readable::Percent::from(1000.123);
assert_eq!(a, 1000.123);
assert_eq!(a, "1,000.12%");Runtime:
let a = readable::Runtime::from(11111.0);
assert_eq!(a, 11111.0);
assert_eq!(a, "3:05:11");Time:
let a = readable::TimeFull::from(86399_u32);
assert_eq!(a, 86399_u32);
assert_eq!(a, "23 hours, 59 minutes, 59 seconds");Date:
let a = readable::Date::from_str("2014-12-31").unwrap();
assert_eq!(a, (2014, 12, 31));
assert_eq!(a, "2014-12-31");Comparison
All number types implement PartialEq against str and their internal numbers.
This is comparing b’s inner String:
let a = std::time::Duration::from_secs(86399);
let b = readable::TimeFull::from(a);
assert_eq!(b, "23 hours, 59 minutes, 59 seconds");This is comparing a’s inner i64:
let a = readable::Int::from(-1000);
assert_eq!(a, -1000);This compares both the u64 AND String inside a and b:
let a = readable::Unsigned::from(1000_u64);
let b = readable::Unsigned::from(1000_u64);
assert_eq!(a, b);Arithmetic
All number types implement the common arithmetic operators +, -, /, *, %, outputting a new Self.
+ Addition
let f1 = readable::Float::from(1.0);
let f2 = readable::Float::from(2.0);
assert_eq!(f1 + f2, 3.0);- Subtraction
let p50 = readable::Percent::from(50.0);
let p25 = readable::Percent::from(25.0);
assert_eq!(p50 - p25, "25.00%");/ Division
let u100 = readable::Unsigned::from(100_u64);
let u10 = readable::Unsigned::from(10_u64);
assert_eq!(u100 / u10, 10);* Muliplication
let u10 = readable::Unsigned::from(10_u64);
assert_eq!(u10 * u10, readable::Unsigned::from(100_u64));% Modulo
let u10 = readable::Unsigned::from(10_u64);
assert_eq!(u10 % u10, 0);Feature Flags
| Flag | Purpose |
|---|---|
serde | Enables serde on most types |
bincode | Enables bincode 2.0.0-rc.3’s Encode/Decode on most types |
byte | Enables the byte module |
date | Enables the date module |
num | Enables the num module |
run | Enables the run module |
str | Enables the str module |
time | Enables the time module |
toa | Enables the toa module |
full | Enables everything above. This is on by default. |
Re-Exports
Types are separated per module depending on what type of data they take as input, and what type of data they output.
use readable::num::{ // Number formatting
Unsigned, // Formats u8, u16, u32, etc...
Int, // Formats i8, i16, i32, etc...
};All major types are exported to the root, so they can be imported without specifying the full path:
use readable::HeadTail; // shorter, preferred.
use readable::str::HeadTail; // longerRe-exports
pub use str::Str;pub use str::HeadTail;pub use num::Unsigned;pub use num::Int;pub use num::Float;pub use num::Percent;pub use run::Runtime;pub use run::RuntimePad;pub use run::RuntimeMilli;pub use run::RuntimeUnion;pub use time::Time;pub use time::TimeFull;pub use date::Date;pub use toa::Itoa;pub use toa::ItoaTmp;pub use toa::Dtoa;pub use toa::DtoaTmp;pub use byte::Byte;
Modules
- Human-readable byte formatting
- Human-readable date formatting
- Human-readable number formatting
- Human-readable runtime formatting
- General string utilities
- Human-readable time formatting Human-readable time
- Fast integer/float to string conversion