use super::{AwarenessMap, EcosystemAwareness};
use once_cell::sync::Lazy;
use regex::Regex;
static ERROR_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"error\[(\w+)\]:(.+)").unwrap());
static WARNING_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"warning:(.+)").unwrap());
static FINISHED_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"Finished .+ in ([\d.]+)s").unwrap());
pub struct BuildAwareness;
impl EcosystemAwareness for BuildAwareness {
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, "make" | "cmake" | "gradle" | "bazel")
|| (prog == "cargo" && matches!(sub, "build" | "check" | "clippy"))
|| (prog == "go" && matches!(sub, "build" | "vet"))
}
fn extract(&self, stdout: &str, stderr: &str, exit_code: i32) -> AwarenessMap {
let mut map = AwarenessMap::new("build", "compile");
map.insert("exit_code", exit_code.to_string());
let combined = format!("{stderr}\n{stdout}");
let error_count = ERROR_RE.captures_iter(&combined).count();
let warning_count = WARNING_RE.captures_iter(&combined).count();
map.insert("error_count", error_count.to_string());
map.insert("warning_count", warning_count.to_string());
map.insert("succeeded", (exit_code == 0).to_string());
if let Some(cap) = FINISHED_RE.captures(&combined) {
map.insert("build_time_secs", cap[1].to_string());
}
map
}
}