Skip to main content

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/// Fire-and-forget `create_dir_all` — logs errors via `trace_error!` and
23/// swallows them. Prefer [`try_mkdir!`] when callers need to short-circuit.
24#[macro_export]
25macro_rules! mkdir {
26    ($path:expr) => {
27        if !$path.exists() {
28            if let Err(e) = tokio::fs::create_dir_all(&$path).await {
29                $crate::trace_error!("Failed to create directory {:?}: {}", $path, e);
30            }
31        }
32    };
33}
34
35/// Async `create_dir_all` that propagates the `io::Error` for fail-fast pipelines.
36#[macro_export]
37macro_rules! try_mkdir {
38    ($path:expr) => {{
39        let __path = &$path;
40        if __path.exists() {
41            Ok::<(), std::io::Error>(())
42        } else {
43            tokio::fs::create_dir_all(__path).await
44        }
45    }};
46}
47
48#[macro_export]
49macro_rules! mkdir_blocking {
50    ($path:expr) => {
51        if !$path.exists() {
52            let path = $path.to_path_buf();
53            tokio::task::spawn_blocking(move || {
54                std::fs::create_dir_all(&path)
55            }).await.ok();
56        }
57    };
58}
59
60
61#[cfg(feature = "tracing")]
62#[macro_export]
63macro_rules! time_it {
64    ($label:expr, $expr:expr) => {{
65        let start = std::time::Instant::now();
66        let result = $expr;
67        let elapsed = start.elapsed();
68        ::tracing::debug!(label = $label, elapsed = ?elapsed, "Operation completed");
69        result
70    }};
71}
72
73#[cfg(not(feature = "tracing"))]
74#[macro_export]
75macro_rules! time_it {
76    ($label:expr, $expr:expr) => {{
77        $expr
78    }};
79}
80
81#[cfg(feature = "tracing")]
82#[macro_export]
83macro_rules! trace_debug {
84    ($($arg:tt)*) => {
85        ::tracing::debug!($($arg)*)
86    };
87}
88
89#[cfg(not(feature = "tracing"))]
90#[macro_export]
91macro_rules! trace_debug {
92    ($($arg:tt)*) => {{}};
93}
94
95#[cfg(feature = "tracing")]
96#[macro_export]
97macro_rules! trace_info {
98    ($($arg:tt)*) => {
99        ::tracing::info!($($arg)*)
100    };
101}
102
103#[cfg(not(feature = "tracing"))]
104#[macro_export]
105macro_rules! trace_info {
106    ($($arg:tt)*) => {{}};
107}
108
109#[cfg(feature = "tracing")]
110#[macro_export]
111macro_rules! trace_warn {
112    ($($arg:tt)*) => {
113        ::tracing::warn!($($arg)*)
114    };
115}
116
117#[cfg(not(feature = "tracing"))]
118#[macro_export]
119macro_rules! trace_warn {
120    ($($arg:tt)*) => {{}};
121}
122
123#[cfg(feature = "tracing")]
124#[macro_export]
125macro_rules! trace_error {
126    ($($arg:tt)*) => {
127        ::tracing::error!($($arg)*)
128    };
129}
130
131#[cfg(not(feature = "tracing"))]
132#[macro_export]
133macro_rules! trace_error {
134    ($($arg:tt)*) => {{}};
135}