Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refac: simplify error treatment #753

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/archive/rar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ use std::path::Path;

use unrar::Archive;

use crate::{error::Error, list::FileInArchive, utils::logger::info};
use crate::{
error::{Error, Result},
list::FileInArchive,
utils::logger::info,
};

/// Unpacks the archive given by `archive_path` into the folder given by `output_folder`.
/// Assumes that output_folder is empty
Expand Down Expand Up @@ -48,15 +52,13 @@ pub fn unpack_archive(
pub fn list_archive(
archive_path: &Path,
password: Option<&[u8]>,
) -> crate::Result<impl Iterator<Item = crate::Result<FileInArchive>>> {
) -> Result<impl Iterator<Item = Result<FileInArchive>>> {
let archive = match password {
Some(password) => Archive::with_password(archive_path, password),
None => Archive::new(archive_path),
};

let archive = archive.open_for_listing()?;

Ok(archive.map(|item| {
Ok(archive.open_for_listing()?.map(|item| {
let item = item?;
let is_dir = item.is_directory();
let path = item.filename;
Expand Down
14 changes: 5 additions & 9 deletions src/archive/sevenz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use same_file::Handle;
use sevenz_rust::SevenZArchiveEntry;

use crate::{
error::{Error, FinalError},
error::{Error, FinalError, Result},
list::FileInArchive,
utils::{
cd_into_same_dir_as,
Expand Down Expand Up @@ -174,7 +174,7 @@ where
pub fn list_archive(
archive_path: &Path,
password: Option<&[u8]>,
) -> crate::Result<impl Iterator<Item = crate::Result<FileInArchive>>> {
) -> Result<impl Iterator<Item = crate::Result<FileInArchive>>> {
let reader = fs::File::open(archive_path)?;

let mut files = Vec::new();
Expand All @@ -187,7 +187,7 @@ pub fn list_archive(
Ok(true)
};

let result = match password {
match password {
Some(password) => {
let password = match password.to_str() {
Ok(p) => p,
Expand All @@ -202,13 +202,9 @@ pub fn list_archive(
".",
sevenz_rust::Password::from(password),
entry_extract_fn,
)
)?;
}
None => sevenz_rust::decompress_with_extract_fn(reader, ".", entry_extract_fn),
};

if let Err(e) = result {
return Err(e.into());
None => sevenz_rust::decompress_with_extract_fn(reader, ".", entry_extract_fn)?,
}

Ok(files.into_iter())
Expand Down
4 changes: 2 additions & 2 deletions src/archive/tar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn unpack_archive(reader: Box<dyn Read>, output_folder: &Path, quiet: bool)
/// List contents of `archive`, returning a vector of archive entries
pub fn list_archive(
mut archive: tar::Archive<impl Read + Send + 'static>,
) -> crate::Result<impl Iterator<Item = crate::Result<FileInArchive>>> {
) -> impl Iterator<Item = crate::Result<FileInArchive>> {
struct Files(Receiver<crate::Result<FileInArchive>>);
impl Iterator for Files {
type Item = crate::Result<FileInArchive>;
Expand All @@ -77,7 +77,7 @@ pub fn list_archive(
}
});

Ok(Files(rx))
Files(rx)
}

/// Compresses the archives given by `input_filenames` into the file given previously to `writer`.
Expand Down
4 changes: 2 additions & 2 deletions src/archive/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ where
pub fn list_archive<R>(
mut archive: ZipArchive<R>,
password: Option<&[u8]>,
) -> crate::Result<impl Iterator<Item = crate::Result<FileInArchive>>>
) -> impl Iterator<Item = crate::Result<FileInArchive>>
where
R: Read + Seek + Send + 'static,
{
Expand Down Expand Up @@ -145,7 +145,7 @@ where
}
});

Ok(Files(rx))
Files(rx)
}

/// Compresses the archives given by `input_filenames` into the file given previously to `writer`.
Expand Down
10 changes: 5 additions & 5 deletions src/commands/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn list_archive_contents(
// Any other Zip decompression done can take up the whole RAM and freeze ouch.
if let &[Zip] = formats.as_slice() {
let zip_archive = zip::ZipArchive::new(reader)?;
let files = crate::archive::zip::list_archive(zip_archive, password)?;
let files = crate::archive::zip::list_archive(zip_archive, password);
list::list_files(archive_path, files, list_options)?;

return Ok(());
Expand Down Expand Up @@ -65,7 +65,7 @@ pub fn list_archive_contents(
}

let files: Box<dyn Iterator<Item = crate::Result<FileInArchive>>> = match formats[0] {
Tar => Box::new(crate::archive::tar::list_archive(tar::Archive::new(reader))?),
Tar => Box::new(crate::archive::tar::list_archive(tar::Archive::new(reader))),
Zip => {
if formats.len() > 1 {
// Locking necessary to guarantee that warning and question
Expand All @@ -82,7 +82,7 @@ pub fn list_archive_contents(
io::copy(&mut reader, &mut vec)?;
let zip_archive = zip::ZipArchive::new(io::Cursor::new(vec))?;

Box::new(crate::archive::zip::list_archive(zip_archive, password)?)
Box::new(crate::archive::zip::list_archive(zip_archive, password))
}
#[cfg(feature = "unrar")]
Rar => {
Expand Down Expand Up @@ -116,6 +116,6 @@ pub fn list_archive_contents(
panic!("Not an archive! This should never happen, if it does, something is wrong with `CompressionFormat::is_archive()`. Please report this error!");
}
};
list::list_files(archive_path, files, list_options)?;
Ok(())

list::list_files(archive_path, files, list_options)
}