format_core/
lib.rs

1#![no_std]
2
3use core::fmt::{self, Formatter, Result};
4
5// Generate lazy format macro
6macro_rules! gen_lazy_format {
7    (Debug) => {
8        gen_lazy_format! { @base
9            #[doc = "A lazy format type that implements [Debug](core::fmt::Debug) as base format trait"]
10            Debug
11        }
12    };
13    (Display) => {
14        gen_lazy_format! { @base
15            #[doc = "A lazy format type that implements [Display](core::fmt::Display) as base format trait, [Debug](core::fmt::Debug) as derivative from [Display](core::fmt::Display)"]
16            Display
17        }
18        gen_lazy_format! { @debug Display }
19    };
20    ($self:ident) => {
21        gen_lazy_format! { @base
22            #[doc = concat!(
23                "A lazy format type that implements [",
24                stringify!($self),
25                "](core::fmt::",
26                stringify!($self),
27                ") as base format trait, [Display](core::fmt::Display) and [Debug](core::fmt::Debug) as derivative from [",
28                stringify!($self),
29                "](core::fmt::",
30                stringify!($self),
31                ")"
32            )]
33            $self
34        }
35        gen_lazy_format! { @display $self }
36        gen_lazy_format! { @debug $self }
37    };
38    (@base $(#[doc = $doc:expr])* $self:ident) => {
39        $(#[doc = $doc])*
40        #[derive(Clone, Copy)]
41        pub struct $self<F: Fn(&mut Formatter) -> Result>(pub F);
42
43        impl<F: Fn(&mut Formatter) -> Result> fmt::$self for $self<F> {
44            fn fmt(&self, f: &mut Formatter) -> Result {
45                (self.0)(f)
46            }
47        }
48    };
49    (@display $self:ident) => {
50        impl<F: Fn(&mut Formatter) -> Result> fmt::Display for $self<F> {
51            fn fmt(&self, f: &mut Formatter) -> Result {
52                fmt::$self::fmt(self, f)
53            }
54        }
55    };
56    (@debug $self:ident) => {
57        impl<F: Fn(&mut Formatter) -> Result> fmt::Debug for $self<F> {
58            fn fmt(&self, f: &mut Formatter) -> Result {
59                f.debug_tuple(stringify!($self))
60                    .field(&Debug(&self.0))
61                    .finish()
62            }
63        }
64    };
65}
66
67gen_lazy_format! { Debug }
68gen_lazy_format! { Display }
69gen_lazy_format! { Binary }
70gen_lazy_format! { LowerExp }
71gen_lazy_format! { LowerHex }
72gen_lazy_format! { Octal }
73gen_lazy_format! { Pointer }
74gen_lazy_format! { UpperExp }
75gen_lazy_format! { UpperHex }