Skip to main content

tree_table/utils/
utils_macros.rs

1// Create a struct and a new() function
2#[macro_export]
3macro_rules! define_struct_with_new {
4    ($struct_name:ident { $($field:ident: $ty:ty $(= $init:expr)?),* }) => {
5        #[derive(Debug)]
6        pub struct $struct_name {
7            $(pub $field: $ty),*
8        }
9
10        impl $struct_name {
11            pub fn new() -> Self {
12                $struct_name {
13                    $(
14                        $field: {
15                            #[allow(unused_assignments)]
16                            #[allow(unused_mut)]
17                            {
18                                let mut tmp = <$ty>::new();
19                                $(tmp = $init;)?
20                                tmp
21                            }
22                        }
23                    ),*
24                }
25            }
26        }
27    };
28}
29
30// Create a HashMap with string keys and generic values
31#[macro_export]
32macro_rules! hm_str_key {
33    ($( $key:expr => $value:expr ),* $(,)?) => {
34        {
35            let mut map: hashbrown::HashMap<String, _> = hashbrown::HashMap::new();
36            $(
37                map.insert($key.to_string(), $value);
38            )*
39            map
40        }
41    };
42}
43
44// Create a HashMap with string keys and string values
45#[macro_export]
46macro_rules! hm_str {
47    ($( $key:expr => $value:expr ),* $(,)?) => {
48        {
49            let mut map: hashbrown::HashMap<String, String> = hashbrown::HashMap::new();
50            $(
51                map.insert($key.to_string(), $value.to_string());
52            )*
53            map
54        }
55    };
56}
57
58// Create a HashMap with raw key-value pairs (no conversion)
59#[macro_export]
60macro_rules! hm_raw {
61    ($( $key:expr => $value:expr ),* $(,)?) => {
62        {
63            let mut map: hashbrown::HashMap<_, _> = hashbrown::HashMap::new();
64            $(
65                map.insert($key, $value);
66            )*
67            map
68        }
69    };
70}
71
72// log to console
73#[macro_export]
74macro_rules! log {
75    // Single string literal
76    ($lit:literal) => {
77        #[cfg(feature = "std")]
78        ::std::println!($lit);
79    };
80    // Single expression with Debug formatting
81    ($expr:expr) => {
82        #[cfg(feature = "std")]
83        ::std::println!("{:?}", $expr);
84    };
85    // Formatted string with arguments
86    ($fmt:literal, $($arg:expr),*) => {
87        #[cfg(feature = "std")]
88        ::std::println!($fmt, $($arg),*);
89    };
90    // Iterable as a Vec for printing
91    (array: $expr:expr) => {
92        #[cfg(feature = "std")]
93        ::std::println!("{:?}", $expr.collect::<Vec<_>>());
94    };
95    // Flexible logging
96    ($($t:tt)*) => {
97        #[cfg(feature = "std")]
98        ::std::println!($($t)*);
99    };
100}
101
102// Properly formats a String Err. Supports literal strings and format! syntax.
103#[macro_export]
104macro_rules! str_err {
105    ($($arg:tt)*) => {
106        ::alloc::format!("Error at {}:{}: {}", file!(), line!(), ::alloc::format!($($arg)*))
107    };
108}