Skip to content

Commit d04807b

Browse files
committed
improved usability of the Action enum (#470)
1 parent 88c4a57 commit d04807b

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

git-diff/src/tree/visit.rs

+6
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ pub enum Action {
4242
Cancel,
4343
}
4444

45+
impl Default for Action {
46+
fn default() -> Self {
47+
Action::Continue
48+
}
49+
}
50+
4551
impl Action {
4652
/// Returns true if this action means to stop the traversal.
4753
pub fn cancelled(&self) -> bool {

git-repository/src/object/tree/mod.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,21 @@ pub mod diff {
8181
ForEach(#[source] Box<dyn std::error::Error + Send + Sync + 'static>),
8282
}
8383

84+
/// Returned by the `for_each` function to control flow.
85+
#[derive(Clone, Copy, PartialOrd, PartialEq, Ord, Eq, Hash)]
86+
pub enum Action {
87+
/// Continue the traversal of changes.
88+
Continue,
89+
/// Stop the traversal of changes and stop calling this function.
90+
Cancel,
91+
}
92+
93+
impl Default for Action {
94+
fn default() -> Self {
95+
Action::Continue
96+
}
97+
}
98+
8499
/// Represents any possible change in order to turn one tree into another.
85100
#[derive(Debug, Clone, Copy)]
86101
pub struct Change<'a, 'repo, 'other_repo> {
@@ -164,7 +179,7 @@ pub mod diff {
164179
pub fn for_each_to_obtain_tree<'other_repo, E>(
165180
&mut self,
166181
other: &Tree<'other_repo>,
167-
for_each: impl FnMut(Change<'_, 'repo, 'other_repo>) -> Result<git_diff::tree::visit::Action, E>,
182+
for_each: impl FnMut(Change<'_, 'repo, 'other_repo>) -> Result<Action, E>,
168183
) -> Result<(), Error>
169184
where
170185
E: std::error::Error + Sync + Send + 'static,
@@ -202,8 +217,7 @@ pub mod diff {
202217

203218
impl<'repo, 'other_repo, VisitFn, E> git_diff::tree::Visit for Delegate<'repo, 'other_repo, VisitFn, E>
204219
where
205-
VisitFn:
206-
for<'delegate> FnMut(Change<'delegate, 'repo, 'other_repo>) -> Result<git_diff::tree::visit::Action, E>,
220+
VisitFn: for<'delegate> FnMut(Change<'delegate, 'repo, 'other_repo>) -> Result<Action, E>,
207221
E: std::error::Error + Sync + Send + 'static,
208222
{
209223
fn pop_front_tracked_path_and_set_current(&mut self) {}
@@ -251,7 +265,8 @@ pub mod diff {
251265
event,
252266
location: self.location.as_ref(),
253267
}) {
254-
Ok(action) => action,
268+
Ok(Action::Cancel) => git_diff::tree::visit::Action::Cancel,
269+
Ok(Action::Continue) => git_diff::tree::visit::Action::Continue,
255270
Err(err) => {
256271
self.err = Some(err);
257272
git_diff::tree::visit::Action::Cancel

git-repository/tests/object/tree.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ mod diff {
2525
assert_eq!(entry_mode, EntryMode::Blob);
2626
assert_eq!(previous_id.object().unwrap().data.as_bstr(), "g\n");
2727
assert_eq!(id.object().unwrap().data.as_bstr(), "h\n");
28-
Ok(git::diff::tree::visit::Action::Continue)
28+
Ok(Default::default())
2929
}
3030
Event::Deletion { .. } | Event::Addition { .. } => unreachable!("only modification is expected"),
3131
}
@@ -36,7 +36,7 @@ mod diff {
3636
.track_filename()
3737
.for_each_to_obtain_tree(&to, |change| -> Result<_, Infallible> {
3838
assert_eq!(change.location, "file");
39-
Ok(git::diff::tree::visit::Action::Continue)
39+
Ok(git::object::tree::diff::Action::Continue)
4040
})
4141
.unwrap();
4242
}

0 commit comments

Comments
 (0)