use super::decision::DecisionSpec;
use super::reqfile::CommandCheck;
#[derive(Debug, Clone, Copy)]
pub struct Candidate<'s> {
pub path: &'s str,
pub relative: &'s str,
pub exists: bool,
}
#[derive(Debug, PartialEq)]
pub enum CommandPlan {
Run(Vec<String>),
NoMatchingFiles,
}
pub fn plan_command(check: &CommandCheck, candidates: &[Candidate]) -> CommandPlan {
let Some(glob) = &check.files else {
return CommandPlan::Run(Vec::new());
};
let matching: Vec<&Candidate> = candidates
.iter()
.filter(|c| glob.is_match(c.relative))
.collect();
if check.pass_files {
let args: Vec<String> = matching
.iter()
.filter(|c| c.exists)
.map(|c| c.relative.to_string())
.collect();
if args.is_empty() {
CommandPlan::NoMatchingFiles
} else {
CommandPlan::Run(args)
}
} else if matching.is_empty() {
CommandPlan::NoMatchingFiles
} else {
CommandPlan::Run(Vec::new())
}
}
pub fn decision_files<'s>(spec: &DecisionSpec, candidates: &[Candidate<'s>]) -> Vec<Candidate<'s>> {
candidates
.iter()
.filter(|c| c.exists && spec.applies_to(c.relative))
.copied()
.collect()
}