use serde_json::Value;
use crate::analysis::findings::{Finding, Severity};
use crate::languages::runner::ToolOutputError;
use crate::languages::spec::ToolSpec;
use super::{expect_array, json_payload, path_or_root};
pub(super) fn parse_shellcheck(
spec: &ToolSpec,
output: &str,
root_name: &str,
) -> Result<Vec<Finding>, ToolOutputError> {
let Some(payload) = json_payload(spec, output)? else {
return Ok(Vec::new());
};
let entries = expect_array(spec, &payload)?;
let mut findings = Vec::new();
for entry in entries {
let code = entry.get("code").and_then(Value::as_u64);
findings.push(Finding::deterministic(
code.map(|n| format!("SC{n}"))
.unwrap_or_else(|| spec.name.to_owned()),
shellcheck_severity(entry.get("level").and_then(Value::as_str)),
path_or_root(entry.get("file"), root_name),
entry
.get("line")
.and_then(Value::as_u64)
.map(|n| n as u32)
.unwrap_or(1),
entry
.get("column")
.and_then(Value::as_u64)
.map(|n| n as u32),
entry
.get("message")
.and_then(Value::as_str)
.unwrap_or("")
.to_owned(),
None,
));
}
Ok(findings)
}
fn shellcheck_severity(level: Option<&str>) -> Severity {
match level {
Some("error") => Severity::Error,
Some("info") | Some("style") => Severity::Info,
_ => Severity::Warning,
}
}