humanize_duration/
macros.rs1#[macro_export]
2macro_rules! unit {
3 ($unit_name:tt, $one:expr) => {
4 pub struct $unit_name;
5 impl $crate::Unit for $unit_name {
6 fn one(&self) -> &'static str {
7 $one
8 }
9
10 fn many(&self) -> &'static str {
11 $one
12 }
13
14 fn format(
15 &self,
16 f: &mut std::fmt::Formatter<'_>,
17 value: u64,
18 allow_zero: bool,
19 started: &mut bool,
20 ) -> std::fmt::Result {
21 if value != 0 || (allow_zero && !*started) {
22 if *started {
23 f.write_str(" ")?;
24 }
25 write!(f, "{}{}", value, $one)?;
26 *started = true;
27 }
28 Ok(())
29 }
30 }
31 };
32 ($unit_name:tt, $one:expr, $many:expr) => {
33 pub struct $unit_name;
34 impl $crate::Unit for $unit_name {
35 fn one(&self) -> &'static str {
36 $one
37 }
38
39 fn many(&self) -> &'static str {
40 $many
41 }
42
43 fn format(
44 &self,
45 f: &mut std::fmt::Formatter<'_>,
46 value: u64,
47 allow_zero: bool,
48 started: &mut bool,
49 ) -> std::fmt::Result {
50 if value != 0 || (allow_zero && !*started) {
51 if *started {
52 f.write_str(" ")?;
53 }
54 if value > 1 || value == 0 {
55 write!(f, "{}{}", value, $many)?;
56 } else {
57 write!(f, "{}{}", value, $one)?;
58 }
59 *started = true;
60 }
61 Ok(())
62 }
63 }
64 };
65}