macrotest/
message.rs

1use diff::Result;
2
3/// Prints the difference of the two snippets of expanded code.
4pub(crate) fn message_different(name: &str, a: &[u8], b: &[u8]) {
5    let a = String::from_utf8_lossy(a);
6    let b = String::from_utf8_lossy(b);
7
8    let changes = diff::lines(&a, &b);
9
10    let mut lines_added = 0;
11    let mut lines_removed = 0;
12    for diff in &changes {
13        match diff {
14            Result::Left(_) => lines_added += 1,
15            Result::Right(_) => lines_removed += 1,
16            _ => (),
17        }
18    }
19
20    eprintln!("{} - different!", name);
21
22    eprintln!(
23        "Diff [lines: {} added, {} removed]:",
24        lines_added, lines_removed
25    );
26    eprintln!("--------------------------");
27
28    for change in changes {
29        match change {
30            Result::Both(x, _) => {
31                eprintln!(" {}", x);
32            }
33            Result::Left(x) => {
34                eprintln!("+{}", x);
35            }
36            Result::Right(x) => {
37                eprintln!("-{}", x);
38            }
39        }
40    }
41
42    eprintln!("--------------------------");
43}
44
45/// Prints an error from `cargo expand` invocation.
46/// Makes some suggestions when possible.
47pub(crate) fn message_expansion_error(msg: Vec<u8>) {
48    let msg = String::from_utf8(msg);
49
50    eprintln!("Expansion error:");
51    if let Ok(msg) = msg {
52        eprintln!("{}", msg);
53
54        // No `cargo expand` subcommand installed, make a suggestion
55        if msg.contains("no such subcommand: `expand`") {
56            eprintln!("Perhaps, `cargo expand` is not installed?");
57            eprintln!("Install it by running:");
58            eprintln!();
59            eprintln!("\tcargo install cargo-expand");
60            eprintln!();
61        }
62    } else {
63        eprintln!("<unprintable>");
64    }
65}