From 8ded5ae03bfe434a4f99c49c43b6f8bd75a81f25 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Wed, 16 Aug 2023 16:08:40 -0700 Subject: [PATCH] working_copy: convert `Diff` into `Options` for matching This just a little refactoring to make the next step of sharing code between `Modified` and `Added` simpler. --- lib/src/working_copy.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/src/working_copy.rs b/lib/src/working_copy.rs index a627628aa..c30a937ac 100644 --- a/lib/src/working_copy.rs +++ b/lib/src/working_copy.rs @@ -1211,8 +1211,8 @@ impl TreeState { let disk_path = path.to_fs_path(&self.working_copy_path); // TODO: Check that the file has not changed before overwriting/removing it. - match diff { - Diff::Removed(_before) => { + match diff.into_options() { + (Some(_before), None) => { fs::remove_file(&disk_path).ok(); let mut parent_dir = disk_path.parent().unwrap(); loop { @@ -1224,7 +1224,7 @@ impl TreeState { self.file_states.remove(&path); stats.removed_files += 1; } - Diff::Added(after) => { + (None, Some(after)) => { let file_state = match after { TreeValue::File { id, executable } => { self.write_file(&disk_path, &path, &id, executable)? @@ -1242,12 +1242,12 @@ impl TreeState { self.file_states.insert(path, file_state); stats.added_files += 1; } - Diff::Modified( - TreeValue::File { + ( + Some(TreeValue::File { id: old_id, executable: old_executable, - }, - TreeValue::File { id, executable }, + }), + Some(TreeValue::File { id, executable }), ) if id == old_id => { // Optimization for when only the executable bit changed assert_ne!(executable, old_executable); @@ -1259,7 +1259,7 @@ impl TreeState { } stats.updated_files += 1; } - Diff::Modified(_before, after) => { + (Some(_before), Some(after)) => { fs::remove_file(&disk_path).ok(); let file_state = match after { TreeValue::File { id, executable } => { @@ -1279,6 +1279,9 @@ impl TreeState { self.file_states.insert(path, file_state); stats.updated_files += 1; } + (None, None) => { + unreachable!() + } } Ok(()) };