1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//! Human readable formatting of numbers and durations. No dependencies, `no_std`, no allocation,
//! no `unsafe`.
//!
//! ```
//! # use humanize_proper::{bin, dur};
//! println!("downloaded {} in {}", bin(4_823_512), dur(93));
//! // downloaded 4.6MiB in 01m33s
//! # assert_eq!(bin(4_823_512).to_string(), "4.6MiB");
//! # assert_eq!(dur(93).to_string(), "01m33s");
//! ```
//!
//! # Numbers
//!
//! A large number is scaled down until it fits into a few digits, and a unit prefix carries the
//! rest of the magnitude. Which prefix system is the right one depends on what is being counted, so
//! both are available and neither is a global setting:
//!
//! - decimal (metric, factor 1000) with the prefixes `k M G T P E Z Y R Q`, for disk capacities,
//! data rates, frequencies and everything else the SI touches: [`dec`], [`NumFmt::dec`].
//! - binary (factor 1024) with the prefixes `Ki Mi Gi Ti Pi Ei Zi Yi Ri Qi`, for file sizes, memory
//! and everything a computer counts in powers of two: [`bin`], [`NumFmt::bin`].
//!
//! ```
//! # use humanize_proper::{bin, dec};
//! // A file on disk, the way `ls -lh` shows it.
//! println!("archive.tar.zst {}", bin(4_823_512));
//! // archive.tar.zst 4.6MiB
//!
//! // A disk's capacity, the way the vendor prints it on the box.
//! println!("capacity: {}", dec(2_000_398_934_016u64));
//! // capacity: 2TB
//! # assert_eq!(bin(4_823_512).to_string(), "4.6MiB");
//! # assert_eq!(dec(2_000_398_934_016u64).to_string(), "2TB");
//! ```
//!
//! Byte formatting is the default. [`bin_no_unit`] and [`dec_no_unit`] drop the trailing `B`, and
//! [`NumFmt::unit`] replaces it with something else:
//!
//! ```
//! # use humanize_proper::{dec_no_unit, NumFmt};
//! println!("{} rows imported", dec_no_unit(18_500_000));
//! // 18.5M rows imported
//!
//! println!("clock: {}", NumFmt::dec().unit("Hz").fmt(4_700_000_000u64));
//! // clock: 4.7GHz
//! # assert_eq!(dec_no_unit(18_500_000).to_string(), "18.5M");
//! # assert_eq!(NumFmt::dec().unit("Hz").fmt(4_700_000_000u64).to_string(), "4.7GHz");
//! ```
//!
//! [`NumFmt`] is `Copy` and all of its builder methods are `const`, so a format used in several
//! places can live in a constant:
//!
//! ```
//! # use humanize_proper::NumFmt;
//! const RATE: NumFmt = NumFmt::dec().unit("B/s").space(true);
//!
//! println!("throughput: {}", RATE.fmt(2_411_756));
//! // throughput: 2.41 MB/s
//! # assert_eq!(RATE.fmt(2_411_756).to_string(), "2.41 MB/s");
//! ```
//!
//! Values below 1 print as a fraction of the unit, which is what a byte count wants. For measured
//! quantities, [`NumFmt::small_prefixes`] switches them to the SI submultiples
//! `m ยต n p f a z y r q`:
//!
//! ```
//! # use humanize_proper::NumFmt;
//! const VOLT: NumFmt = NumFmt::dec().unit("V").small_prefixes(true);
//!
//! println!("offset: {}", VOLT.fmt(0.0125));
//! // offset: 12.5mV
//!
//! println!("noise floor: {}", VOLT.fmt(0.000_000_47));
//! // noise floor: 470nV
//! # assert_eq!(VOLT.fmt(0.0125).to_string(), "12.5mV");
//! # assert_eq!(VOLT.fmt(0.000_000_47).to_string(), "470nV");
//! ```
//!
//! Integers are scaled with exact `u128` arithmetic and never take a detour through `f64`, so
//! rounding rolls over into the next prefix where it should, and the extremes stay precise:
//!
//! ```
//! # use humanize_proper::{bin, dec};
//! println!("{} {}", dec(999_999), bin(u64::MAX));
//! // 1MB 16EiB
//! # assert_eq!(format!("{} {}", dec(999_999), bin(u64::MAX)), "1MB 16EiB");
//! ```
//!
//! Every primitive integer type plus `f32` and `f64` is accepted, through [`ToValue`].
//!
//! # Durations
//!
//! The default style is `32y007d_05h01m05s`: years and days, then hours, minutes and seconds, with
//! `_` between the two groups. Leading units that are zero are dropped, so a short duration stays
//! short.
//!
//! ```
//! # use humanize_proper::dur;
//! println!("uptime: {}", dur(3665));
//! // uptime: 01h01m05s
//! # assert_eq!(dur(3665).to_string(), "01h01m05s");
//! ```
//!
//! Zero padding keeps columns aligned, which is what a log or a table wants. In a sentence, trim it
//! away with [`Zeros`], and cut the display off at the unit that carries the message with
//! [`DurFmt::smallest`]:
//!
//! ```
//! # use humanize_proper::{DurFmt, TimeUnit, Zeros};
//! const REMAINING: DurFmt = DurFmt::new().zeros(Zeros::TrimLargest).smallest(TimeUnit::Day);
//!
//! println!("certificate expires in {}", REMAINING.fmt(7_776_000));
//! // certificate expires in 90d
//!
//! println!("took {}", DurFmt::new().zeros(Zeros::TrimAll).fmt(3665));
//! // took 1h1m5s
//! # assert_eq!(REMAINING.fmt(7_776_000).to_string(), "90d");
//! # assert_eq!(DurFmt::new().zeros(Zeros::TrimAll).fmt(3665).to_string(), "1h1m5s");
//! ```
//!
//! For timings, the seconds field takes a fraction, selected with [`Frac`].
//! [`core::time::Duration`] is accepted directly; for anything finer than nanoseconds there is
//! [`Dur`], which resolves down to attoseconds:
//!
//! ```
//! # use core::time::Duration;
//! # use humanize_proper::{dur, Dur, DurFmt, Frac};
//! println!("{}", dur(Duration::from_millis(90_500)));
//! // 01m30s
//!
//! let elapsed = Dur::from_nanos(65_123_456_789);
//!
//! println!("finished in {}", DurFmt::new().frac(Frac::Millis).fmt(elapsed));
//! // finished in 01m05.123s
//!
//! println!("one attosecond is {}", DurFmt::new().frac(Frac::Attos).fmt(Dur::from_attos(1)));
//! // one attosecond is 00.000000000000000001s
//! # assert_eq!(dur(Duration::from_millis(90_500)).to_string(), "01m30s");
//! # assert_eq!(DurFmt::new().frac(Frac::Millis).fmt(elapsed).to_string(), "01m05.123s");
//! # let attos = DurFmt::new().frac(Frac::Attos).fmt(Dur::from_attos(1)).to_string();
//! # assert_eq!(attos, "00.000000000000000001s");
//! ```
//!
//! A year is 365.2425 days, which is exactly `31_556_952` seconds, so day counts run `0..=365` and
//! the arithmetic stays exact at second granularity. Override it with [`DurFmt::secs_per_year`].
//!
//! # Display behavior
//!
//! Both formatters return values implementing [`core::fmt::Display`] that render through
//! [`core::fmt::Formatter::pad`], so width, alignment and fill work as usual and nothing is
//! allocated on the way:
//!
//! ```
//! # use humanize_proper::{bin, dur};
//! for (name, size, age) in [("index.db", 1_048_576, 90), ("wal.log", 6_291_456, 3_600)] {
//! println!("{name:<10}{:>9}{:>11}", bin(size), dur(age));
//! }
//! // index.db 1MiB 01m30s
//! // wal.log 6MiB 01h00m00s
//! # assert_eq!(format!("{:>9}", bin(1_048_576)), " 1MiB");
//! # assert_eq!(format!("{:>11}", dur(3_600)), " 01h00m00s");
//! ```
//!
//! # Scope
//!
//! Deliberately out of scope: localization such as a decimal comma or grouped integer digits, and
//! parsing, since this library only formats.
pub use ;
pub use ;
pub use ;
/// Formats a byte count with binary prefixes, e.g. `1.5KiB`.
///
/// ```
/// # use humanize_proper::bin;
/// assert_eq!(bin(1_048_576).to_string(), "1MiB");
/// ```
/// Formats a byte count with SI prefixes, e.g. `1.5kB`.
///
/// ```
/// # use humanize_proper::dec;
/// assert_eq!(dec(1_000_000).to_string(), "1MB");
/// ```
/// Formats a number with binary prefixes and no unit, e.g. `1.5Ki`.
///
/// ```
/// # use humanize_proper::bin_no_unit;
/// assert_eq!(bin_no_unit(1_048_576).to_string(), "1Mi");
/// ```
/// Formats a number with SI prefixes and no unit, e.g. `1.5k`.
///
/// ```
/// # use humanize_proper::dec_no_unit;
/// assert_eq!(dec_no_unit(1_000_000).to_string(), "1M");
/// ```
/// Formats a duration in the default style, e.g. `32y007d_05h01m05s`.
///
/// ```
/// # use humanize_proper::dur;
/// assert_eq!(dur(90).to_string(), "01m30s");
/// ```