Skip to main content

actions_rs/
macros.rs

1//! Ergonomic `format!`-style macros for the most common log calls.
2//!
3//! These are thin wrappers over [`crate::log`]; the functions remain available
4//! for composition and testing. They are re-exported both at the crate root and
5//! from [`crate::log`], so call them as either `actions_rs::warning!(...)` or
6//! `actions_rs::log::warning!(...)`.
7
8/// `debug!("x = {x}")` → [`crate::log::debug()`] with `format!` arguments.
9///
10/// # Examples
11///
12/// ```
13/// let key = "v2-linux";
14/// actions_rs::debug!("cache key = {key}");
15/// ```
16#[macro_export]
17macro_rules! debug {
18    ($($arg:tt)*) => { $crate::log::debug(::std::format!($($arg)*)) };
19}
20
21/// `info!("...")` → [`crate::log::info()`] with `format!` arguments.
22///
23/// # Examples
24///
25/// ```
26/// let n = 3;
27/// actions_rs::info!("processed {n} files");
28/// ```
29#[macro_export]
30macro_rules! info {
31    ($($arg:tt)*) => { $crate::log::info(::std::format!($($arg)*)) };
32}
33
34/// `notice!("...")` → [`crate::log::notice()`] with `format!` arguments.
35///
36/// # Examples
37///
38/// ```
39/// actions_rs::notice!("released v{}.{}", 1, 2);
40/// ```
41#[macro_export]
42macro_rules! notice {
43    ($($arg:tt)*) => { $crate::log::notice(::std::format!($($arg)*)) };
44}
45
46/// `warning!("...")` → [`crate::log::warning()`] with `format!` arguments.
47///
48/// # Examples
49///
50/// ```
51/// let pct = 92;
52/// actions_rs::warning!("disk {pct}% full");
53/// ```
54#[macro_export]
55macro_rules! warning {
56    ($($arg:tt)*) => { $crate::log::warning(::std::format!($($arg)*)) };
57}
58
59/// `error!("...")` → [`crate::log::error()`] with `format!` arguments.
60///
61/// # Examples
62///
63/// ```
64/// let path = "Cargo.toml";
65/// actions_rs::error!("{path}: missing `version` field");
66/// ```
67#[macro_export]
68macro_rules! error {
69    ($($arg:tt)*) => { $crate::log::error(::std::format!($($arg)*)) };
70}
71
72/// `group!("name", { ... })` runs the block inside a collapsible group that is
73/// closed even on panic. Evaluates to the block's value.
74///
75/// # Examples
76///
77/// ```
78/// let answer = actions_rs::group!("compute", { 6 * 7 });
79/// assert_eq!(answer, 42);
80/// ```
81#[macro_export]
82macro_rules! group {
83    ($name:expr, $body:block) => {
84        $crate::log::group($name, || $body)
85    };
86}
87
88// `#[macro_export]` publishes the macros at the crate root (e.g.
89// `actions_rs::group!`). These re-exports additionally give them a path inside
90// this module (`crate::macros::group`, …) so they can be surfaced from
91// [`crate::log`] next to the functions they wrap — `crate::macros::error`
92// resolves to the macro alone, sidestepping the `crate::error` module.
93pub use {debug, error, group, info, notice, warning};
94
95#[cfg(test)]
96mod tests {
97    #[test]
98    fn group_macro_returns_value() {
99        let n = group!("compute", { 6 * 7 });
100        assert_eq!(n, 42);
101    }
102
103    #[test]
104    fn log_macros_format_without_panicking() {
105        let x = 3;
106        debug!("debug {x}");
107        info!("info {}", x);
108        notice!("notice");
109        warning!("warn {x}");
110        error!("err {x}");
111    }
112}