ktrs_cli/ktlint/reporter/
checkstyle.rs1use crate::ktlint::console::Printer;
4use crate::ktlint::reporter::{KtlintCliError, ReporterV2, Status, accumulate, escape_xml_attr_value, sorted_by_file};
5
6pub struct CheckStyleReporter {
7 out: Printer,
8 acc: Vec<(String, Vec<KtlintCliError>)>,
9}
10
11impl CheckStyleReporter {
12 pub fn new(out: Printer) -> CheckStyleReporter {
13 CheckStyleReporter { out, acc: Vec::new() }
14 }
15}
16
17impl ReporterV2 for CheckStyleReporter {
18 fn on_lint_error(&mut self, file: &str, ktlint_cli_error: &KtlintCliError) {
19 if ktlint_cli_error.status != Status::FormatIsAutocorrected {
20 accumulate(&mut self.acc, file, ktlint_cli_error);
21 }
22 }
23
24 fn after_all(&mut self) {
25 let out = &mut self.out;
26 out.println(r#"<?xml version="1.0" encoding="utf-8"?>"#);
27 out.println(r#"<checkstyle version="8.0">"#);
28 for (file, err_list) in sorted_by_file(&self.acc) {
29 out.println(&format!(r#" <file name="{}">"#, escape_xml_attr_value(file)));
30 for err in err_list {
31 let message = escape_xml_attr_value(&err.detail);
32 out.println(&format!(
33 r#" <error line="{}" column="{}" severity="error" message="{message}" source="{}" />"#,
34 err.line, err.col, err.rule_id
35 ));
36 }
37 out.println(" </file>");
38 }
39 out.println("</checkstyle>");
40 }
41}