mod checkstyle;
mod codeclimate;
mod gnu;
mod json;
mod sarif;
mod tty;
pub use checkstyle::CheckstyleFormatter;
pub use codeclimate::CodeClimateFormatter;
pub use gnu::GnuFormatter;
pub use json::JsonFormatter;
pub use sarif::SarifFormatter;
pub use tty::TtyFormatter;
use crate::analyzer::hadolint::lint::LintResult;
use std::io::Write;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum OutputFormat {
#[default]
Tty,
Json,
Sarif,
Checkstyle,
CodeClimate,
Gnu,
}
impl OutputFormat {
pub fn parse(s: &str) -> Option<Self> {
match s.to_lowercase().as_str() {
"tty" | "terminal" | "color" => Some(Self::Tty),
"json" => Some(Self::Json),
"sarif" => Some(Self::Sarif),
"checkstyle" => Some(Self::Checkstyle),
"codeclimate" | "gitlab" => Some(Self::CodeClimate),
"gnu" => Some(Self::Gnu),
_ => None,
}
}
pub fn all_names() -> &'static [&'static str] {
&["tty", "json", "sarif", "checkstyle", "codeclimate", "gnu"]
}
}
pub trait Formatter {
fn format<W: Write>(
&self,
result: &LintResult,
filename: &str,
writer: &mut W,
) -> std::io::Result<()>;
fn format_to_string(&self, result: &LintResult, filename: &str) -> String {
let mut buf = Vec::new();
self.format(result, filename, &mut buf).unwrap_or_default();
String::from_utf8(buf).unwrap_or_default()
}
}
pub fn format_result<W: Write>(
result: &LintResult,
filename: &str,
format: OutputFormat,
writer: &mut W,
) -> std::io::Result<()> {
match format {
OutputFormat::Tty => TtyFormatter::new().format(result, filename, writer),
OutputFormat::Json => JsonFormatter::new().format(result, filename, writer),
OutputFormat::Sarif => SarifFormatter::new().format(result, filename, writer),
OutputFormat::Checkstyle => CheckstyleFormatter::new().format(result, filename, writer),
OutputFormat::CodeClimate => CodeClimateFormatter::new().format(result, filename, writer),
OutputFormat::Gnu => GnuFormatter::new().format(result, filename, writer),
}
}
pub fn format_result_to_string(
result: &LintResult,
filename: &str,
format: OutputFormat,
) -> String {
let mut buf = Vec::new();
format_result(result, filename, format, &mut buf).unwrap_or_default();
String::from_utf8(buf).unwrap_or_default()
}