syncable_cli/analyzer/kubelint/formatter/
mod.rs

1//! Output formatters for lint results.
2
3pub mod json;
4pub mod plain;
5pub mod sarif;
6
7use crate::analyzer::kubelint::lint::LintResult;
8
9/// Output format options.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
11pub enum OutputFormat {
12    /// Plain text output.
13    #[default]
14    Plain,
15    /// JSON output.
16    Json,
17    /// SARIF format for IDE integration.
18    Sarif,
19    /// GitHub Actions annotations.
20    GitHub,
21}
22
23impl OutputFormat {
24    /// Parse from a string.
25    pub fn parse(s: &str) -> Option<Self> {
26        match s.to_lowercase().as_str() {
27            "plain" | "text" => Some(Self::Plain),
28            "json" => Some(Self::Json),
29            "sarif" => Some(Self::Sarif),
30            "github" | "github-actions" => Some(Self::GitHub),
31            _ => None,
32        }
33    }
34}
35
36/// Format a lint result to a string.
37pub fn format_result_to_string(result: &LintResult, format: OutputFormat) -> String {
38    match format {
39        OutputFormat::Plain => plain::format(result),
40        OutputFormat::Json => json::format(result),
41        OutputFormat::Sarif => sarif::format(result),
42        OutputFormat::GitHub => plain::format_github(result),
43    }
44}
45
46/// Format and print a lint result.
47pub fn format_result(result: &LintResult, format: OutputFormat) {
48    print!("{}", format_result_to_string(result, format));
49}