use std::collections::{BTreeMap, HashMap};
use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};
use globset::{Glob, GlobMatcher};
use super::load::{Layer, Sources, read_in};
use super::schema::{EcosystemsSection, KeepTable};
use crate::catalog;
use crate::classify::{ArtifactId, Selection};
use crate::error::{Error, Result};
use crate::policy::{KeepPolicy, parse_duration, parse_size};
#[derive(Debug, Default, Clone)]
pub struct Flags {
pub ecosystems: Option<Vec<String>>,
pub enable: Vec<String>,
pub disable: Vec<String>,
pub keep: KeepPolicy,
pub exclude: Vec<String>,
pub include: Vec<String>,
pub clean_caches: Vec<String>,
pub git: Option<bool>,
pub bazel: Option<bool>,
pub bazel_max_age: Option<std::time::Duration>,
pub docker: Option<bool>,
pub docker_max_age: Option<std::time::Duration>,
pub clean_tagged: Option<bool>,
}
#[derive(Debug, Clone)]
struct Rule {
pattern: String,
glob: GlobMatcher,
keep: KeepPolicy,
ops: Vec<(String, bool)>,
}
#[derive(Debug, Default, Clone)]
struct Accumulated {
ops: Vec<(String, bool)>,
keep: KeepPolicy,
keep_by_ecosystem: BTreeMap<String, KeepPolicy>,
rules: Vec<Rule>,
exclude: Vec<String>,
include: Vec<String>,
clean_caches: Vec<String>,
git: Option<bool>,
bazel: Option<bool>,
bazel_max_age: Option<std::time::Duration>,
docker: Option<bool>,
docker_max_age: Option<std::time::Duration>,
clean_tagged: Option<bool>,
sources: Vec<PathBuf>,
}
#[expect(
clippy::struct_excessive_bools,
reason = "these mirror command-line flags, which are bools by nature"
)]
#[derive(Debug, Clone)]
pub struct Resolved {
selection: Selection,
keep: KeepPolicy,
flags_keep: KeepPolicy,
keep_by_ecosystem: BTreeMap<String, KeepPolicy>,
rules: Vec<Rule>,
pub exclude: Vec<String>,
pub include: Vec<String>,
pub clean_caches: Vec<String>,
pub git: bool,
pub bazel: bool,
pub bazel_max_age: Option<std::time::Duration>,
pub docker: bool,
pub docker_max_age: Option<std::time::Duration>,
pub clean_tagged: bool,
pub sources: Vec<PathBuf>,
}
impl Resolved {
#[must_use]
pub fn is_enabled(&self, path: &Path, id: ArtifactId) -> bool {
let mut selection = None;
for rule in &self.rules {
if rule.ops.is_empty() || !rule.glob.is_match(path) {
continue;
}
let selection = selection.get_or_insert_with(|| self.selection.clone());
for (spec, enabled) in &rule.ops {
selection.set(spec, *enabled);
}
}
selection.as_ref().unwrap_or(&self.selection).is_enabled(id)
}
#[must_use]
pub fn keep_for(&self, path: &Path, ecosystem: Option<&str>) -> KeepPolicy {
let mut keep = self.keep;
if let Some(per_ecosystem) = ecosystem.and_then(|id| self.keep_by_ecosystem.get(id)) {
keep = keep.narrow(*per_ecosystem);
}
for rule in &self.rules {
if rule.glob.is_match(path) {
keep = keep.narrow(rule.keep);
}
}
keep.narrow(self.flags_keep)
}
#[must_use]
pub fn keep(&self) -> KeepPolicy {
self.keep
}
#[must_use]
pub fn selection(&self) -> &Selection {
&self.selection
}
pub fn keep_overrides(&self) -> impl Iterator<Item = (&str, KeepPolicy)> {
self.keep_by_ecosystem.iter().map(|(id, keep)| (id.as_str(), *keep))
}
pub fn rules(&self) -> impl Iterator<Item = (&str, KeepPolicy, &[(String, bool)])> {
self.rules
.iter()
.map(|rule| (rule.pattern.as_str(), rule.keep, rule.ops.as_slice()))
}
}
#[derive(Debug)]
pub struct Resolver {
root: PathBuf,
flags: Flags,
base: Accumulated,
explicit: bool,
cache: RwLock<HashMap<PathBuf, Arc<Cached>>>,
}
#[derive(Debug)]
struct Cached {
accumulated: Accumulated,
effective: Arc<Resolved>,
}
impl Resolver {
pub fn new(root: &Path, sources: &Sources, flags: Flags) -> Result<Self> {
let mut base = Accumulated::default();
for layer in &sources.layers {
apply(&mut base, layer)?;
}
Ok(Self {
root: root.to_path_buf(),
flags,
base,
explicit: sources.explicit,
cache: RwLock::new(HashMap::new()),
})
}
pub fn root_config(&self) -> Result<Arc<Resolved>> {
let root = self.root.clone();
self.for_dir(&root)
}
pub fn for_dir(&self, dir: &Path) -> Result<Arc<Resolved>> {
Ok(self.cached(dir)?.effective.clone())
}
fn cached(&self, dir: &Path) -> Result<Arc<Cached>> {
if let Ok(cache) = self.cache.read()
&& let Some(cached) = cache.get(dir)
{
return Ok(cached.clone());
}
let mut accumulated = if dir == self.root {
self.base.clone()
} else {
match dir.parent() {
Some(parent) if dir.starts_with(&self.root) => self.cached(parent)?.accumulated.clone(),
_ => self.base.clone(),
}
};
if !self.explicit
&& let Some(layer) = read_in(dir)?
{
apply(&mut accumulated, &layer)?;
}
let effective = Arc::new(self.effective(&accumulated)?);
let cached = Arc::new(Cached { accumulated, effective });
if let Ok(mut cache) = self.cache.write() {
cache.insert(dir.to_path_buf(), cached.clone());
}
Ok(cached)
}
fn effective(&self, accumulated: &Accumulated) -> Result<Resolved> {
let selection = self.selection(accumulated)?;
let mut exclude = accumulated.exclude.clone();
exclude.extend(expand_flag_patterns(&self.flags.exclude, &self.root));
let mut include = accumulated.include.clone();
include.extend(expand_flag_patterns(&self.flags.include, &self.root));
let (clean_caches, clear_all) = self.clean_caches(accumulated)?;
Ok(Resolved {
selection,
keep: accumulated.keep.narrow(self.flags.keep),
flags_keep: self.flags.keep,
keep_by_ecosystem: accumulated.keep_by_ecosystem.clone(),
rules: accumulated.rules.clone(),
exclude,
include,
clean_caches,
git: self.flags.git.or(accumulated.git).unwrap_or(true),
bazel: self.flags.bazel.or(accumulated.bazel).unwrap_or(true),
bazel_max_age: self.flags.bazel_max_age.or(accumulated.bazel_max_age),
docker: self.flags.docker.or(accumulated.docker).unwrap_or(false),
docker_max_age: self.flags.docker_max_age.or(accumulated.docker_max_age),
clean_tagged: self
.flags
.clean_tagged
.or_else(|| self.flags.clean_caches.iter().any(|id| id == "all").then_some(true))
.or(accumulated.clean_tagged)
.unwrap_or(clear_all),
sources: accumulated.sources.clone(),
})
}
fn selection(&self, accumulated: &Accumulated) -> Result<Selection> {
let mut selection = match &self.flags.ecosystems {
Some(ids) => {
let (selection, unknown) = Selection::only(ids.iter().map(String::as_str));
if let Some(name) = unknown.first() {
return Err(Error::UnknownEcosystem { name: name.clone() });
}
selection
}
None => Selection::all(),
};
for (spec, enabled) in &accumulated.ops {
selection.set(spec, *enabled);
}
for spec in &self.flags.enable {
if !selection.set(spec, true) {
return Err(Error::UnknownEcosystem { name: spec.clone() });
}
}
for spec in &self.flags.disable {
if !selection.set(spec, false) {
return Err(Error::UnknownEcosystem { name: spec.clone() });
}
}
Ok(selection)
}
fn clean_caches(&self, accumulated: &Accumulated) -> Result<(Vec<String>, bool)> {
let mut clean_caches = accumulated.clean_caches.clone();
clean_caches.extend(self.flags.clean_caches.iter().cloned());
let clear_all = clean_caches.iter().any(|id| id == "all");
if clear_all {
clean_caches.retain(|id| id != "all");
clean_caches.extend(
crate::caches::CACHES
.iter()
.filter(|cache| cache.in_all)
.map(|cache| cache.id.to_owned()),
);
clean_caches.push(crate::caches::BAZEL_ID.to_owned());
}
let excluded: Vec<String> = clean_caches
.iter()
.filter_map(|id| id.strip_prefix('-').map(str::to_owned))
.collect();
if !excluded.is_empty() && !clean_caches.iter().any(|id| !id.starts_with('-')) {
return Err(Error::LoneCacheExclusion {
id: excluded.first().cloned().unwrap_or_default(),
});
}
clean_caches.retain(|id| !id.starts_with('-') && !excluded.contains(id));
clean_caches.sort_unstable();
clean_caches.dedup();
let unknown = crate::caches::unknown_ids(&clean_caches)
.into_iter()
.chain(crate::caches::unknown_ids(&excluded))
.collect::<Vec<_>>();
if let Some(id) = unknown.first() {
return Err(Error::UnknownCache { id: id.clone() });
}
Ok((clean_caches, clear_all))
}
}
fn apply(accumulated: &mut Accumulated, layer: &Layer) -> Result<()> {
let source = &layer.source;
let dir = source.parent().unwrap_or(Path::new("."));
for spec in &layer.file.ecosystems.disable {
validate_spec(spec, source)?;
accumulated.ops.push((spec.clone(), false));
}
for spec in &layer.file.ecosystems.enable {
validate_spec(spec, source)?;
accumulated.ops.push((spec.clone(), true));
}
if let Some(enabled) = layer.file.tagged.enabled {
accumulated.clean_tagged = Some(enabled);
}
if let Some(enabled) = layer.file.git.enabled {
accumulated.git = Some(enabled);
}
apply_machine_global(accumulated, layer)?;
apply_keep(accumulated, layer)?;
for pattern in &layer.file.exclude {
accumulated.exclude.push(expand(pattern, dir));
}
for pattern in &layer.file.include {
accumulated.include.push(expand(pattern, dir));
}
apply_rules(accumulated, layer, dir)?;
accumulated.sources.push(source.clone());
Ok(())
}
fn apply_machine_global(accumulated: &mut Accumulated, layer: &Layer) -> Result<()> {
let source = &layer.source;
if layer.user_level {
if let Some(enabled) = layer.file.bazel.enabled {
accumulated.bazel = Some(enabled);
}
if let Some(age) = &layer.file.bazel.max_age {
accumulated.bazel_max_age = Some(crate::policy::parse_duration(age).map_err(|error| Error::Config {
path: source.clone(),
reason: format!("`[bazel] max_age`: {error}"),
})?);
}
if let Some(enabled) = layer.file.docker.enabled {
accumulated.docker = Some(enabled);
}
if let Some(age) = &layer.file.docker.max_age {
accumulated.docker_max_age = Some(crate::policy::parse_duration(age).map_err(|error| Error::Config {
path: source.clone(),
reason: format!("`[docker] max_age`: {error}"),
})?);
}
for id in &layer.file.caches.enable {
accumulated.clean_caches.push(id.clone());
}
} else if layer.file.bazel.enabled.is_some()
|| layer.file.bazel.max_age.is_some()
|| layer.file.docker.enabled.is_some()
|| layer.file.docker.max_age.is_some()
|| !layer.file.caches.enable.is_empty()
{
return Err(Error::Config {
path: source.clone(),
reason: "`[bazel]`, `[docker]` and `[caches]` are machine-global and are read only from the user \
configuration or `--config`, never from a repository's `voom.toml`"
.to_owned(),
});
}
Ok(())
}
fn apply_keep(accumulated: &mut Accumulated, layer: &Layer) -> Result<()> {
let source = &layer.source;
accumulated.keep = accumulated.keep.narrow(keep_from(&layer.file.keep.scalars(), source)?);
for (id, table) in &layer.file.keep.per_ecosystem {
if catalog::find(id).is_none() {
return Err(Error::Config {
path: source.clone(),
reason: format!("`[keep.{id}]` is not a known ecosystem"),
});
}
let policy = keep_from(table, source)?;
let entry = accumulated.keep_by_ecosystem.entry(id.clone()).or_default();
*entry = entry.narrow(policy);
}
Ok(())
}
fn apply_rules(accumulated: &mut Accumulated, layer: &Layer, dir: &Path) -> Result<()> {
let source = &layer.source;
for rule in &layer.file.paths {
let pattern = expand(&rule.pattern, dir);
let glob = Glob::new(&pattern)
.map_err(|error| Error::Config {
path: source.clone(),
reason: format!("`[[paths]] match = \"{pattern}\"` is not a valid glob: {error}"),
})?
.compile_matcher();
let keep = match &rule.keep {
Some(table) => keep_from(table, source)?,
None => KeepPolicy::default(),
};
accumulated.rules.push(Rule {
pattern,
glob,
keep,
ops: rule_ops(rule.ecosystems.as_ref(), source)?,
});
}
Ok(())
}
fn rule_ops(section: Option<&EcosystemsSection>, source: &Path) -> Result<Vec<(String, bool)>> {
let Some(section) = section else { return Ok(Vec::new()) };
let mut ops = Vec::new();
for spec in §ion.disable {
validate_spec(spec, source)?;
ops.push((spec.clone(), false));
}
for spec in §ion.enable {
validate_spec(spec, source)?;
ops.push((spec.clone(), true));
}
Ok(ops)
}
fn validate_spec(spec: &str, source: &Path) -> Result<()> {
let known = match spec.split_once('.') {
Some((ecosystem, _)) => ArtifactId::from_spec(spec).is_some() && catalog::find(ecosystem).is_some(),
None => catalog::find(spec).is_some(),
};
if known {
return Ok(());
}
Err(Error::Config {
path: source.to_path_buf(),
reason: format!("`{spec}` is not a known ecosystem or artifact — run `voom catalog` to see the table"),
})
}
fn keep_from(table: &KeepTable, source: &Path) -> Result<KeepPolicy> {
let attribute = |error: Error| Error::Config {
path: source.to_path_buf(),
reason: error.to_string(),
};
Ok(KeepPolicy {
min_age: table
.min_age
.as_deref()
.map(parse_duration)
.transpose()
.map_err(attribute)?,
min_size: table
.min_size
.as_deref()
.map(parse_size)
.transpose()
.map_err(attribute)?,
max_size: table
.max_size
.as_deref()
.map(parse_size)
.transpose()
.map_err(attribute)?,
})
}
fn expand_flag_patterns(patterns: &[String], root: &Path) -> Vec<String> {
patterns.iter().map(|pattern| expand(pattern, root)).collect()
}
fn expand(pattern: &str, dir: &Path) -> String {
if let Some(rest) = pattern.strip_prefix("~/").or_else(|| pattern.strip_prefix("~\\"))
&& let Some(home) = dirs::home_dir()
{
return as_glob(&home.join(rest));
}
if Path::new(pattern).is_absolute() || pattern.starts_with("**") {
return pattern.to_owned();
}
as_glob(&dir.join(pattern))
}
fn as_glob(path: &Path) -> String {
path.display().to_string().replace(std::path::MAIN_SEPARATOR, "/")
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::load::read_in;
use crate::scan::PatternSet;
use crate::testing::tree;
fn home() -> PathBuf {
dirs::home_dir().expect("a home directory")
}
#[test]
fn should_expand_a_home_pattern_to_a_slash_separated_glob() {
let expanded = expand("~/work/client-x/**", Path::new("/anywhere"));
assert!(
!expanded.contains('\\'),
"a backslash escapes in glob syntax: {expanded}"
);
assert!(expanded.ends_with("/work/client-x/**"), "{expanded}");
assert_ne!(expanded, "~/work/client-x/**", "the home prefix must be expanded");
}
#[test]
fn should_expand_a_backslash_home_prefix_the_same_way() {
let dir = Path::new("/anywhere");
assert_eq!(expand("~\\scratch/**", dir), expand("~/scratch/**", dir));
}
#[test]
fn should_expand_a_relative_pattern_to_a_slash_separated_glob() {
let fixture = tree(&["project/voom.toml"]);
let expanded = expand("build/**", &fixture.path().join("project"));
assert!(
!expanded.contains('\\'),
"a backslash escapes in glob syntax: {expanded}"
);
assert!(expanded.ends_with("/project/build/**"), "{expanded}");
}
#[test]
fn should_pass_a_double_star_pattern_through_unchanged() {
let expanded = expand("**/fixtures/**", Path::new("/anywhere"));
assert_eq!(expanded, "**/fixtures/**");
assert!(!expanded.contains('\\'), "{expanded}");
}
#[test]
fn should_pass_an_absolute_pattern_through_unchanged() {
let absolute = home().join("work").display().to_string();
let pattern = format!("{absolute}/**");
assert_eq!(expand(&pattern, Path::new("/anywhere")), pattern);
}
#[test]
fn should_produce_an_exclude_that_matches_the_path_it_names() {
let fixture = tree(&["project/voom.toml", "project/build/output.o"]);
let dir = fixture.path().join("project");
std::fs::write(dir.join("voom.toml"), "exclude = [\"build/**\"]\n").unwrap();
let layer = read_in(&dir).expect("it parses").expect("a layer");
let mut accumulated = Accumulated::default();
apply(&mut accumulated, &layer).expect("the layer applies");
let excludes = PatternSet::new(accumulated.exclude.clone()).expect("the exclude compiles");
assert_eq!(
excludes.matched(&dir.join("build").join("output.o")),
Some(accumulated.exclude[0].as_str())
);
}
#[test]
fn should_produce_a_home_exclude_that_matches_the_path_it_names() {
let pattern = expand("~/work/client-x/**", Path::new("/anywhere"));
let excludes = PatternSet::new([pattern]).expect("the exclude compiles");
let candidate = home().join("work").join("client-x").join("target");
assert!(excludes.matched(&candidate).is_some(), "{}", candidate.display());
}
#[test]
fn should_anchor_a_relative_flag_pattern_to_the_scan_root() {
assert_eq!(
expand_flag_patterns(&["build".to_owned()], Path::new("/srv/tree")),
vec!["/srv/tree/build".to_owned()],
"a bare pattern must be anchored, or it matches nothing the walker produces"
);
}
#[test]
fn should_keep_the_roots_spelling_when_anchoring_a_flag_pattern() {
assert_eq!(
expand_flag_patterns(&["proj".to_owned()], Path::new(".")),
vec!["./proj".to_owned()]
);
}
#[test]
fn should_leave_an_absolute_flag_pattern_alone() {
let absolute = if cfg!(windows) {
r"C:\builds\**"
} else {
"/var/builds/**"
};
assert_eq!(
expand_flag_patterns(&[absolute.to_owned()], Path::new("/srv/tree")),
vec![absolute.to_owned()]
);
}
#[test]
fn should_produce_a_path_rule_that_matches_the_path_it_names() {
let fixture = tree(&["project/voom.toml"]);
let dir = fixture.path().join("project");
std::fs::write(
dir.join("voom.toml"),
"[[paths]]\nmatch = \"vendor/**\"\n[paths.keep]\nmin_age = \"7d\"\n",
)
.unwrap();
let layer = read_in(&dir).expect("it parses").expect("a layer");
let mut accumulated = Accumulated::default();
apply(&mut accumulated, &layer).expect("the layer applies");
let rule = accumulated.rules.first().expect("one rule");
assert!(!rule.pattern.contains('\\'), "{}", rule.pattern);
assert!(rule.glob.is_match(dir.join("vendor").join("target")));
}
}