-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): Dry run mode (
--dry-run
flag) (#156)
This change adds a new flag `--dry-run` that when given, prevents otherwise destructive file overwrite operations and instead prints a rich diff. The diff contains names of files which would be modified, and a git-like (word) diff of what modifications *would* be made. The diff is human-readable, and not designed to be easily machine-readable. It leverages the existing search mode. Closes #152.
- Loading branch information
Showing
16 changed files
with
497 additions
and
42 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/// Extension trait that adds parallel zipping functionality to iterators over iterators. | ||
pub trait ParallelZipExt: Iterator { | ||
/// Zips multiple iterators in parallel, such that the nth invocation yields a | ||
/// [`Vec`] of all nth items of the subiterators. | ||
fn parallel_zip(self) -> ParallelZip<Self::Item> | ||
where | ||
Self: Sized, | ||
Self::Item: Iterator; | ||
} | ||
|
||
/// An iterator similar to [`std::iter::zip`], but instead it zips over *multiple | ||
/// iterators* in parallel, such that the nth invocation yields a [`Vec`] of all | ||
/// nth items of its subiterators. | ||
#[derive(Debug)] | ||
pub struct ParallelZip<I>(Vec<I>); | ||
|
||
impl<I: Iterator> Iterator for ParallelZip<I> { | ||
type Item = Vec<I::Item>; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
if self.0.is_empty() { | ||
return None; | ||
} | ||
|
||
self.0.iter_mut().map(Iterator::next).collect() | ||
} | ||
} | ||
|
||
// Implement the extension trait for any iterator whose items are themselves iterators | ||
impl<T> ParallelZipExt for T | ||
where | ||
T: Iterator, | ||
T::Item: Iterator, | ||
{ | ||
fn parallel_zip(self) -> ParallelZip<T::Item> { | ||
ParallelZip(self.collect()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use itertools::Itertools; | ||
use rstest::rstest; | ||
|
||
use super::*; | ||
|
||
#[rstest] | ||
#[case::empty_once( | ||
Vec::<Vec<i32>>::new(), | ||
Vec::new(), | ||
)] | ||
#[case::empty_twice( | ||
vec![ | ||
vec![], | ||
vec![], | ||
], | ||
vec![], | ||
)] | ||
#[case::zips_to_shortest( | ||
vec![ | ||
vec![0, 1, 2], | ||
vec![3, 4], | ||
], | ||
vec![ | ||
vec![0, 3], | ||
vec![1, 4], | ||
] | ||
)] | ||
#[case::base_case( | ||
vec![ | ||
vec![1, 2], | ||
vec![3, 4], | ||
vec![5, 6] | ||
], | ||
vec![ | ||
vec![1, 3, 5], | ||
vec![2, 4, 6] | ||
] | ||
)] | ||
#[case::transpose_horizontal( | ||
vec![ | ||
vec![1, 2, 3], | ||
], | ||
vec![ | ||
vec![1], | ||
vec![2], | ||
vec![3], | ||
] | ||
)] | ||
#[case::transpose_vertical( | ||
vec![ | ||
vec![1], | ||
vec![2], | ||
vec![3] | ||
], | ||
vec![ | ||
vec![1, 2, 3] | ||
] | ||
)] | ||
fn test_parallel_zip(#[case] input: Vec<Vec<i32>>, #[case] expected: Vec<Vec<i32>>) { | ||
let iters = input.into_iter().map(IntoIterator::into_iter).collect_vec(); | ||
let res = iters.into_iter().parallel_zip().collect_vec(); | ||
assert_eq!(res, expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.