macros_rs/
fmt.rs

1#[doc(hidden)]
2#[cfg(feature = "color")]
3pub use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
4
5#[doc(hidden)]
6#[macro_export]
7macro_rules! _lib_string {
8    () => {
9        String::new()
10    };
11    ($x:expr $(,)?) => {
12        ToString::to_string(&$x)
13    };
14}
15
16#[doc(hidden)]
17#[macro_export]
18macro_rules! _lib_str {
19    ($x: expr) => {{
20        let out: &'static str = { Box::leak($x.into_boxed_str()) };
21        out
22    }};
23}
24
25#[doc(hidden)]
26#[macro_export]
27macro_rules! _lib_fmtstr {
28    ($($t:tt)*) => {{
29        let out: &'static str = { Box::leak(format!($($t)*).into_boxed_str()) };
30        out
31    }};
32}
33
34#[doc(hidden)]
35#[macro_export]
36macro_rules! _fmt_writer {
37    ($func:ident, $handle:expr, $($arg:tt)*) => {{
38        use std::io::Write;
39        let result = $func!(&mut $handle, $($arg)*);
40        result.expect("Unable to write to handle (file handle closed?)");
41    }};
42}
43
44#[doc(hidden)]
45#[macro_export]
46macro_rules! _fmt_writer_crash {
47    ($func:ident, $handle:expr, $($arg:tt)*) => {{
48        use std::io::Write;
49        let result = $func!(&mut $handle, $($arg)*);
50        result.expect("Unable to write to handle (file handle closed?)");
51        std::process::exit(1)
52    }};
53}
54
55#[doc(hidden)]
56#[macro_export]
57#[cfg(feature = "color")]
58macro_rules! _io_stderr {
59    () => {{
60        use $crate::fmt::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
61        let mut stderr = StandardStream::stderr(ColorChoice::Always);
62        stderr.set_color(ColorSpec::new().set_fg(Some(Color::Red))).expect("Unable to set stderr color (file handle closed?)");
63        stderr
64    }};
65}
66
67#[doc(hidden)]
68#[macro_export]
69#[cfg(not(feature = "color"))]
70macro_rules! _io_stderr {
71    () => {
72        std::io::stderr()
73    };
74}
75
76#[doc(hidden)]
77#[macro_export]
78#[cfg(not(feature = "color"))]
79macro_rules! _lib_error {
80    ($($arg:tt)*) => {
81        $crate::_fmt_writer!(write, $crate::_io_stderr!(), $($arg)*)
82    };
83}
84
85#[doc(hidden)]
86#[macro_export]
87#[cfg(not(feature = "color"))]
88macro_rules! _lib_errorln {
89    ($($arg:tt)*) => {
90        $crate::_fmt_writer!(writeln, $crate::_io_stderr!(), $($arg)*)
91    };
92}
93
94#[doc(hidden)]
95#[macro_export]
96#[cfg(not(feature = "color"))]
97macro_rules! _lib_crash {
98    ($($arg:tt)*) => {
99        $crate::_fmt_writer_crash!(write, $crate::_io_stderr!(), $($arg)*)
100    };
101}
102
103#[doc(hidden)]
104#[macro_export]
105#[cfg(not(feature = "color"))]
106macro_rules! _lib_crashln {
107    ($($arg:tt)*) => {
108        $crate::_fmt_writer_crash!(writeln, $crate::_io_stderr!(), $($arg)*)
109    };
110}
111
112#[doc(hidden)]
113#[macro_export]
114#[cfg(feature = "color")]
115macro_rules! _lib_error {
116    ($($arg:tt)*) => {
117        $crate::_fmt_writer!(write, $crate::_io_stderr!(), $($arg)*)
118    };
119}
120
121#[doc(hidden)]
122#[macro_export]
123#[cfg(feature = "color")]
124macro_rules! _lib_errorln {
125    ($($arg:tt)*) => {
126        $crate::_fmt_writer!(writeln, $crate::_io_stderr!(), $($arg)*)
127    };
128}
129
130#[doc(hidden)]
131#[macro_export]
132#[cfg(feature = "color")]
133macro_rules! _lib_crash {
134    ($($arg:tt)*) => {
135        $crate::_fmt_writer_crash!(write, $crate::_io_stderr!(), $($arg)*)
136    };
137}
138
139#[doc(hidden)]
140#[macro_export]
141#[cfg(feature = "color")]
142macro_rules! _lib_crashln {
143    ($($arg:tt)*) => {
144        $crate::_fmt_writer_crash!(writeln, $crate::_io_stderr!(), $($arg)*)
145    };
146}
147
148#[doc(inline)]
149pub use _lib_string as string;
150
151#[doc(inline)]
152pub use _lib_str as str;
153
154#[doc(inline)]
155pub use _lib_fmtstr as fmtstr;
156
157#[doc(inline)]
158pub use _lib_error as error;
159
160#[doc(inline)]
161pub use _lib_errorln as errorln;
162
163#[doc(inline)]
164pub use _lib_crash as crash;
165
166#[doc(inline)]
167pub use _lib_crashln as crashln;