Skip to main content

fren_date/plan/
sort.rs

1//! Bottom-up sort invariant.
2//!
3//! The executor processes plans in deepest-first order so that a plan's
4//! `original_path` is still valid at execution time (no parent has been
5//! renamed yet). At the same depth, files are processed before
6//! directories so that within the same parent the dir rename happens
7//! last, after its children have already been renamed.
8
9use crate::RenamePlan;
10use std::cmp::Reverse;
11
12use crate::plan_types::ItemKind;
13
14/// Sort a plan vector in-place: deepest first; at equal depth, files
15/// before directories.
16pub fn sort_bottom_up(plans: &mut [RenamePlan]) {
17    plans.sort_by_key(|p| {
18        let kind_order = match p.kind {
19            ItemKind::File | ItemKind::Symlink => 0,
20            ItemKind::Dir => 1,
21        };
22        (Reverse(p.depth), kind_order)
23    });
24}