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},
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 completion_message(&self) {
28        let lock = self.violations.lock().unwrap();
29        let json = serde_json::to_string(&*lock).unwrap();
30        println!("{json}");
31    }
32
33    fn dispatch_template_header(
34        &self,
35        _f_name: String,
36        _linter_config: FluffConfig,
37        _file_config: FluffConfig,
38    ) {
39    }
40
41    fn dispatch_parse_header(&self, _f_name: String) {}
42}