mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-05 02:55:31 +00:00
refac: simplify error treatment
This commit is contained in:
parent
2f7c7e8ff2
commit
7ea0625860
@ -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;
|
||||
|
@ -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())
|
||||
|
@ -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`.
|
||||
|
@ -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`.
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user