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>,
}
#[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>,
sources: Vec<PathBuf>,
}
#[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 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 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.disable {
if !selection.set(spec, false) {
return Err(Error::UnknownEcosystem { name: spec.clone() });
}
}
for spec in &self.flags.enable {
if !selection.set(spec, true) {
return Err(Error::UnknownEcosystem { name: spec.clone() });
}
}
let mut exclude = accumulated.exclude.clone();
exclude.extend(self.flags.exclude.iter().cloned());
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: accumulated.include.clone(),
sources: accumulated.sources.clone(),
})
}
}
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));
}
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);
}
for pattern in &layer.file.exclude {
accumulated.exclude.push(expand(pattern, dir));
}
for pattern in &layer.file.include {
accumulated.include.push(expand(pattern, dir));
}
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)?,
});
}
accumulated.sources.push(source.clone());
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(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_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")));
}
}