fix(error): return result in list_archive

Refactor list_archive functions to return results directly for better error handling.
This commit is contained in:
ttyS3 2024-09-06 15:41:03 +00:00 committed by João Marcos
parent 9e1c30bb86
commit 39cef75dfe
4 changed files with 18 additions and 21 deletions

View File

@ -48,12 +48,13 @@ pub fn unpack_archive(
pub fn list_archive(
archive_path: &Path,
password: Option<&[u8]>,
) -> impl Iterator<Item = crate::Result<FileInArchive>> {
) -> crate::Result<impl Iterator<Item = crate::Result<FileInArchive>>> {
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::<Vec<_>>().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 {

View File

@ -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<Item = crate::Result<FileInArchive>> {
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<impl Iterator<Item = crate::Result<FileInArchive>>> {
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())
}

View File

@ -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!");

View File

@ -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()),
};