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. Exported at the crate root, so call them as
5//! `actions_rs::warning!(...)`.
6
7/// `debug!("x = {x}")` → [`crate::log::debug`] with `format!` arguments.
8///
9/// # Examples
10///
11/// ```
12/// let key = "v2-linux";
13/// actions_rs::debug!("cache key = {key}");
14/// ```
15#[macro_export]
16macro_rules! debug {
17    ($($arg:tt)*) => { $crate::log::debug(::std::format!($($arg)*)) };
18}
19
20/// `info!("...")` → [`crate::log::info`] with `format!` arguments.
21///
22/// # Examples
23///
24/// ```
25/// let n = 3;
26/// actions_rs::info!("processed {n} files");
27/// ```
28#[macro_export]
29macro_rules! info {
30    ($($arg:tt)*) => { $crate::log::info(::std::format!($($arg)*)) };
31}
32
33/// `notice!("...")` → [`crate::log::notice`] with `format!` arguments.
34///
35/// # Examples
36///
37/// ```
38/// actions_rs::notice!("released v{}.{}", 1, 2);
39/// ```
40#[macro_export]
41macro_rules! notice {
42    ($($arg:tt)*) => { $crate::log::notice(::std::format!($($arg)*)) };
43}
44
45/// `warning!("...")` → [`crate::log::warning`] with `format!` arguments.
46///
47/// # Examples
48///
49/// ```
50/// let pct = 92;
51/// actions_rs::warning!("disk {pct}% full");
52/// ```
53#[macro_export]
54macro_rules! warning {
55    ($($arg:tt)*) => { $crate::log::warning(::std::format!($($arg)*)) };
56}
57
58/// `error!("...")` → [`crate::log::error`] with `format!` arguments.
59///
60/// # Examples
61///
62/// ```
63/// let path = "Cargo.toml";
64/// actions_rs::error!("{path}: missing `version` field");
65/// ```
66#[macro_export]
67macro_rules! error {
68    ($($arg:tt)*) => { $crate::log::error(::std::format!($($arg)*)) };
69}
70
71/// `group!("name", { ... })` runs the block inside a collapsible group that is
72/// closed even on panic. Evaluates to the block's value.
73///
74/// # Examples
75///
76/// ```
77/// let answer = actions_rs::group!("compute", { 6 * 7 });
78/// assert_eq!(answer, 42);
79/// ```
80#[macro_export]
81macro_rules! group {
82    ($name:expr, $body:block) => {
83        $crate::log::group($name, || $body)
84    };
85}
86
87#[cfg(test)]
88mod tests {
89    #[test]
90    fn group_macro_returns_value() {
91        let n = group!("compute", { 6 * 7 });
92        assert_eq!(n, 42);
93    }
94
95    #[test]
96    fn log_macros_format_without_panicking() {
97        let x = 3;
98        debug!("debug {x}");
99        info!("info {}", x);
100        notice!("notice");
101        warning!("warn {x}");
102        error!("err {x}");
103    }
104}