remove-except 0.5.1

A Rust CLI to keep selected paths or glob matches and remove everything else under a root.
Documentation
use std::path::{Path, PathBuf};
use std::time::SystemTime;

use anyhow::Result;

use crate::error::invalid;

/// A half-open range of modification instants.
///
/// The lower bound is inclusive and the upper bound is exclusive. Bounds retain the
/// precision and range of the supplied `SystemTime` values.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModifiedRange {
    since: Option<SystemTime>,
    before: Option<SystemTime>,
}

impl ModifiedRange {
    /// Constructs a range with at least one bound.
    ///
    /// Returns a [`crate::ValidationError`] if both bounds are absent or the lower
    /// bound is greater than or equal to the upper bound.
    pub fn new(since: Option<SystemTime>, before: Option<SystemTime>) -> Result<Self> {
        if since.is_none() && before.is_none() {
            return Err(invalid("At least one modification-time bound is required"));
        }
        if let (Some(since), Some(before)) = (since, before)
            && since >= before
        {
            return Err(invalid("Modification-time start must precede the end"));
        }
        Ok(Self { since, before })
    }

    /// Returns the inclusive lower bound, if specified.
    pub fn since(&self) -> Option<SystemTime> {
        self.since
    }

    /// Returns the exclusive upper bound, if specified.
    pub fn before(&self) -> Option<SystemTime> {
        self.before
    }

    pub(crate) fn contains(&self, time: SystemTime) -> bool {
        self.since.is_none_or(|since| time >= since)
            && self.before.is_none_or(|before| time < before)
    }
}

/// Optional conditions used while generating a removal plan.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct PlanOptions {
    modified_range: Option<ModifiedRange>,
}

impl PlanOptions {
    /// Constructs options with a modification-time range.
    pub fn new(modified_range: ModifiedRange) -> Self {
        Self {
            modified_range: Some(modified_range),
        }
    }

    /// Returns the modification-time range, if specified.
    pub fn modified_range(&self) -> Option<&ModifiedRange> {
        self.modified_range.as_ref()
    }
}

/// The type of a filesystem item found during traversal.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ItemType {
    /// An item other than a directory or symbolic link, including special files.
    File,
    /// A directory.
    Directory,
    /// A symbolic link. Symbolic links are evaluated as leaf items and never traversed.
    Symlink,
}

/// A filesystem item observed during the traversal that created a removal plan.
///
/// The traversal is not atomic. Instances are not automatically updated or revalidated if
/// filesystem contents change during or after planning.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PlannedItem {
    /// Absolute path to the item, resolved under the normalized root.
    /// On Windows, the canonicalized path may include the extended-path prefix `\\?\`.
    pub(crate) path: PathBuf,
    /// Path relative to the root, using `/` separators.
    pub(crate) relative_path: String,
    /// The type of this item.
    pub(crate) item_type: ItemType,
}

impl PlannedItem {
    /// Returns the absolute path to the item, resolved under the normalized root.
    ///
    /// On Windows, the canonicalized path may include the extended-length path prefix (`\\?\`).
    pub fn path(&self) -> &Path {
        &self.path
    }

    /// Returns the item's path relative to the processing root, always normalized with `/` separators.
    pub fn relative_path(&self) -> &str {
        &self.relative_path
    }

    /// Returns the [`ItemType`] classification of this item.
    pub fn item_type(&self) -> ItemType {
        self.item_type
    }
}

/// An in-memory plan containing items to keep or delete beneath a root directory.
///
/// Created by [`crate::build_plan`] or [`crate::build_plan_with_options`]. Constructing or querying a `RemovalPlan` is strictly
/// read-only and never modifies the filesystem.
///
/// # Item Collections
///
/// A `RemovalPlan` separates discovered filesystem items into three collections:
///
/// - [`keep_items`](RemovalPlan::keep_items): Contains all items directly matched by keep
///   conditions, plus all ancestor directories required to preserve their paths from the root.
/// - [`delete_items`](RemovalPlan::delete_items): Contains every individual item targeted for
///   deletion. This comprehensive list is suitable for full previews, logging, or reporting.
/// - [`delete_roots`](RemovalPlan::delete_roots): Contains only the top-level deletion targets.
///   When an entire directory is targeted for deletion, its descendants appear in `delete_items`
///   but are excluded from `delete_roots`. This minimal set is ideal for executing deletion via
///   recursive removal routines without redundant operations.
#[derive(Debug)]
pub struct RemovalPlan {
    /// The normalized traversal root. The root itself is not included in the plan.
    pub(crate) root: PathBuf,
    /// Number of items directly matching all keep conditions; ancestors alone are not counted.
    pub(crate) direct_match_count: usize,
    pub(crate) modified_range: Option<ModifiedRange>,
    /// Items to keep and their ancestors, sorted by relative path.
    pub(crate) keep_items: Vec<PlannedItem>,
    /// All deletion targets, sorted by relative path.
    pub(crate) delete_items: Vec<PlannedItem>,
    /// Top-level items used for deletion. Descendants of deletion-target directories are excluded.
    pub(crate) delete_roots: Vec<PlannedItem>,
}

impl RemovalPlan {
    /// Returns the normalized root directory of the plan.
    ///
    /// The root directory itself is never included in any keep or delete list.
    pub fn root(&self) -> &Path {
        &self.root
    }

    /// Returns the number of items that directly matched all keep conditions.
    ///
    /// Directories kept solely because they are ancestors of matched items are not
    /// included in this count.
    pub fn direct_match_count(&self) -> usize {
        self.direct_match_count
    }

    /// Returns the modification-time range used to create the plan, if specified.
    pub fn modified_range(&self) -> Option<&ModifiedRange> {
        self.modified_range.as_ref()
    }

    /// Returns all kept items and their required ancestor directories, sorted by relative path.
    pub fn keep_items(&self) -> &[PlannedItem] {
        &self.keep_items
    }

    /// Returns all items targeted for deletion, sorted by relative path.
    ///
    /// This is the complete, exhaustive list of all deletion targets, including items inside
    /// directories that will be removed.
    pub fn delete_items(&self) -> &[PlannedItem] {
        &self.delete_items
    }

    /// Returns the minimal top-level items targeted for deletion, sorted by relative path.
    ///
    /// If a directory is targeted for deletion, the directory itself is included in this list,
    /// but its child items are omitted. This provides the minimal set of targets needed to
    /// execute recursive deletion safely and efficiently.
    pub fn delete_roots(&self) -> &[PlannedItem] {
        &self.delete_roots
    }
}