refac: simplify error treatment

This commit is contained in:
João Marcos P. Bezerra 2024-11-18 00:43:19 -03:00
parent df6d2cea98
commit 4a35472e34
5 changed files with 21 additions and 23 deletions

View File

@ -4,7 +4,11 @@ use std::path::Path;
use unrar::Archive; 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`. /// Unpacks the archive given by `archive_path` into the folder given by `output_folder`.
/// Assumes that output_folder is empty /// Assumes that output_folder is empty
@ -48,15 +52,13 @@ pub fn unpack_archive(
pub fn list_archive( pub fn list_archive(
archive_path: &Path, archive_path: &Path,
password: Option<&[u8]>, password: Option<&[u8]>,
) -> crate::Result<impl Iterator<Item = crate::Result<FileInArchive>>> { ) -> Result<impl Iterator<Item = Result<FileInArchive>>> {
let archive = match password { let archive = match password {
Some(password) => Archive::with_password(archive_path, password), Some(password) => Archive::with_password(archive_path, password),
None => Archive::new(archive_path), None => Archive::new(archive_path),
}; };
let archive = archive.open_for_listing()?; Ok(archive.open_for_listing()?.map(|item| {
Ok(archive.map(|item| {
let item = item?; let item = item?;
let is_dir = item.is_directory(); let is_dir = item.is_directory();
let path = item.filename; let path = item.filename;

View File

@ -12,7 +12,7 @@ use same_file::Handle;
use sevenz_rust::SevenZArchiveEntry; use sevenz_rust::SevenZArchiveEntry;
use crate::{ use crate::{
error::{Error, FinalError}, error::{Error, FinalError, Result},
list::FileInArchive, list::FileInArchive,
utils::{ utils::{
cd_into_same_dir_as, cd_into_same_dir_as,
@ -174,7 +174,7 @@ where
pub fn list_archive( pub fn list_archive(
archive_path: &Path, archive_path: &Path,
password: Option<&[u8]>, 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 reader = fs::File::open(archive_path)?;
let mut files = Vec::new(); let mut files = Vec::new();
@ -187,7 +187,7 @@ pub fn list_archive(
Ok(true) Ok(true)
}; };
let result = match password { match password {
Some(password) => { Some(password) => {
let password = match password.to_str() { let password = match password.to_str() {
Ok(p) => p, Ok(p) => p,
@ -202,13 +202,9 @@ pub fn list_archive(
".", ".",
sevenz_rust::Password::from(password), sevenz_rust::Password::from(password),
entry_extract_fn, entry_extract_fn,
) )?;
} }
None => sevenz_rust::decompress_with_extract_fn(reader, ".", entry_extract_fn), None => sevenz_rust::decompress_with_extract_fn(reader, ".", entry_extract_fn)?,
};
if let Err(e) = result {
return Err(e.into());
} }
Ok(files.into_iter()) Ok(files.into_iter())

View File

@ -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 /// List contents of `archive`, returning a vector of archive entries
pub fn list_archive( pub fn list_archive(
mut archive: tar::Archive<impl Read + Send + 'static>, 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>>); struct Files(Receiver<crate::Result<FileInArchive>>);
impl Iterator for Files { impl Iterator for Files {
type Item = crate::Result<FileInArchive>; 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`. /// Compresses the archives given by `input_filenames` into the file given previously to `writer`.

View File

@ -105,7 +105,7 @@ where
pub fn list_archive<R>( pub fn list_archive<R>(
mut archive: ZipArchive<R>, mut archive: ZipArchive<R>,
password: Option<&[u8]>, password: Option<&[u8]>,
) -> crate::Result<impl Iterator<Item = crate::Result<FileInArchive>>> ) -> impl Iterator<Item = crate::Result<FileInArchive>>
where where
R: Read + Seek + Send + 'static, 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`. /// Compresses the archives given by `input_filenames` into the file given previously to `writer`.

View File

@ -34,7 +34,7 @@ pub fn list_archive_contents(
// Any other Zip decompression done can take up the whole RAM and freeze ouch. // Any other Zip decompression done can take up the whole RAM and freeze ouch.
if let &[Zip] = formats.as_slice() { if let &[Zip] = formats.as_slice() {
let zip_archive = zip::ZipArchive::new(reader)?; 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)?; list::list_files(archive_path, files, list_options)?;
return Ok(()); return Ok(());
@ -65,7 +65,7 @@ pub fn list_archive_contents(
} }
let files: Box<dyn Iterator<Item = crate::Result<FileInArchive>>> = match formats[0] { 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 => { Zip => {
if formats.len() > 1 { if formats.len() > 1 {
// Locking necessary to guarantee that warning and question // Locking necessary to guarantee that warning and question
@ -82,7 +82,7 @@ pub fn list_archive_contents(
io::copy(&mut reader, &mut vec)?; io::copy(&mut reader, &mut vec)?;
let zip_archive = zip::ZipArchive::new(io::Cursor::new(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")] #[cfg(feature = "unrar")]
Rar => { 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!"); 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)
} }