cargo_emit/warning.rs
1/// Tells Cargo to print the formatted `warning` message.
2///
3/// This is equivalent to:
4///
5/// ```
6/// println!("cargo:warning=$args");
7/// ```
8///
9/// # Examples
10///
11/// Useful for showing when something expected (but not critical) has failed.
12///
13/// ```
14/// match std::env::current_dir() {
15/// Ok(dir) => { /* ... */ }
16/// Err(error) => cargo_emit::warning!(
17/// "Something suspicious is happening: {}",
18/// error,
19/// ),
20/// }
21/// ```
22///
23/// or, in case you want it to emit to a custom stream:
24///
25/// ```
26/// let mut stdout = std::io::stdout();
27/// match std::env::current_dir() {
28/// Ok(dir) => { /* ... */ }
29/// Err(error) => cargo_emit::warning!(
30/// to: stdout,
31/// "Something suspicious is happening: {}",
32/// error,
33/// ),
34/// }
35/// ```
36///
37/// Assuming you're building `my-crate`, you will see:
38///
39/// ```sh
40/// $ cargo build
41/// Compiling my-crate v0.1.0 (/path/to/my-crate)
42/// warning: Something suspicious is happening: ...
43/// ```
44#[macro_export]
45macro_rules! warning {
46 (to: $stream:expr, $($args:tt)+) => {
47 $crate::pair!(to: $stream, "warning", $($args)+)
48 };
49 ($($args:tt)+) => {
50 $crate::warning!(to: std::io::stdout(), $($args)+)
51 };
52}
53
54#[cfg(test)]
55mod tests {
56 #[test]
57 fn single_literal() {
58 insta::assert_display_snapshot!(
59 crate::capture_output(|output| {
60 crate::warning!(
61 to: output,
62 "WARNING"
63 );
64 }),
65 @"cargo:warning=WARNING\n"
66 );
67 }
68
69 #[test]
70 fn single_formatted_by_index() {
71 // Formatted argument:
72 insta::assert_display_snapshot!(
73 crate::capture_output(|output| {
74 crate::warning!(
75 to: output,
76 "{}", "WARNING"
77 );
78 }),
79 @"cargo:warning=WARNING\n"
80 );
81 }
82
83 #[test]
84 fn single_formatted_by_key() {
85 // Formatted argument:
86 insta::assert_display_snapshot!(
87 crate::capture_output(|output| {
88 crate::warning!(
89 to: output,
90 "{warning}", warning = "WARNING"
91 );
92 }),
93 @"cargo:warning=WARNING\n"
94 );
95 }
96}