garden/
macros.rs

1/// Print a message to stderr with an "error: " prefix
2///
3/// Parameters:
4/// - `args`: A `std::fmt::Arguments`
5pub(crate) fn error(args: std::fmt::Arguments) {
6    eprintln!("error: {args}");
7}
8
9/// Print a message to stderr with a "debug: " prefix
10///
11/// Parameters:
12/// - `args`: A `std::fmt::Arguments`
13pub(crate) fn debug(args: std::fmt::Arguments) {
14    eprintln!("debug: {args}");
15}
16
17/// Convert a string literal into a String
18#[inline]
19pub fn string(value: &'static str) -> String {
20    value.to_string()
21}
22
23/// Convert a value into a string
24#[macro_export]
25macro_rules! string {
26    ( $value:expr ) => {
27        $crate::macros::string($value)
28    };
29}
30
31/// Print a message to stderr with an "debug : " prefix
32///
33/// Parameters:
34/// - `fmt`: A format string.
35/// - `args*`: Format string arguments.
36#[macro_export]
37macro_rules! debug {
38    ( $fmt:expr $(, $args:expr )* ) => (
39        $crate::macros::debug(format_args!($fmt, $( $args ),*))
40    );
41}
42
43/// Print a message to stderr with a "error: " prefix and terminate the process
44///
45/// Parameters:
46/// - `fmt`: A format string.
47/// - `args*`: Format string arguments.
48#[macro_export]
49macro_rules! error {
50    ( $fmt:expr $(, $args:expr )* ) => {
51        $crate::macros::error(format_args!($fmt, $( $args ),*));
52        std::process::exit(1);
53    }
54}
55
56/// Implement std::display::Display with a custom format
57/// Parameters:
58/// - `struct_name`: The struct to extend.
59/// - `format`: The format string to use.
60#[macro_export]
61macro_rules! impl_display_fmt {
62    ($struct_name:ident, $format:expr) => {
63        impl std::fmt::Display for $struct_name {
64            fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
65                return write!(formatter, $format, self);
66            }
67        }
68    };
69}
70
71/// Implement std::display::Display with a pretty-print format
72/// Parameters:
73/// - `struct_name`: The struct to extend.
74#[macro_export]
75macro_rules! impl_display {
76    ($x:ident) => {
77        impl_display_fmt!($x, "{:#?}");
78    };
79}
80
81/// Implement std::display::Display with a brief debug format
82/// Parameters:
83/// - `struct_name`: The struct to extend.
84#[macro_export]
85macro_rules! impl_display_brief {
86    ($x:ident) => {
87        impl_display_fmt!($x, "{:?}");
88    };
89}