Expand description
Human readable data formatting.
This crate turns various data into human-readable strings.
Most of the internal strings are implemented as fixed length, stack allocated arrays that are Copy-able.
Feature flags
| Flag | Purpose |
|---|---|
serde | Enables serde on all types |
bincode | Enables bincode 2.0.0’s Encode/Decode on all types |
ignore_nan_inf | Disables checking f64’s for f64::NAN, f64::INFINITY, and f64::NEG_INFINITY |
inline_date | Inlines any Date that is in YYYY-MM-HH format and is between year 1900-2100 |
inline_time | Inlines any Time that is under 1 hour, 1 minute (0..=3660) |
inline_runtime | Inlines ALL of Runtime (0:00..99:59:59/0..=359999) |
full | Enables everything above |
Warning: The inline_* features are disabled by default. While they increase speed,
they also heavily increase build time and binary size.
Unsigned integers:
let a = readable::Unsigned::from(1000_u64);
assert!(a == 1000_u64);
assert!(a == "1,000");Signed integers:
let a = readable::Int::from(-1000);
assert!(a == -1000);
assert!(a == "-1,000");Floats:
let a = readable::Float::from(1000.123);
assert!(a == 1000.123);
assert!(a == "1,000.123");Percents:
let a = readable::Percent::from(1000.123);
assert!(a == 1000.123);
assert!(a == "1,000.12%");Runtime:
let a = readable::Runtime::from(11111_u16);
assert!(a == 11111);
assert!(a == "3:05:11");Time:
let a = readable::Time::from(86399_u64);
assert!(a == 86399_u64);
assert!(a == "23 hours, 59 minutes, 59 seconds");Date:
let a = readable::Date::from_str("2014-12-31").unwrap();
assert!(a == (2014, 12, 31));
assert!(a == "2014-12-31");Comparison
All types implement Display, PartialEq, PartialEq<&str> and PartialEq for their inner number primitive.
Example 1:
let a = std::time::Duration::from_secs(86399);
let b = readable::Time::from(a);
assert!(b == "23 hours, 59 minutes, 59 seconds");This is comparing b’s inner String.
Example 2:
let a = readable::Int::from(-1000);
assert!(a == -1000);This is comparing a’s inner i64.
Example 3:
let a = readable::Unsigned::from(1000_u64);
let b = readable::Unsigned::from(1000_u64);
assert!(a == b);This compares both the u64 AND String inside a and b.
Math
Most types implement +, -, /, *, %, outputting a new Self.
Example - Add +:
let f1 = readable::Float::from(1.0);
let f2 = readable::Float::from(2.0);
let f3 = readable::Float::from(3.0);
assert!(f1 + f2 == f3);Example - Sub -:
let p50 = readable::Percent::from(50.0);
let p25 = readable::Percent::from(25.0);
assert!(p50 - p25 == "25.00%");Example - Div /:
let u100 = readable::Unsigned::from(100_u64);
let u10 = readable::Unsigned::from(10_u64);
assert!(u100 / u10 == 10);Example - Mul *:
let u10 = readable::Unsigned::from(10_u64);
assert!(u10 * u10 == readable::Unsigned::from(100_u64));Example - Rem %:
let u10 = readable::Unsigned::from(10_u64);
assert!(u10 % u10 == 0);Re-exports
Macros
Structs
- A recent date that is in
YEAR-MONTH-DAYformat - Human readable float.
- Human readable signed integer.
- Human readable percentage.
- Human readable “audio/video runtime” in
H:M:Sformat. - Human-readable
std::time::Duration. - Human readable unsigned integer.
Constants
- The separator character. This looks like:
1,000 - Returned when using [
Runtime::hour] - UTF-8 byte encoding of
HOUR_RUNTIME - Returned when calling [
Runtime::hour] - Returned when encountering an
INFINITYvariant of anf32/f64. - The max length the inner buffer within [
Unsigned] or [Int] can be. - The text [
Runtime] will returnUNKNOWN_RUNTIME - UTF-8 byte encoding of
MAX_RUNTIME - The max input to [
Runtime] before it overflows and returnsUNKNOWN_RUNTIME - Returned when using [
Runtime::minute] - UTF-8 byte encoding of
MINUTE_RUNTIME - Returned when calling [
Runtime::minute] - Returned when using [
Runtime::second] - UTF-8 byte encoding of
SECOND_RUNTIME - Returned when calling [
Runtime::second] - Returned when using an
*::unknown()function - Returned when using [
Date::unknown] - UTF-8 byte encoding of
UNKNOWN_DATE, aka:????-??-?? - Returned when using [
Float::unknown] - UTF-8 byte encoding of
UNKNOWN - Returned when using [
Percent::unknown] - Returned when using [
Runtime::unknown] - UTF-8 byte encoding of
UNKNOWN_DATE, aka:?:?? - Returned when using [
Int::zero] or [Unsigned::zero] - Returned when using [
Float::zero] - Returned when using [
Unsigned::zero] or [Int::zero] - UTF-8 byte encoding of
ZERO_NUMfor [Unsigned] - Returned when using [
Percent::zero] - Returned when using [
Runtime::zero] - UTF-8 byte encoding of
ZERO_RUNTIME - Returned when calling [
Runtime::zero]