Skip to content

Commit

Permalink
Merge pull request #267 from figsoda/clippy
Browse files Browse the repository at this point in the history
Apply clippy lints and simplify smart_unpack
  • Loading branch information
marcospb19 authored Aug 14, 2022
2 parents 72295d8 + e9ae311 commit e377dd3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ Categories Used:
- CI: Check the format with Github Action [\#126](https://github.com/ouch-org/ouch/pull/126) ([dcariotti](https://github.com/dcariotti))
- CI: Rewrite [\#135](https://github.com/ouch-org/ouch/pull/135) ([figsoda](https://github.com/figsoda))
- Improving error messages and removing dead error treatment code [\#140](https://github.com/ouch-org/ouch/pull/140) ([marcospb19](https://github.com/marcospb19))
- Apply clippy lints and simplify smart_unpack [\#267](https://github.com/ouch-org/ouch/pull/267) ([figsoda](https://github.com/figsoda))

### Tweaks

Expand Down
50 changes: 15 additions & 35 deletions src/commands/decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,8 @@ pub fn decompress_file(
{
let zip_archive = zip::ZipArchive::new(reader)?;
let files = if let ControlFlow::Continue(files) = smart_unpack(
Box::new(move |output_dir| {
let mut progress = Progress::new_accessible_aware(total_input_size, true, None);
crate::archive::zip::unpack_archive(
zip_archive,
output_dir,
progress
.as_mut()
.map(Progress::display_handle)
.unwrap_or(&mut io::stdout()),
)
}),
|output_dir, progress| crate::archive::zip::unpack_archive(zip_archive, output_dir, progress),
total_input_size,
output_dir,
&output_file_path,
question_policy,
Expand Down Expand Up @@ -130,17 +121,8 @@ pub fn decompress_file(
}
Tar => {
if let ControlFlow::Continue(files) = smart_unpack(
Box::new(move |output_dir| {
let mut progress = Progress::new_accessible_aware(total_input_size, true, None);
crate::archive::tar::unpack_archive(
reader,
output_dir,
progress
.as_mut()
.map(Progress::display_handle)
.unwrap_or(&mut io::stdout()),
)
}),
|output_dir, progress| crate::archive::tar::unpack_archive(reader, output_dir, progress),
total_input_size,
output_dir,
&output_file_path,
question_policy,
Expand All @@ -165,17 +147,8 @@ pub fn decompress_file(
let zip_archive = zip::ZipArchive::new(io::Cursor::new(vec))?;

if let ControlFlow::Continue(files) = smart_unpack(
Box::new(move |output_dir| {
let mut progress = Progress::new_accessible_aware(total_input_size, true, None);
crate::archive::zip::unpack_archive(
zip_archive,
output_dir,
progress
.as_mut()
.map(Progress::display_handle)
.unwrap_or(&mut io::stdout()),
)
}),
|output_dir, progress| crate::archive::zip::unpack_archive(zip_archive, output_dir, progress),
total_input_size,
output_dir,
&output_file_path,
question_policy,
Expand Down Expand Up @@ -207,7 +180,8 @@ pub fn decompress_file(
/// output_dir named after the archive (given by `output_file_path`)
/// Note: This functions assumes that `output_dir` exists
fn smart_unpack(
unpack_fn: Box<dyn FnOnce(&Path) -> crate::Result<Vec<PathBuf>>>,
unpack_fn: impl FnOnce(&Path, &mut dyn Write) -> crate::Result<Vec<PathBuf>>,
total_input_size: u64,
output_dir: &Path,
output_file_path: &Path,
question_policy: QuestionPolicy,
Expand All @@ -222,7 +196,13 @@ fn smart_unpack(
);

// unpack the files
let files = unpack_fn(temp_dir_path)?;
let files = unpack_fn(
temp_dir_path,
Progress::new_accessible_aware(total_input_size, true, None)
.as_mut()
.map(Progress::display_handle)
.unwrap_or(&mut io::stdout()),
)?;

let root_contains_only_one_element = fs::read_dir(&temp_dir_path)?.count() == 1;
if root_contains_only_one_element {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ mod utf8 {
pub fn get_invalid_utf8_paths(paths: &[PathBuf]) -> Vec<&PathBuf> {
paths
.iter()
.filter_map(|path| is_invalid_utf8(path).then(|| path))
.filter_map(|path| is_invalid_utf8(path).then_some(path))
.collect()
}
}

0 comments on commit e377dd3

Please sign in to comment.