use std::collections::{BTreeMap, BTreeSet};
use super::{Model, Query};
use crate::RepoPath;
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct PlanIndex {
by_target: BTreeMap<RepoPath, BTreeSet<RepoPath>>,
}
impl PlanIndex {
pub fn add(&mut self, planner: &RepoPath, target: &RepoPath) {
self.by_target
.entry(target.clone())
.or_default()
.insert(planner.clone());
}
pub fn contains(&self, target: &RepoPath) -> bool {
self.by_target.contains_key(target)
}
pub fn targets(&self) -> Vec<RepoPath> {
self.by_target.keys().cloned().collect()
}
pub fn planners(&self, target: &RepoPath) -> Vec<RepoPath> {
self.by_target
.get(target)
.map(|p| p.iter().cloned().collect())
.unwrap_or_default()
}
pub fn of(&self, planner: &RepoPath) -> BTreeSet<RepoPath> {
self.by_target
.iter()
.filter(|(_, planners)| planners.contains(planner))
.map(|(target, _)| target.clone())
.collect()
}
}
impl Model {
pub fn is_planned(&self, path: &RepoPath) -> bool {
self.note(Query::Planned(path.clone()));
self.plans.contains(path)
}
}