use super::{AwarenessMap, EcosystemAwareness};
use once_cell::sync::Lazy;
use regex::Regex;
static CLIPPY_ERR_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"error\[(\w+)\]").unwrap());
static CLIPPY_WARN_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"warning: ").unwrap());
static RUFF_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"Found (\d+) error").unwrap());
pub struct LintAwareness;
impl EcosystemAwareness for LintAwareness {
fn matches(&self, program: &str, args: &[String]) -> bool {
let prog = program.split('/').next_back().unwrap_or(program);
let sub = args.first().map(|s| s.as_str()).unwrap_or("");
matches!(prog, "ruff" | "mypy" | "eslint" | "golangci-lint")
|| (prog == "cargo" && sub == "clippy")
}
fn extract(&self, stdout: &str, stderr: &str, exit_code: i32) -> AwarenessMap {
let mut map = AwarenessMap::new("lint", "check");
map.insert("exit_code", exit_code.to_string());
let combined = format!("{stdout}\n{stderr}");
let error_count = CLIPPY_ERR_RE.captures_iter(&combined).count();
let warning_count = CLIPPY_WARN_RE.find_iter(&combined).count();
map.insert("error_count", error_count.to_string());
map.insert("warning_count", warning_count.to_string());
if let Some(cap) = RUFF_RE.captures(&combined) {
map.insert("ruff_errors", cap[1].to_string());
}
map
}
}