Skip to content

Commit

Permalink
improved usability of the Action enum (#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Sep 19, 2022
1 parent 88c4a57 commit d04807b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
6 changes: 6 additions & 0 deletions git-diff/src/tree/visit.rs
Expand Up @@ -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 {
Expand Down
23 changes: 19 additions & 4 deletions git-repository/src/object/tree/mod.rs
Expand Up @@ -81,6 +81,21 @@ pub mod diff {
ForEach(#[source] Box<dyn std::error::Error + Send + Sync + 'static>),
}

/// 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> {
Expand Down Expand Up @@ -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<git_diff::tree::visit::Action, E>,
for_each: impl FnMut(Change<'_, 'repo, 'other_repo>) -> Result<Action, E>,
) -> Result<(), Error>
where
E: std::error::Error + Sync + Send + 'static,
Expand Down Expand Up @@ -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<git_diff::tree::visit::Action, E>,
VisitFn: for<'delegate> FnMut(Change<'delegate, 'repo, 'other_repo>) -> Result<Action, E>,
E: std::error::Error + Sync + Send + 'static,
{
fn pop_front_tracked_path_and_set_current(&mut self) {}
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions git-repository/tests/object/tree.rs
Expand Up @@ -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"),
}
Expand All @@ -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();
}
Expand Down

0 comments on commit d04807b

Please sign in to comment.