sqruff_lib/cli/
json.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
use std::sync::Mutex;

use crate::core::{config::FluffConfig, linter::linted_file::LintedFile};

use super::{
    formatters::Formatter,
    json_types::{Diagnostic, DiagnosticCollection, DiagnosticSeverity},
};

#[derive(Default)]
pub struct JsonFormatter {
    violations: Mutex<DiagnosticCollection>,
}

impl Formatter for JsonFormatter {
    fn dispatch_file_violations(&self, linted_file: &LintedFile, only_fixable: bool) {
        let violations = linted_file.get_violations(only_fixable.then_some(true));
        let mut lock = self.violations.lock().unwrap();
        lock.entry(linted_file.path.clone()).or_default().extend(
            violations
                .iter()
                .map(|err| Diagnostic::from(err.clone()))
                .collect::<Vec<_>>(),
        );
    }

    fn has_fail(&self) -> bool {
        let lock = self.violations.lock().unwrap();
        lock.values().any(|v| {
            v.iter()
                .any(|d| matches!(&d.severity, DiagnosticSeverity::Error))
        })
    }

    fn completion_message(&self) {
        let lock = self.violations.lock().unwrap();
        let json = serde_json::to_string(&*lock).unwrap();
        println!("{}", json);
    }

    fn dispatch_template_header(
        &self,
        _f_name: String,
        _linter_config: FluffConfig,
        _file_config: FluffConfig,
    ) {
    }

    fn dispatch_parse_header(&self, _f_name: String) {}
}