github_actions/
macros.rs

1#[macro_export]
2macro_rules! info {
3    ($value:expr) => {
4        println!("{}", $value);
5    };
6    ($($arg:tt)*) => {
7        $crate::info!(format!($($arg)*));
8    };
9}
10
11#[macro_export]
12macro_rules! issue_command {
13    ($command:ident, $value:expr) => {
14        println!(
15            "{}",
16            $crate::Command {
17                command: stringify!($command),
18                value: $value.as_ref() as &str,
19                properties: None,
20            }
21        );
22    };
23    ($command:ident, $value:expr, $($key:ident: $val:expr),+) => {
24        println!("{}", $crate::Command {
25            command: stringify!($command),
26            value: $value.as_ref() as &str,
27            properties: Some([$((stringify!($key), $val.as_ref() as &str)),+].into()),
28        });
29    };
30}
31
32#[macro_export]
33macro_rules! issue_command_with_properties {
34    ($command:ident, $value:expr, $key1:ident: $val1:expr, $key2:ident: $val2:expr, $key3:ident: $val3:expr, $key4:ident: $val4:expr, $key5:ident: $val5:expr, $key6:ident: $val6:expr) => {
35        println!("{}", $crate::CommandWithProperties {
36            command: stringify!($command),
37            value: $value.as_ref() as &str,
38            $key1: Some($val1),
39            $key2: Some($val2),
40            $key3: Some($val3),
41            $key4: Some($val4),
42            $key5: Some($val5),
43            $key6: Some($val6),
44        });
45    };
46    ($command:ident, $value:expr, $($key:ident: $val:expr),*) => {
47        println!("{}", $crate::CommandWithProperties {
48            command: stringify!($command),
49            value: $value.as_ref() as &str,
50            $($key: Some($val)),+,
51            ..Default::default()
52        });
53    };
54}
55
56#[macro_export]
57macro_rules! debug {
58    ($value:expr) => {
59        issue_command!(debug, $value);
60    };
61    ($($arg:tt)*) => {
62        $crate::debug!(format!($($arg)*));
63    };
64}
65
66#[macro_export]
67macro_rules! error {
68    ($value:expr) => {
69        issue_command!(error, $value);
70    };
71    ($value:expr, $($key:ident: $val:expr),+) => {
72        issue_command_with_properties!(error, $value, $($key: $val),+);
73    };
74}
75
76#[macro_export]
77macro_rules! warn {
78    ($value:expr) => {
79        issue_command!(warning, $value);
80    };
81    ($value:expr, $($key:ident: $val:expr),+) => {
82        issue_command_with_properties!(warning, $value, $($key: $val),+);
83    };
84}
85
86#[macro_export]
87macro_rules! notice {
88    ($value:expr) => {
89        issue_command!(notice, $value);
90    };
91    ($value:expr, $($key:ident: $val:expr),+) => {
92        issue_command_with_properties!(notice, $value, $($key: $val),+);
93    };
94}
95
96#[macro_export]
97macro_rules! group {
98    ($title:expr) => {
99        let __group = $crate::Group::new($title.as_ref() as &str);
100        let __group_ender = $crate::GroupEnder::new(&__group);
101    };
102    ($($arg:tt)*) => {
103        $crate::group!(format!($($arg)*));
104    };
105}
106
107#[macro_export]
108macro_rules! stop_commands {
109    () => {
110        let __stop_commands = $crate::StopCommands::gen();
111        let __stop_commands_ender = $crate::StopCommandsEnder::new(&__stop_commands);
112    };
113    ($token:expr) => {
114        let __stop_commands = $crate::StopCommands::new($token.as_ref() as &str);
115        let __stop_commands_ender = $crate::StopCommandsEnder::new(&__stop_commands);
116    };
117    ($($arg:tt)*) => {
118        $crate::stop_commands!(format!($($arg)*));
119    };
120}
121
122#[cfg(test)]
123mod tests {
124    #[test]
125    fn test_commands() {
126        issue_command!(cmd, format!("val"), k: format!("prop"));
127        issue_command!(cmd, "val", k: "prop");
128        info!("message");
129        debug!("message");
130        debug!("message {}", "debug");
131        error!("message");
132        error!("message", title: "title", file: "file.rs");
133        error!("message", title: "title", file: "file", col: 1, end_column: 1, line: 1);
134        error!("message", title: "title", file: "file", col: 1, end_column: 1, line: 1, end_line: 1);
135        warn!("message");
136        warn!("message", title: "title");
137        warn!("message", title: "title", file: "file", col: 1, end_column: 1, line: 1, end_line: 1);
138        notice!("message");
139        notice!("message", title: "title");
140        notice!("message", title: "title", file: "file", col: 1, end_column: 1, line: 1, end_line: 1);
141    }
142
143    #[test]
144    fn test_group() {
145        {
146            group!("group {}", 1);
147        }
148        {
149            group!(format!("group {}", 2));
150            println!("log");
151        }
152        group!("group last");
153        println!("log");
154    }
155
156    #[test]
157    fn test_stop_commands() {
158        {
159            let token = format!("token{}", 1);
160            stop_commands!(token);
161            println!("log");
162        }
163        {
164            stop_commands!();
165            println!("log");
166        }
167        stop_commands!("token");
168        println!("log");
169    }
170}