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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use crate::diagnostic::{Diagnostic, DiagnosticSeverity};
use serde::Serialize;

pub trait AnniLinter<T> {
    fn add(&mut self, msg: Diagnostic<T>);
    fn flush(&self) -> bool;
}

#[derive(Default)]
pub struct AnniLinterReviewDogJsonLineFormat(bool);

impl AnniLinterReviewDogJsonLineFormat {
    pub fn new() -> Self {
        AnniLinterReviewDogJsonLineFormat(false)
    }
}

impl<T: Serialize> AnniLinter<T> for AnniLinterReviewDogJsonLineFormat {
    fn add(&mut self, msg: Diagnostic<T>) {
        if let DiagnosticSeverity::Error = msg.severity {
            self.0 = true;
        }
        println!("{}", serde_json::to_string(&msg).unwrap());
    }

    fn flush(&self) -> bool {
        !self.0
    }
}

pub struct AnniLinterTextFormat<T> {
    errors: Vec<Diagnostic<T>>,
    warnings: Vec<Diagnostic<T>>,
}

impl<T> Default for AnniLinterTextFormat<T> {
    fn default() -> Self {
        Self {
            errors: Vec::new(),
            warnings: Vec::new(),
        }
    }
}

impl<T> AnniLinter<T> for AnniLinterTextFormat<T> {
    fn add(&mut self, msg: Diagnostic<T>) {
        match msg.severity {
            DiagnosticSeverity::Error => self.errors.push(msg),
            DiagnosticSeverity::Warning => self.warnings.push(msg),
            _ => {}
        }
    }

    fn flush(&self) -> bool {
        println!(
            "{} errors, {} warnings",
            self.errors.len(),
            self.warnings.len()
        );
        println!();
        for error in self.errors.iter() {
            println!(
                "[ERROR][{}] {}:{}: {}",
                error.location.path,
                error.location.start_line(),
                error.location.start_column().unwrap_or(0),
                error.message.message
            );
        }
        println!();
        for warn in self.warnings.iter() {
            println!(
                "[WARN][{}] {}:{}: {}",
                warn.location.path,
                warn.location.start_line(),
                warn.location.start_column().unwrap_or(0),
                warn.message.message
            );
        }

        self.errors.is_empty()
    }
}