lighty_core/
macros.rs

1#[macro_export]
2macro_rules! join_and_mkdir {
3    ($path:expr, $join:expr) => {{
4        let path = $path.join($join);
5        $crate::mkdir!(&path);
6        path
7    }};
8}
9
10#[macro_export]
11macro_rules! join_and_mkdir_vec {
12    ($path:expr, $joins:expr) => {{
13        let mut path = $path.to_path_buf();
14        for join in $joins {
15            path = path.join(join);
16            $crate::mkdir!(&path);
17        }
18        path
19    }};
20}
21
22#[macro_export]
23macro_rules! mkdir {
24    ($path:expr) => {
25        if !$path.exists() {
26            if let Err(e) = tokio::fs::create_dir_all(&$path).await {
27                $crate::trace_error!("Failed to create directory {:?}: {}", $path, e);
28            }
29        }
30    };
31}
32
33// Blocking version for sync contexts (if needed)
34#[macro_export]
35macro_rules! mkdir_blocking {
36    ($path:expr) => {
37        if !$path.exists() {
38            let path = $path.to_path_buf();
39            tokio::task::spawn_blocking(move || {
40                std::fs::create_dir_all(&path)
41            }).await.ok();
42        }
43    };
44}
45
46
47#[cfg(feature = "tracing")]
48#[macro_export]
49macro_rules! time_it {
50    ($label:expr, $expr:expr) => {{
51        let start = std::time::Instant::now();
52        let result = $expr;
53        let elapsed = start.elapsed();
54        ::tracing::debug!(label = $label, elapsed = ?elapsed, "Operation completed");
55        result
56    }};
57}
58
59#[cfg(not(feature = "tracing"))]
60#[macro_export]
61macro_rules! time_it {
62    ($label:expr, $expr:expr) => {{
63        $expr
64    }};
65}
66
67// Conditional tracing macros
68#[cfg(feature = "tracing")]
69#[macro_export]
70macro_rules! trace_debug {
71    ($($arg:tt)*) => {
72        ::tracing::debug!($($arg)*)
73    };
74}
75
76#[cfg(not(feature = "tracing"))]
77#[macro_export]
78macro_rules! trace_debug {
79    ($($arg:tt)*) => {{}};
80}
81
82#[cfg(feature = "tracing")]
83#[macro_export]
84macro_rules! trace_info {
85    ($($arg:tt)*) => {
86        ::tracing::info!($($arg)*)
87    };
88}
89
90#[cfg(not(feature = "tracing"))]
91#[macro_export]
92macro_rules! trace_info {
93    ($($arg:tt)*) => {{}};
94}
95
96#[cfg(feature = "tracing")]
97#[macro_export]
98macro_rules! trace_warn {
99    ($($arg:tt)*) => {
100        ::tracing::warn!($($arg)*)
101    };
102}
103
104#[cfg(not(feature = "tracing"))]
105#[macro_export]
106macro_rules! trace_warn {
107    ($($arg:tt)*) => {{}};
108}
109
110#[cfg(feature = "tracing")]
111#[macro_export]
112macro_rules! trace_error {
113    ($($arg:tt)*) => {
114        ::tracing::error!($($arg)*)
115    };
116}
117
118#[cfg(not(feature = "tracing"))]
119#[macro_export]
120macro_rules! trace_error {
121    ($($arg:tt)*) => {{}};
122}