syncable_cli/analyzer/hadolint/formatter/
mod.rs1mod checkstyle;
12mod codeclimate;
13mod gnu;
14mod json;
15mod sarif;
16mod tty;
17
18pub use checkstyle::CheckstyleFormatter;
19pub use codeclimate::CodeClimateFormatter;
20pub use gnu::GnuFormatter;
21pub use json::JsonFormatter;
22pub use sarif::SarifFormatter;
23pub use tty::TtyFormatter;
24
25use crate::analyzer::hadolint::lint::LintResult;
26use std::io::Write;
27
28#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
30pub enum OutputFormat {
31 #[default]
33 Tty,
34 Json,
36 Sarif,
38 Checkstyle,
40 CodeClimate,
42 Gnu,
44}
45
46impl OutputFormat {
47 pub fn parse(s: &str) -> Option<Self> {
49 match s.to_lowercase().as_str() {
50 "tty" | "terminal" | "color" => Some(Self::Tty),
51 "json" => Some(Self::Json),
52 "sarif" => Some(Self::Sarif),
53 "checkstyle" => Some(Self::Checkstyle),
54 "codeclimate" | "gitlab" => Some(Self::CodeClimate),
55 "gnu" => Some(Self::Gnu),
56 _ => None,
57 }
58 }
59
60 pub fn all_names() -> &'static [&'static str] {
62 &["tty", "json", "sarif", "checkstyle", "codeclimate", "gnu"]
63 }
64}
65
66pub trait Formatter {
68 fn format<W: Write>(
70 &self,
71 result: &LintResult,
72 filename: &str,
73 writer: &mut W,
74 ) -> std::io::Result<()>;
75
76 fn format_to_string(&self, result: &LintResult, filename: &str) -> String {
78 let mut buf = Vec::new();
79 self.format(result, filename, &mut buf).unwrap_or_default();
80 String::from_utf8(buf).unwrap_or_default()
81 }
82}
83
84pub fn format_result<W: Write>(
86 result: &LintResult,
87 filename: &str,
88 format: OutputFormat,
89 writer: &mut W,
90) -> std::io::Result<()> {
91 match format {
92 OutputFormat::Tty => TtyFormatter::new().format(result, filename, writer),
93 OutputFormat::Json => JsonFormatter::new().format(result, filename, writer),
94 OutputFormat::Sarif => SarifFormatter::new().format(result, filename, writer),
95 OutputFormat::Checkstyle => CheckstyleFormatter::new().format(result, filename, writer),
96 OutputFormat::CodeClimate => CodeClimateFormatter::new().format(result, filename, writer),
97 OutputFormat::Gnu => GnuFormatter::new().format(result, filename, writer),
98 }
99}
100
101pub fn format_result_to_string(
103 result: &LintResult,
104 filename: &str,
105 format: OutputFormat,
106) -> String {
107 let mut buf = Vec::new();
108 format_result(result, filename, format, &mut buf).unwrap_or_default();
109 String::from_utf8(buf).unwrap_or_default()
110}