syncable_cli/analyzer/helmlint/formatter/
mod.rs1pub mod github;
9pub mod json;
10pub mod stylish;
11
12use crate::analyzer::helmlint::lint::LintResult;
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
16pub enum OutputFormat {
17 Json,
19 #[default]
21 Stylish,
22 Github,
24 Compact,
26}
27
28impl OutputFormat {
29 pub fn parse(s: &str) -> Option<Self> {
31 match s.to_lowercase().as_str() {
32 "json" => Some(Self::Json),
33 "stylish" | "default" => Some(Self::Stylish),
34 "github" | "github-actions" => Some(Self::Github),
35 "compact" => Some(Self::Compact),
36 _ => None,
37 }
38 }
39}
40
41pub fn format_result(result: &LintResult, format: OutputFormat) {
43 let output = format_result_to_string(result, format);
44 println!("{}", output);
45}
46
47pub fn format_result_to_string(result: &LintResult, format: OutputFormat) -> String {
49 match format {
50 OutputFormat::Json => json::format(result),
51 OutputFormat::Stylish => stylish::format(result),
52 OutputFormat::Github => github::format(result),
53 OutputFormat::Compact => compact_format(result),
54 }
55}
56
57pub fn format_results(results: &[LintResult], format: OutputFormat) -> String {
59 match format {
60 OutputFormat::Json => {
61 let jsons: Vec<String> = results.iter().map(json::format).collect();
63 format!("[{}]", jsons.join(","))
64 }
65 _ => results
66 .iter()
67 .map(|r| format_result_to_string(r, format))
68 .collect::<Vec<_>>()
69 .join("\n"),
70 }
71}
72
73fn compact_format(result: &LintResult) -> String {
75 let mut lines = Vec::new();
76
77 for failure in &result.failures {
78 lines.push(format!(
79 "{}:{}:{}: {} {}",
80 failure.file.display(),
81 failure.line,
82 failure.column.unwrap_or(1),
83 failure.code,
84 failure.message
85 ));
86 }
87
88 if lines.is_empty() {
89 format!("{}: No issues found", result.chart_path)
90 } else {
91 lines.join("\n")
92 }
93}
94
95#[cfg(test)]
96mod tests {
97 use super::*;
98
99 #[test]
100 fn test_output_format_parse() {
101 assert_eq!(OutputFormat::parse("json"), Some(OutputFormat::Json));
102 assert_eq!(OutputFormat::parse("stylish"), Some(OutputFormat::Stylish));
103 assert_eq!(OutputFormat::parse("github"), Some(OutputFormat::Github));
104 assert_eq!(OutputFormat::parse("compact"), Some(OutputFormat::Compact));
105 assert_eq!(OutputFormat::parse("invalid"), None);
106 }
107}