sqruff_lib/cli/
json.rs

1use std::sync::Mutex;
2
3use crate::core::{config::FluffConfig, linter::linted_file::LintedFile};
4
5use super::{
6    formatters::Formatter,
7    json_types::{Diagnostic, DiagnosticCollection, DiagnosticSeverity},
8};
9
10#[derive(Default)]
11pub struct JsonFormatter {
12    violations: Mutex<DiagnosticCollection>,
13}
14
15impl Formatter for JsonFormatter {
16    fn dispatch_file_violations(&self, linted_file: &LintedFile, only_fixable: bool) {
17        let violations = linted_file.get_violations(only_fixable.then_some(true));
18        let mut lock = self.violations.lock().unwrap();
19        lock.entry(linted_file.path.clone()).or_default().extend(
20            violations
21                .iter()
22                .map(|err| Diagnostic::from(err.clone()))
23                .collect::<Vec<_>>(),
24        );
25    }
26
27    fn has_fail(&self) -> bool {
28        let lock = self.violations.lock().unwrap();
29        lock.values().any(|v| {
30            v.iter()
31                .any(|d| matches!(&d.severity, DiagnosticSeverity::Error))
32        })
33    }
34
35    fn completion_message(&self) {
36        let lock = self.violations.lock().unwrap();
37        let json = serde_json::to_string(&*lock).unwrap();
38        println!("{}", json);
39    }
40
41    fn dispatch_template_header(
42        &self,
43        _f_name: String,
44        _linter_config: FluffConfig,
45        _file_config: FluffConfig,
46    ) {
47    }
48
49    fn dispatch_parse_header(&self, _f_name: String) {}
50}