remove_except/model.rs
1use std::path::{Path, PathBuf};
2
3/// The type of a filesystem item found during traversal.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum ItemType {
6 /// A regular file.
7 File,
8 /// A directory.
9 Directory,
10 /// A symbolic link. Symbolic links are evaluated as leaf items and never traversed.
11 Symlink,
12}
13
14/// A filesystem item observed during the traversal that created a removal plan.
15///
16/// The traversal is not atomic. Instances are not automatically updated or revalidated if
17/// filesystem contents change during or after planning.
18#[derive(Debug, Clone, PartialEq, Eq)]
19pub struct PlannedItem {
20 /// Absolute path to the item, resolved under the normalized root.
21 /// On Windows, the canonicalized path may include the extended-path prefix `\\?\`.
22 pub(crate) path: PathBuf,
23 /// Path relative to the root, using `/` separators.
24 pub(crate) relative_path: String,
25 /// The type of this item.
26 pub(crate) item_type: ItemType,
27}
28
29impl PlannedItem {
30 /// Returns the absolute path to the item, resolved under the normalized root.
31 ///
32 /// On Windows, the canonicalized path may include the extended-length path prefix (`\\?\`).
33 pub fn path(&self) -> &Path {
34 &self.path
35 }
36
37 /// Returns the item's path relative to the processing root, always normalized with `/` separators.
38 pub fn relative_path(&self) -> &str {
39 &self.relative_path
40 }
41
42 /// Returns the [`ItemType`] classification of this item.
43 pub fn item_type(&self) -> ItemType {
44 self.item_type
45 }
46}
47
48/// An in-memory plan containing items to keep or delete beneath a root directory.
49///
50/// Created by [`crate::build_plan`]. Constructing or querying a `RemovalPlan` is strictly
51/// read-only and never modifies the filesystem.
52///
53/// # Item Collections
54///
55/// A `RemovalPlan` separates discovered filesystem items into three collections:
56///
57/// - [`keep_items`](RemovalPlan::keep_items): Contains all items directly matched by keep
58/// patterns, plus all ancestor directories required to preserve their paths from the root.
59/// - [`delete_items`](RemovalPlan::delete_items): Contains every individual item targeted for
60/// deletion. This comprehensive list is suitable for full previews, logging, or reporting.
61/// - [`delete_roots`](RemovalPlan::delete_roots): Contains only the top-level deletion targets.
62/// When an entire directory is targeted for deletion, its descendants appear in `delete_items`
63/// but are excluded from `delete_roots`. This minimal set is ideal for executing deletion via
64/// recursive removal routines without redundant operations.
65#[derive(Debug)]
66pub struct RemovalPlan {
67 /// The normalized traversal root. The root itself is not included in the plan.
68 pub(crate) root: PathBuf,
69 /// Number of items directly matched by keep patterns; ancestors alone are not counted.
70 pub(crate) direct_match_count: usize,
71 /// Items to keep and their ancestors, sorted by relative path.
72 pub(crate) keep_items: Vec<PlannedItem>,
73 /// All deletion targets, sorted by relative path.
74 pub(crate) delete_items: Vec<PlannedItem>,
75 /// Top-level items used for deletion. Descendants of deletion-target directories are excluded.
76 pub(crate) delete_roots: Vec<PlannedItem>,
77}
78
79impl RemovalPlan {
80 /// Returns the normalized root directory of the plan.
81 ///
82 /// The root directory itself is never included in any keep or delete list.
83 pub fn root(&self) -> &Path {
84 &self.root
85 }
86
87 /// Returns the number of items that directly matched one or more keep patterns.
88 ///
89 /// Directories kept solely because they are ancestors of matched items are not
90 /// included in this count.
91 pub fn direct_match_count(&self) -> usize {
92 self.direct_match_count
93 }
94
95 /// Returns all kept items and their required ancestor directories, sorted by relative path.
96 pub fn keep_items(&self) -> &[PlannedItem] {
97 &self.keep_items
98 }
99
100 /// Returns all items targeted for deletion, sorted by relative path.
101 ///
102 /// This is the complete, exhaustive list of all deletion targets, including items inside
103 /// directories that will be removed.
104 pub fn delete_items(&self) -> &[PlannedItem] {
105 &self.delete_items
106 }
107
108 /// Returns the minimal top-level items targeted for deletion, sorted by relative path.
109 ///
110 /// If a directory is targeted for deletion, the directory itself is included in this list,
111 /// but its child items are omitted. This provides the minimal set of targets needed to
112 /// execute recursive deletion safely and efficiently.
113 pub fn delete_roots(&self) -> &[PlannedItem] {
114 &self.delete_roots
115 }
116}