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
use gitql_parser::diagnostic::GQLError;

use termcolor::Color;

use crate::colored_stream::ColoredStream;

const PORPOT_LENGTH: usize = 6;

pub struct DiagnosticReporter {
    stdout: ColoredStream,
}

impl DiagnosticReporter {
    pub fn new() -> Self {
        Self {
            stdout: ColoredStream::new(),
        }
    }

    pub fn report_error(&mut self, message: &str) {
        self.stdout.set_color(Some(Color::Red));
        self.stdout.print("ERROR: ");
        self.stdout.println(message);
        self.stdout.reset();
    }

    pub fn report_gql_error(&mut self, error: GQLError) {
        self.stdout.set_color(Some(Color::Red));

        let start = error.location.start;
        self.stdout.print(&"-".repeat(PORPOT_LENGTH + start));
        self.stdout.println("^");

        self.stdout.print("Compiletime ERROR: ");

        let end = error.location.end;
        let message = error.message;
        self.stdout.print("[");
        self.stdout.print(&start.to_string());
        self.stdout.print(" - ");
        self.stdout.print(&end.to_string());
        self.stdout.print("] -> ");
        self.stdout.println(&message);

        self.stdout.reset();
    }

    pub fn report_runtime_error(&mut self, message: String) {
        self.stdout.set_color(Some(Color::Red));
        self.stdout.print("RUNTIME EXCEPTION: ");
        self.stdout.println(&message);

        self.stdout.reset();
    }
}