Skip to content

Commit

Permalink
refac: simplify error treatment
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospb19 committed Nov 20, 2024
1 parent 2f7c7e8 commit 7ea0625
Showing 5 changed files with 21 additions and 23 deletions.
12 changes: 7 additions & 5 deletions src/archive/rar.rs
Original file line number Diff line number Diff line change
@@ -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
@@ -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;
14 changes: 5 additions & 9 deletions src/archive/sevenz.rs
Original file line number Diff line number Diff line change
@@ -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,
@@ -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();
@@ -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,
@@ -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())
4 changes: 2 additions & 2 deletions src/archive/tar.rs
Original file line number Diff line number Diff line change
@@ -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>;
@@ -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`.
4 changes: 2 additions & 2 deletions src/archive/zip.rs
Original file line number Diff line number Diff line change
@@ -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,
{
@@ -145,7 +145,7 @@ where
}
});

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

/// Compresses the archives given by `input_filenames` into the file given previously to `writer`.
10 changes: 5 additions & 5 deletions src/commands/list.rs
Original file line number Diff line number Diff line change
@@ -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(());
@@ -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
@@ -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 => {
@@ -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)
}

0 comments on commit 7ea0625

Please sign in to comment.