pub fn build_plan(root: &Path, patterns: &[String]) -> Result<RemovalPlan>Expand description
Scans beneath the specified root directory and constructs a RemovalPlan.
Discovered filesystem entries are evaluated against the provided patterns. An item is
kept if it matches any pattern (logical OR). When an item is kept, all of its ancestor
directories up to root are also kept to preserve directory hierarchy.
§Arguments
root- Path to the directory to scan. Relative paths are resolved against the current working directory. Filesystem roots (e.g./on Unix,C:\on Windows) and Windows drive-relative paths (e.g.C:foo) are rejected.patterns- One or more keep patterns. Patterns may be literal relative paths or glob patterns (*,?,**,[...],{a,b}; on non-Windows platforms\escapes the next character).?and a character class each match one Unicode scalar value. Literal directories without glob syntax match recursively; glob matches do not keep unmatched descendants. Relative patterns are resolved relative toroot. Patterns containing a..component are rejected. Leading and trailing whitespace is preserved.
§Safety and Behavior
- Read-Only: This function only traverses and reads directory metadata. It does not modify, create, or delete any files.
- No Symlink Traversal: Symbolic links beneath
rootare treated as individual leaf items and are never followed. - Root Exclusion: The
rootdirectory itself is never included in the plan. - Unicode Paths: The normalized root and every scanned entry path must be
representable as Unicode, regardless of whether an entry would be kept or deleted.
Link targets beneath
rootare not checked because those links are not followed. - Observed Entries: The plan contains entries observed during traversal. The traversal is not atomic and does not lock or prevent concurrent filesystem changes. The plan is not refreshed if the filesystem changes after construction.
§Errors
Returns an error in the following circumstances (exact error message strings are not part of the stable API contract):
- Pattern errors:
patternsis empty, a pattern contains invalid glob syntax, or a pattern escapesroot(e.g., starts with../or resolves to an ancestor). - Root errors:
rootcannot be resolved, is not a directory, is a filesystem root, or is a Windows drive-relative path. - I/O errors: Directory traversal fails due to filesystem permissions or I/O errors.
- Path encoding errors: The normalized root or a scanned entry path cannot be represented as Unicode. No partial plan is returned, even if all entries would be kept.
Pattern and root validation errors carry a ValidationError that can be detected with
anyhow::Error::downcast_ref. Traversal and path encoding errors do not.
§Examples
use remove_except::build_plan;
use std::fs::{self, File};
let temp_dir = assert_fs::TempDir::new().unwrap();
let root = temp_dir.path();
fs::create_dir_all(root.join("docs")).unwrap();
File::create(root.join("docs/guide.md")).unwrap();
File::create(root.join("temp.log")).unwrap();
let plan = build_plan(root, &["docs/**".to_owned()])
.expect("plan construction should succeed");
assert_eq!(plan.direct_match_count(), 1);
assert!(plan.keep_items().iter().any(|item| item.relative_path() == "docs/guide.md"));
assert!(plan.delete_roots().iter().any(|item| item.relative_path() == "temp.log"));