Simplify extract_compressed_from_readable impl

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-06-11 20:16:41 +10:00
parent 57b40d809e
commit 5a43ee2681
No known key found for this signature in database
GPG key ID: 591C0B03040416D6
2 changed files with 10 additions and 10 deletions

View file

@ -219,7 +219,7 @@ where
extract_impl(stream, move |mut rx| { extract_impl(stream, move |mut rx| {
fs::create_dir_all(path.parent().unwrap())?; fs::create_dir_all(path.parent().unwrap())?;
extract_compressed_from_readable(ReadableRx::new(&mut rx), fmt.into(), &path, filter) extract_compressed_from_readable(ReadableRx::new(&mut rx), fmt, &path, filter)
}) })
.await .await
} }
@ -239,7 +239,7 @@ where
extract_compressed_from_readable::<fn(&Path) -> bool, _>( extract_compressed_from_readable::<fn(&Path) -> bool, _>(
ReadableRx::new(&mut rx), ReadableRx::new(&mut rx),
fmt.into(), fmt,
&path, &path,
None, None,
) )

View file

@ -9,7 +9,7 @@ use xz2::bufread::XzDecoder;
use zip::read::ZipArchive; use zip::read::ZipArchive;
use zstd::stream::Decoder as ZstdDecoder; use zstd::stream::Decoder as ZstdDecoder;
use crate::{BinstallError, PkgFmt}; use crate::{BinstallError, TarBasedFmt};
/// * `filter` - If Some, then it will pass the path of the file to it /// * `filter` - If Some, then it will pass the path of the file to it
/// and only extract ones which filter returns `true`. /// and only extract ones which filter returns `true`.
@ -58,32 +58,34 @@ fn untar<Filter: FnMut(&Path) -> bool>(
/// is not `PkgFmt::Bin` or `PkgFmt::Zip`. /// is not `PkgFmt::Bin` or `PkgFmt::Zip`.
pub(crate) fn extract_compressed_from_readable<Filter: FnMut(&Path) -> bool, R: BufRead>( pub(crate) fn extract_compressed_from_readable<Filter: FnMut(&Path) -> bool, R: BufRead>(
dat: R, dat: R,
fmt: PkgFmt, fmt: TarBasedFmt,
path: &Path, path: &Path,
filter: Option<Filter>, filter: Option<Filter>,
) -> Result<(), BinstallError> { ) -> Result<(), BinstallError> {
use TarBasedFmt::*;
match fmt { match fmt {
PkgFmt::Tar => { Tar => {
// Extract to install dir // Extract to install dir
debug!("Extracting from tar archive to `{path:?}`"); debug!("Extracting from tar archive to `{path:?}`");
untar(dat, path, filter)? untar(dat, path, filter)?
} }
PkgFmt::Tgz => { Tgz => {
// Extract to install dir // Extract to install dir
debug!("Decompressing from tgz archive to `{path:?}`"); debug!("Decompressing from tgz archive to `{path:?}`");
let tar = GzDecoder::new(dat); let tar = GzDecoder::new(dat);
untar(tar, path, filter)?; untar(tar, path, filter)?;
} }
PkgFmt::Txz => { Txz => {
// Extract to install dir // Extract to install dir
debug!("Decompressing from txz archive to `{path:?}`"); debug!("Decompressing from txz archive to `{path:?}`");
let tar = XzDecoder::new(dat); let tar = XzDecoder::new(dat);
untar(tar, path, filter)?; untar(tar, path, filter)?;
} }
PkgFmt::Tzstd => { Tzstd => {
// Extract to install dir // Extract to install dir
debug!("Decompressing from tzstd archive to `{path:?}`"); debug!("Decompressing from tzstd archive to `{path:?}`");
@ -94,8 +96,6 @@ pub(crate) fn extract_compressed_from_readable<Filter: FnMut(&Path) -> bool, R:
let tar = ZstdDecoder::with_buffer(dat)?; let tar = ZstdDecoder::with_buffer(dat)?;
untar(tar, path, filter)?; untar(tar, path, filter)?;
} }
PkgFmt::Zip => panic!("Unexpected PkgFmt::Zip!"),
PkgFmt::Bin => panic!("Unexpected PkgFmt::Bin!"),
}; };
Ok(()) Ok(())