From 39cef75dfec06dc11815e44c7558d964fe72be57 Mon Sep 17 00:00:00 2001 From: ttyS3 Date: Fri, 6 Sep 2024 15:41:03 +0000 Subject: [PATCH] fix(error): return result in list_archive Refactor list_archive functions to return results directly for better error handling. --- src/archive/rar.rs | 11 +++++++---- src/archive/sevenz.rs | 18 ++++++------------ src/commands/list.rs | 6 +++--- src/error.rs | 4 ++-- 4 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/archive/rar.rs b/src/archive/rar.rs index a482618..38c3941 100644 --- a/src/archive/rar.rs +++ b/src/archive/rar.rs @@ -48,12 +48,13 @@ pub fn unpack_archive( pub fn list_archive( archive_path: &Path, password: Option<&[u8]>, -) -> impl Iterator> { +) -> crate::Result>> { let archive = match password { Some(password) => Archive::with_password(archive_path, password), None => Archive::new(archive_path), }; - match archive.open_for_listing() { + + let result = match archive.open_for_listing() { Ok(iter) => iter.map(|item| { let item = item?; let is_dir = item.is_directory(); @@ -61,8 +62,10 @@ pub fn list_archive( Ok(FileInArchive { path, is_dir }) }).collect::>().into_iter(), - Err(e) => vec![Err(Error::UnrarError{reason: e.to_string()})].into_iter(), - } + Err(e) => return Err(Error::UnrarError{reason: e.to_string()}), + }; + + Ok(result) } pub fn no_compression() -> Error { diff --git a/src/archive/sevenz.rs b/src/archive/sevenz.rs index 774b379..05a28c3 100644 --- a/src/archive/sevenz.rs +++ b/src/archive/sevenz.rs @@ -167,7 +167,7 @@ where reader, output_path, sevenz_rust::Password::from(password.to_str().map_err(|_| { - Error::InvalidPassword("7z requires that all passwords are valid UTF-8") + Error::InvalidPassword{reason: "7z requires that all passwords are valid UTF-8".to_string()} })?), entry_extract_fn, )?, @@ -181,14 +181,8 @@ where pub fn list_archive( archive_path: &Path, password: Option<&[u8]>, -) -> impl Iterator> { - let reader = fs::File::open(archive_path); - - if let Err(e) = reader { - return vec![Err(Error::IoError {reason:e.to_string()})].into_iter(); - } - - let reader = reader.unwrap(); +) -> crate::Result>> { + let reader = fs::File::open(archive_path)?; let mut files = Vec::new(); @@ -204,7 +198,7 @@ pub fn list_archive( Some(password) => { let password = match password.to_str() { Ok(p) => p, - Err(_) => return vec![Err(Error::InvalidPassword("7z requires that all passwords are valid UTF-8"))].into_iter(), + Err(err) => return Err(Error::InvalidPassword{ reason: err.to_string()}), }; sevenz_rust::decompress_with_extract_fn_and_password( reader, @@ -217,8 +211,8 @@ pub fn list_archive( }; if let Err(e) = result { - return vec![Err(e.into())].into_iter(); + return Err(e.into()); } - files.into_iter() + Ok(files.into_iter()) } diff --git a/src/commands/list.rs b/src/commands/list.rs index 922ca33..a2cdfc9 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -88,9 +88,9 @@ pub fn list_archive_contents( if formats.len() > 1 { let mut temp_file = tempfile::NamedTempFile::new()?; io::copy(&mut reader, &mut temp_file)?; - Box::new(crate::archive::rar::list_archive(temp_file.path(), password)) + Box::new(crate::archive::rar::list_archive(temp_file.path(), password)?) } else { - Box::new(crate::archive::rar::list_archive(archive_path, password)) + Box::new(crate::archive::rar::list_archive(archive_path, password)?) } } #[cfg(not(feature = "unrar"))] @@ -109,7 +109,7 @@ pub fn list_archive_contents( } } - Box::new(sevenz::list_archive(archive_path, password)) + Box::new(sevenz::list_archive(archive_path, password)?) } Gzip | Bzip | Lz4 | Lzma | Snappy | Zstd => { panic!("Not an archive! This should never happen, if it does, something is wrong with `CompressionFormat::is_archive()`. Please report this error!"); diff --git a/src/error.rs b/src/error.rs index a8b6004..3853c52 100644 --- a/src/error.rs +++ b/src/error.rs @@ -40,7 +40,7 @@ pub enum Error { // currently only RAR when built without the `unrar` feature UnsupportedFormat { reason: String }, /// Invalid password provided - InvalidPassword(&'static str), + InvalidPassword { reason: String }, /// UnrarError From unrar::error::UnrarError UnrarError { reason: String }, } @@ -152,7 +152,7 @@ impl fmt::Display for Error { Error::UnsupportedFormat { reason } => { FinalError::with_title("Recognised but unsupported format").detail(reason.clone()) } - Error::InvalidPassword(reason) => FinalError::with_title("Invalid password").detail(*reason), + Error::InvalidPassword{reason} => FinalError::with_title("Invalid password").detail(reason.clone()), Error::UnrarError{reason} => FinalError::with_title("Unrar error").detail(reason.clone()), };