From 9e8214d7426facae8add74c9f763e9947a6bcc63 Mon Sep 17 00:00:00 2001 From: Steppy Date: Fri, 13 Dec 2024 13:07:52 +0100 Subject: [PATCH] Use replace action instead of explicit delete action where possible --- src/main.rs | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9e18302..ebf1d83 100644 --- a/src/main.rs +++ b/src/main.rs @@ -161,14 +161,29 @@ fn main() -> Result<(), String> { }) .chain(once(add_action)) .collect(), - OldVersionPolicy::Delete => files - .filter(|file| file_matcher.matches(file)) - .map(|file| FileAction { - file: PathBuf::from(file), - kind: Action::Delete, - }) - .chain(once(add_action)) - .collect(), + OldVersionPolicy::Delete => { + let mut actions: Vec<_> = files + .filter(|file| file_matcher.matches(file)) + .map(|file| { + //special case -> file has the same name as current file, then we just need to replace it + if file == file_name { + FileAction { + file: PathBuf::from(file), + kind: Action::Replace, + } + } else { + FileAction { + file: PathBuf::from(file), + kind: Action::Delete, + } + } + }) + .collect(); + if !actions.iter().any(|action| action.kind == Action::Replace) { + actions.push(add_action); + } + actions + }, } }, })