pub const IGNORED_DIRS: &[&str] = &[
".git",
".hg",
".svn",
"node_modules",
"target",
"__pycache__",
".venv",
"venv",
];
pub const REUSE_DIRS: &[&str] = &["LICENSES", ".reuse"];
pub const LICENSE_COMPANION_SUFFIX: &str = ".license";
pub const REUSE_MANIFEST_NAMES: &[&str] = &["REUSE.toml", "reuse.toml", ".reuse.toml"];
pub const SPDX_DOCUMENT_SUFFIXES: &[&str] = &[
".spdx.json",
".spdx.xml",
".spdx.yml",
".spdx.yaml",
".spdx.rdf",
];
#[must_use]
pub fn is_pruned_dir(name: &str) -> bool {
IGNORED_DIRS.contains(&name) || REUSE_DIRS.contains(&name)
}
#[must_use]
pub fn is_within_pruned_dir(relative: &std::path::Path) -> bool {
relative
.components()
.any(|c| is_pruned_dir(c.as_os_str().to_string_lossy().as_ref()))
}
#[must_use]
pub fn is_reuse_manifest(name: &str) -> bool {
REUSE_MANIFEST_NAMES.contains(&name)
}
#[must_use]
pub fn is_spdx_document(name: &str) -> bool {
SPDX_DOCUMENT_SUFFIXES.iter().any(|s| name.ends_with(s))
|| std::path::Path::new(name)
.extension()
.is_some_and(|ext| ext.eq_ignore_ascii_case("spdx"))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn pruned_dirs_span_both_lists_and_nothing_else() {
assert!(is_pruned_dir(".git"));
assert!(is_pruned_dir("target"));
assert!(is_pruned_dir(".venv"));
assert!(is_pruned_dir("LICENSES"));
assert!(is_pruned_dir(".reuse"));
assert!(!is_pruned_dir("src"));
assert!(!is_pruned_dir("gitignore"));
}
#[test]
fn a_manifest_is_recognised_by_each_accepted_spelling() {
for name in REUSE_MANIFEST_NAMES {
assert!(is_reuse_manifest(name), "{name}");
}
assert!(!is_reuse_manifest("REUSE.md"));
}
#[test]
fn spdx_documents_include_the_bare_extension_in_any_case() {
assert!(is_spdx_document("sbom.spdx.json"));
assert!(is_spdx_document("sbom.spdx.rdf"));
assert!(is_spdx_document("sbom.spdx"));
assert!(is_spdx_document("sbom.SPDX"));
assert!(!is_spdx_document("spdx.rs"));
assert!(!is_spdx_document("notes.md"));
}
}