mod apple;
mod functional;
mod infra;
mod managed;
mod native;
mod python;
mod scientific;
mod scripting;
mod tooling;
mod web;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Anchor {
Sibling,
Ancestor(u8),
Inside,
WorkspaceRoot,
}
impl Anchor {
#[must_use]
pub const fn levels(self) -> u8 {
match self {
Self::Sibling | Self::Inside | Self::WorkspaceRoot => 0,
Self::Ancestor(n) => n,
}
}
#[must_use]
pub const fn is_inside(self) -> bool {
matches!(self, Self::Inside)
}
#[must_use]
pub const fn is_workspace_root(self) -> bool {
matches!(self, Self::WorkspaceRoot)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Artifact {
pub path: &'static str,
pub default_on: bool,
pub note: Option<&'static str>,
pub anchor: Option<Anchor>,
pub slug: Option<&'static str>,
}
impl Artifact {
#[must_use]
pub const fn on(path: &'static str) -> Self {
Self {
path,
default_on: true,
note: None,
anchor: None,
slug: None,
}
}
#[must_use]
pub const fn off(path: &'static str, note: &'static str) -> Self {
Self {
path,
default_on: false,
note: Some(note),
anchor: None,
slug: None,
}
}
#[must_use]
pub const fn at(mut self, anchor: Anchor) -> Self {
self.anchor = Some(anchor);
self
}
#[must_use]
pub const fn named(mut self, slug: &'static str) -> Self {
self.slug = Some(slug);
self
}
#[must_use]
pub const fn anchor_in(&self, ecosystem: &Ecosystem) -> Anchor {
match self.anchor {
Some(anchor) => anchor,
None => ecosystem.anchor,
}
}
#[must_use]
pub const fn requires_dir(&self) -> bool {
self.path.as_bytes()[self.path.len() - 1] == b'/'
}
pub fn segments(&self) -> impl Iterator<Item = &'static str> {
self.path.trim_end_matches('/').split('/')
}
#[must_use]
pub fn slug(&self) -> String {
if let Some(slug) = self.slug {
return slug.to_owned();
}
let flattened: String = self
.path
.trim_end_matches('/')
.chars()
.filter(|character| *character != '*')
.map(|character| if character == '/' { '-' } else { character })
.collect();
let trimmed = flattened.replace("-.", "-").trim_matches(['.', '-']).to_owned();
if trimmed.is_empty() { flattened } else { trimmed }
}
#[must_use]
pub fn depth(&self) -> usize {
self.segments().count()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Ecosystem {
pub id: &'static str,
pub name: &'static str,
pub markers: &'static [&'static str],
pub anchor: Anchor,
pub artifacts: &'static [Artifact],
}
impl Ecosystem {
#[must_use]
pub fn artifact(&self, path: &str) -> Option<&'static Artifact> {
self.artifacts.iter().find(|artifact| artifact.path == path)
}
}
pub static CATALOG: &[Ecosystem] = &[
native::RUST,
web::NODE,
python::PYTHON,
native::GO,
native::ZIG,
apple::SWIFT,
apple::XCODE,
managed::MAVEN,
managed::GRADLE,
managed::DOTNET,
native::CMAKE,
scripting::DART,
functional::ELIXIR,
scripting::RUBY,
web::PHP,
managed::SCALA,
functional::HASKELL,
functional::OCAML,
scientific::JULIA,
scientific::R,
native::NIM,
web::ELM,
infra::TERRAFORM,
infra::BAZEL,
tooling::ALEF,
tooling::BASEMIND,
];
pub const MAX_ECOSYSTEMS: usize = u32::BITS as usize;
pub const DEPENDENCY_DIRS: &[&str] = &["node_modules", "vendor", "deps", "bower_components"];
pub const DEPENDENCY_ARTIFACTS: &[&str] = &[
"node.node_modules",
"rust.vendor",
"php.vendor",
"go.vendor",
"ruby.vendor-bundle",
"elixir.deps",
"python.venv",
];
#[must_use]
pub fn is_dependency_artifact(spec: &str) -> bool {
DEPENDENCY_ARTIFACTS.contains(&spec)
}
#[must_use]
pub fn find(id: &str) -> Option<&'static Ecosystem> {
CATALOG.iter().find(|ecosystem| ecosystem.id == id)
}
#[cfg(test)]
mod tests {
use std::collections::HashSet;
use super::*;
#[test]
fn every_entry_has_at_least_one_marker() {
for ecosystem in CATALOG {
assert!(
!ecosystem.markers.is_empty(),
"ecosystem `{}` has no marker glob",
ecosystem.id
);
for marker in ecosystem.markers {
assert!(
!marker.is_empty(),
"ecosystem `{}` has an empty marker glob",
ecosystem.id
);
}
}
}
#[test]
fn every_id_is_unique() {
let mut seen = HashSet::new();
for ecosystem in CATALOG {
assert!(seen.insert(ecosystem.id), "duplicate ecosystem id `{}`", ecosystem.id);
}
}
#[test]
fn every_entry_declares_at_least_one_artifact() {
for ecosystem in CATALOG {
assert!(
!ecosystem.artifacts.is_empty(),
"ecosystem `{}` removes nothing",
ecosystem.id
);
}
}
#[test]
fn no_artifact_path_escapes_its_anchor() {
for ecosystem in CATALOG {
for artifact in ecosystem.artifacts {
let path = artifact.path;
assert!(
!path.is_empty(),
"ecosystem `{}` has an empty artifact path",
ecosystem.id
);
assert!(
!path.starts_with('/'),
"`{}`: artifact `{path}` is absolute",
ecosystem.id
);
assert!(
!path.starts_with('~'),
"`{}`: artifact `{path}` is home-relative",
ecosystem.id
);
for segment in artifact.segments() {
assert!(
!segment.is_empty(),
"`{}`: artifact `{path}` has an empty segment",
ecosystem.id
);
assert_ne!(
segment, "..",
"`{}`: artifact `{path}` escapes its anchor",
ecosystem.id
);
assert_ne!(segment, ".", "`{}`: artifact `{path}` has a `.` segment", ecosystem.id);
}
}
}
}
#[test]
fn a_dependency_directory_artifact_is_never_on_by_default() {
for ecosystem in CATALOG {
for artifact in ecosystem.artifacts {
let whole = artifact.path.trim_end_matches('/');
if !DEPENDENCY_DIRS.contains(&whole) {
continue;
}
assert!(
!artifact.default_on,
"`{}`: artifact `{}` is a dependency directory and is on by default — a run \
that asked for nothing would delete a network-fetched cache",
ecosystem.id, artifact.path
);
assert!(
artifact.note.is_some(),
"`{}`: dependency directory `{}` has no note saying what removing it costs",
ecosystem.id,
artifact.path
);
}
}
}
#[test]
fn every_dependency_artifact_spec_names_an_opt_in_declaration() {
for spec in DEPENDENCY_ARTIFACTS {
let id = crate::classify::ArtifactId::from_spec(spec)
.unwrap_or_else(|| panic!("`{spec}` is in DEPENDENCY_ARTIFACTS but not in the catalog"));
assert!(
!id.artifact().default_on,
"`{spec}` is enabled by --clean-dependencies but is already on by default"
);
assert!(is_dependency_artifact(spec), "`{spec}` must answer to the predicate");
}
assert!(
!is_dependency_artifact("rust.target"),
"an ordinary artifact is not a dependency directory"
);
}
#[test]
fn every_opt_in_artifact_has_a_note() {
for ecosystem in CATALOG {
for artifact in ecosystem.artifacts {
if !artifact.default_on {
assert!(
artifact.note.is_some(),
"`{}`: artifact `{}` is off by default with no note",
ecosystem.id,
artifact.path
);
}
}
}
}
#[test]
fn catalog_fits_the_marker_bitmask() {
assert!(
CATALOG.len() <= MAX_ECOSYSTEMS,
"catalog has {} entries but the marker bitmask holds {MAX_ECOSYSTEMS}",
CATALOG.len()
);
}
#[test]
fn ancestor_anchors_are_bounded() {
for ecosystem in CATALOG {
for artifact in ecosystem.artifacts {
let levels = artifact.anchor_in(ecosystem).levels();
assert!(
levels <= 8,
"`{}`: artifact `{}` climbs {levels} levels",
ecosystem.id,
artifact.path
);
}
}
}
#[test]
fn leading_artifact_segments_are_literal() {
for ecosystem in CATALOG {
for artifact in ecosystem.artifacts {
let segments: Vec<_> = artifact.segments().collect();
for segment in &segments[..segments.len() - 1] {
assert!(
crate::names::is_literal(segment),
"`{}`: artifact `{}` has the non-literal leading segment `{segment}`",
ecosystem.id,
artifact.path
);
}
}
}
}
#[test]
fn every_inside_anchored_artifact_requires_a_directory() {
for ecosystem in CATALOG {
for artifact in ecosystem.artifacts {
if artifact.anchor_in(ecosystem).is_inside() {
assert!(
artifact.requires_dir(),
"`{}`: artifact `{}` anchors Inside but does not require a directory — \
it needs a trailing `/`",
ecosystem.id,
artifact.path
);
}
}
}
}
#[test]
fn artifact_slugs_are_unique_within_an_ecosystem() {
for ecosystem in CATALOG {
let mut seen = HashSet::new();
for artifact in ecosystem.artifacts {
let slug = artifact.slug();
assert!(
seen.insert(slug.clone()),
"`{}`: duplicate artifact slug `{slug}`",
ecosystem.id
);
assert!(
!slug.is_empty(),
"`{}`: artifact `{}` has an empty slug",
ecosystem.id,
artifact.path
);
}
}
}
#[test]
fn slug_strips_leading_dots_globs_and_separators() {
assert_eq!(Artifact::on(".venv/").slug(), "venv");
assert_eq!(Artifact::on("build/").slug(), "build");
assert_eq!(Artifact::on("*.egg-info/").slug(), "egg-info");
assert_eq!(Artifact::on("cmake-build-*/").slug(), "cmake-build");
assert_eq!(Artifact::on("deps/build/").slug(), "deps-build");
assert_eq!(Artifact::on("src/*.o").slug(), "src-o");
}
#[test]
fn requires_dir_follows_the_trailing_slash() {
assert!(Artifact::on("target/").requires_dir());
assert!(!Artifact::on("*.tsbuildinfo").requires_dir());
}
#[test]
fn segments_splits_a_multi_segment_artifact() {
let artifact = Artifact::on("vendor/bundle/");
assert_eq!(artifact.segments().collect::<Vec<_>>(), vec!["vendor", "bundle"]);
assert_eq!(artifact.depth(), 2);
}
#[test]
fn find_resolves_a_known_id_and_rejects_an_unknown_one() {
assert_eq!(find("rust").map(|ecosystem| ecosystem.name), Some("Rust"));
assert_eq!(find("node_modules"), None);
}
}