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
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!("=> Column {} to {},", location.0, location.1);
        }

        if !query.is_empty() {
            println!("  |");
            println!("1 | {}", query);
            if let Some(location) = diagnostic.location() {
                print!("  | ");
                print!("{}", &"-".repeat(location.0));
                self.stdout.set_color(Some(Color::Yellow));
                println!("{}", &"^".repeat(usize::max(1, location.1 - location.0)));
                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();
    }
}