use serde_json::Value;
use crate::analysis::findings::{Finding, Severity};
use crate::languages::runner::ToolOutputError;
use crate::languages::spec::ToolSpec;
use super::{expect_keyed_array, json_payload, path_or_root};
pub(super) fn parse_credo(
spec: &ToolSpec,
output: &str,
root_name: &str,
) -> Result<Vec<Finding>, ToolOutputError> {
let Some(payload) = json_payload(spec, output)? else {
return Ok(Vec::new());
};
let issues = expect_keyed_array(spec, &payload, "issues")?;
let mut findings = Vec::new();
for issue in issues {
let column = issue
.get("column")
.and_then(Value::as_u64)
.map(|n| n as u32);
findings.push(Finding::deterministic(
issue
.get("check")
.and_then(Value::as_str)
.map(str::to_owned)
.unwrap_or_else(|| spec.name.to_owned()),
credo_severity(issue.get("category").and_then(Value::as_str)),
path_or_root(issue.get("filename"), root_name),
issue
.get("line_no")
.and_then(Value::as_u64)
.map(|n| n as u32)
.unwrap_or(1),
column,
issue
.get("message")
.and_then(Value::as_str)
.unwrap_or("")
.to_owned(),
None,
));
}
Ok(findings)
}
fn credo_severity(category: Option<&str>) -> Severity {
match category {
Some("warning") => Severity::Error,
Some("readability") | Some("consistency") => Severity::Info,
_ => Severity::Warning,
}
}