From 7ea062586048f1e6cd6c6eaa25e18810652d6cf9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jo=C3=A3o=20Marcos=20P=2E=20Bezerra?= Date: Mon, 18 Nov 2024 00:43:19 -0300 Subject: [PATCH] refac: simplify error treatment --- src/archive/rar.rs | 12 +++++++----- src/archive/sevenz.rs | 14 +++++--------- src/archive/tar.rs | 4 ++-- src/archive/zip.rs | 4 ++-- src/commands/list.rs | 10 +++++----- 5 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/archive/rar.rs b/src/archive/rar.rs index ce43266..c223192 100644 --- a/src/archive/rar.rs +++ b/src/archive/rar.rs @@ -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>> { +) -> Result>> { 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; diff --git a/src/archive/sevenz.rs b/src/archive/sevenz.rs index 6ee345d..8898f5f 100644 --- a/src/archive/sevenz.rs +++ b/src/archive/sevenz.rs @@ -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>> { +) -> Result>> { 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()) diff --git a/src/archive/tar.rs b/src/archive/tar.rs index d20aa7d..fda05eb 100644 --- a/src/archive/tar.rs +++ b/src/archive/tar.rs @@ -54,7 +54,7 @@ pub fn unpack_archive(reader: Box, output_folder: &Path, quiet: bool) /// List contents of `archive`, returning a vector of archive entries pub fn list_archive( mut archive: tar::Archive, -) -> crate::Result>> { +) -> impl Iterator> { struct Files(Receiver>); impl Iterator for Files { type Item = crate::Result; @@ -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`. diff --git a/src/archive/zip.rs b/src/archive/zip.rs index af20950..9995e07 100644 --- a/src/archive/zip.rs +++ b/src/archive/zip.rs @@ -105,7 +105,7 @@ where pub fn list_archive( mut archive: ZipArchive, password: Option<&[u8]>, -) -> crate::Result>> +) -> impl Iterator> 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`. diff --git a/src/commands/list.rs b/src/commands/list.rs index 4d87a8c..1821d6d 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -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>> = 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) } -- 2.11.4.GIT