gitql_cli/
diagnostic_reporter.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 gitql_parser::diagnostic::Diagnostic;

use termcolor::Color;

use crate::colored_stream::ColoredStream;

#[derive(Default)]
pub struct DiagnosticReporter {
    stdout: ColoredStream,
}

impl DiagnosticReporter {
    pub fn report_diagnostic(&mut self, query: &str, diagnostic: Diagnostic) {
        self.stdout.set_color(Some(Color::Red));
        println!("[{}]: {}", diagnostic.label(), diagnostic.message());

        if let Some(location) = diagnostic.location() {
            println!(
                "  --> Location {}:{}",
                location.line_start, location.column_start
            );
        }

        if !query.is_empty() {
            println!("   |");
            if let Some(location) = diagnostic.location() {
                let lines: Vec<&str> = query.split('\n').collect();
                let end = u32::min(location.line_end, lines.len() as u32);
                for zero_based_line_number in location.line_start - 1..end {
                    println!("-> | {}", lines[zero_based_line_number as usize]);
                }

                println!("   | ");
                let column_s = location.column_start.saturating_sub(1) as usize;
                print!("   | {}", &"-".repeat(column_s));

                let diagnostic_length =
                    u32::max(1, location.column_end.saturating_sub(location.column_start)) as usize;

                self.stdout.set_color(Some(Color::Yellow));
                println!("{}", &"^".repeat(diagnostic_length));

                self.stdout.set_color(Some(Color::Red));
            }
            println!("   |");
        }

        self.stdout.set_color(Some(Color::Yellow));
        for note in diagnostic.notes() {
            println!(" = Note: {}", note);
        }

        self.stdout.set_color(Some(Color::Cyan));
        for help in diagnostic.helps() {
            println!(" = Help: {}", help);
        }

        self.stdout.set_color(Some(Color::Blue));
        if let Some(docs) = diagnostic.docs() {
            println!(" = Docs: {}", docs);
        }

        self.stdout.reset();
    }
}