diff --git a/git-diff/src/tree/visit.rs b/git-diff/src/tree/visit.rs index b66e47b37c..a95f488be3 100644 --- a/git-diff/src/tree/visit.rs +++ b/git-diff/src/tree/visit.rs @@ -42,6 +42,12 @@ pub enum Action { Cancel, } +impl Default for Action { + fn default() -> Self { + Action::Continue + } +} + impl Action { /// Returns true if this action means to stop the traversal. pub fn cancelled(&self) -> bool { diff --git a/git-repository/src/object/tree/mod.rs b/git-repository/src/object/tree/mod.rs index 158f69765b..deb7e80f3d 100644 --- a/git-repository/src/object/tree/mod.rs +++ b/git-repository/src/object/tree/mod.rs @@ -81,6 +81,21 @@ pub mod diff { ForEach(#[source] Box), } + /// Returned by the `for_each` function to control flow. + #[derive(Clone, Copy, PartialOrd, PartialEq, Ord, Eq, Hash)] + pub enum Action { + /// Continue the traversal of changes. + Continue, + /// Stop the traversal of changes and stop calling this function. + Cancel, + } + + impl Default for Action { + fn default() -> Self { + Action::Continue + } + } + /// Represents any possible change in order to turn one tree into another. #[derive(Debug, Clone, Copy)] pub struct Change<'a, 'repo, 'other_repo> { @@ -164,7 +179,7 @@ pub mod diff { pub fn for_each_to_obtain_tree<'other_repo, E>( &mut self, other: &Tree<'other_repo>, - for_each: impl FnMut(Change<'_, 'repo, 'other_repo>) -> Result, + for_each: impl FnMut(Change<'_, 'repo, 'other_repo>) -> Result, ) -> Result<(), Error> where E: std::error::Error + Sync + Send + 'static, @@ -202,8 +217,7 @@ pub mod diff { impl<'repo, 'other_repo, VisitFn, E> git_diff::tree::Visit for Delegate<'repo, 'other_repo, VisitFn, E> where - VisitFn: - for<'delegate> FnMut(Change<'delegate, 'repo, 'other_repo>) -> Result, + VisitFn: for<'delegate> FnMut(Change<'delegate, 'repo, 'other_repo>) -> Result, E: std::error::Error + Sync + Send + 'static, { fn pop_front_tracked_path_and_set_current(&mut self) {} @@ -251,7 +265,8 @@ pub mod diff { event, location: self.location.as_ref(), }) { - Ok(action) => action, + Ok(Action::Cancel) => git_diff::tree::visit::Action::Cancel, + Ok(Action::Continue) => git_diff::tree::visit::Action::Continue, Err(err) => { self.err = Some(err); git_diff::tree::visit::Action::Cancel diff --git a/git-repository/tests/object/tree.rs b/git-repository/tests/object/tree.rs index 2e7c1676ee..989f3ea79d 100644 --- a/git-repository/tests/object/tree.rs +++ b/git-repository/tests/object/tree.rs @@ -25,7 +25,7 @@ mod diff { assert_eq!(entry_mode, EntryMode::Blob); assert_eq!(previous_id.object().unwrap().data.as_bstr(), "g\n"); assert_eq!(id.object().unwrap().data.as_bstr(), "h\n"); - Ok(git::diff::tree::visit::Action::Continue) + Ok(Default::default()) } Event::Deletion { .. } | Event::Addition { .. } => unreachable!("only modification is expected"), } @@ -36,7 +36,7 @@ mod diff { .track_filename() .for_each_to_obtain_tree(&to, |change| -> Result<_, Infallible> { assert_eq!(change.location, "file"); - Ok(git::diff::tree::visit::Action::Continue) + Ok(git::object::tree::diff::Action::Continue) }) .unwrap(); }