macrotest/
message.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use diff::Result;

/// Prints the difference of the two snippets of expanded code.
pub(crate) fn message_different(name: &str, a: &[u8], b: &[u8]) {
    let a = String::from_utf8_lossy(&a);
    let b = String::from_utf8_lossy(&b);

    let changes = diff::lines(&a, &b);

    let mut lines_added = 0;
    let mut lines_removed = 0;
    for diff in &changes {
        match diff {
            Result::Left(_) => lines_added += 1,
            Result::Right(_) => lines_removed += 1,
            _ => (),
        }
    }

    eprintln!("{} - different!", name);

    eprintln!(
        "Diff [lines: {} added, {} removed]:",
        lines_added, lines_removed
    );
    eprintln!("--------------------------");

    for change in changes {
        match change {
            Result::Both(x, _) => {
                eprintln!(" {}", x);
            }
            Result::Left(x) => {
                eprintln!("+{}", x);
            }
            Result::Right(x) => {
                eprintln!("-{}", x);
            }
        }
    }

    eprintln!("--------------------------");
}

/// Prints an error from `cargo expand` invocation.
/// Makes some suggestions when possible.
pub(crate) fn message_expansion_error(msg: Vec<u8>) {
    let msg = String::from_utf8(msg);

    eprintln!("Expansion error:");
    if let Ok(msg) = msg {
        eprintln!("{}", msg);

        // No `cargo expand` subcommand installed, make a suggestion
        if msg.contains("no such subcommand: `expand`") {
            eprintln!("Perhaps, `cargo expand` is not installed?");
            eprintln!("Install it by running:");
            eprintln!();
            eprintln!("\tcargo install cargo-expand");
            eprintln!();
        }
    } else {
        eprintln!("<unprintable>");
    }
}