Skip to content

Commit

Permalink
refactor: update method signatures to accept PathBuf directly instead…
Browse files Browse the repository at this point in the history
… of &PathBuf
  • Loading branch information
tamada committed Jan 26, 2025
1 parent 503e90e commit ab8e0c2
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 44 deletions.
21 changes: 10 additions & 11 deletions src/extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl PathUtils<'_> {
self.e.base_dir()
}

fn destination(&self, target: PathBuf) -> Result<PathBuf> {
fn destination<P: AsRef<Path>>(&self, target: P) -> Result<PathBuf> {
self.e.destination(target)
}
}
Expand Down Expand Up @@ -121,7 +121,7 @@ impl Extractor {
Ok(e) => e,
Err(e) => return Err(e),
};
extractor.list(&self.archive_file)
extractor.list(self.archive_file.clone())
}

/// Execute extraction of the archive file.
Expand All @@ -131,7 +131,7 @@ impl Extractor {
Err(e) => return Err(e),
};
match self.can_extract() {
Ok(_) => extractor.perform(&self.archive_file, PathUtils { e: self }),
Ok(_) => extractor.perform(self.archive_file.clone(), PathUtils { e: &self }),
Err(e) => Err(e),
}
}
Expand All @@ -151,8 +151,7 @@ impl Extractor {
fn base_dir(&self) -> PathBuf {
if self.use_archive_name_dir {
if let Some(stem) = self.archive_file.file_stem() {
let dir_name = stem.to_str().unwrap();
self.destination.join(dir_name)
self.destination.join(stem)
} else {
self.destination.clone()
}
Expand All @@ -162,7 +161,7 @@ impl Extractor {
}

/// Return the path of the `target` file for output.
fn destination(&self, target: PathBuf) -> Result<PathBuf> {
fn destination<P: AsRef<Path>>(&self, target: P) -> Result<PathBuf> {
let base = self.base_dir();
let dest = base.join(target);
if dest.exists() && !self.overwrite {
Expand All @@ -187,9 +186,9 @@ impl Extractor {
/// The trait for extracting the archive file.
pub(crate) trait ToteExtractor {
/// returns the entry list of the given archive file.
fn list(&self, archive_file: &PathBuf) -> Result<Vec<Entry>>;
fn list(&self, archive_file: PathBuf) -> Result<Vec<Entry>>;
/// extract the given archive file into the specified directory with the given options.
fn perform(&self, archive_file: &PathBuf, opts: PathUtils) -> Result<()>;
fn perform(&self, archive_file: PathBuf, opts: PathUtils) -> Result<()>;
#[cfg(test)]
/// returns the supported format of the extractor.
fn format(&self) -> Format;
Expand Down Expand Up @@ -232,17 +231,17 @@ mod tests {
.use_archive_name_dir(true)
.build();
assert_eq!(opts1.base_dir(), PathBuf::from("./archive"));
if let Ok(t) = opts1.destination("text1.txt".into()) {
if let Ok(t) = opts1.destination("text1.txt") {
assert_eq!(t, PathBuf::from("./archive/text1.txt"));
}
if let Ok(t) = opts1.destination("text2.txt".into()) {
if let Ok(t) = opts1.destination("text2.txt") {
assert_eq!(t, PathBuf::from("./archive/text2.txt"));
}

let archive_file = PathBuf::from("/tmp/archive.zip");
let opts2 = Extractor::builder().archive_file(archive_file).build();
assert_eq!(opts2.base_dir(), PathBuf::from("."));
if let Ok(t) = opts2.destination("./text1.txt".into()) {
if let Ok(t) = opts2.destination("./text1.txt") {
assert_eq!(t, PathBuf::from("./text1.txt"));
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/extractor/cab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ use crate::{Result, ToteError};
pub(super) struct CabExtractor {}

impl ToteExtractor for CabExtractor {
fn list(&self, target: &PathBuf) -> Result<Vec<Entry>> {
list_impl(target, convert)
fn list(&self, target: PathBuf) -> Result<Vec<Entry>> {
list_impl(&target, convert)
}

fn perform(&self, target: &PathBuf, opts: PathUtils) -> Result<()> {
let list = match list_impl(target, |file| {
fn perform(&self, target: PathBuf, opts: PathUtils) -> Result<()> {
let list = match list_impl(&target, |file| {
(file.name().to_string(), file.uncompressed_size())
}) {
Ok(l) => l,
Err(e) => return Err(e),
};
let mut errs = vec![];
let mut cabinet = open_cabinet(target)?;
let mut cabinet = open_cabinet(&target)?;
for file in list {
if let Err(e) = write_file_impl(&mut cabinet, file, &opts) {
errs.push(e);
Expand Down Expand Up @@ -116,7 +116,7 @@ mod tests {
fn test_list_archives() {
let file = PathBuf::from("testdata/test.cab");
let extractor = CabExtractor {};
match extractor.list(&file) {
match extractor.list(file) {
Ok(r) => {
let r = r.iter().map(|e| e.name.clone()).collect::<Vec<_>>();
assert_eq!(r.len(), 16);
Expand Down
6 changes: 3 additions & 3 deletions src/extractor/lha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{Result, ToteError};
pub(super) struct LhaExtractor {}

impl ToteExtractor for LhaExtractor {
fn list(&self, archive_file: &PathBuf) -> Result<Vec<Entry>> {
fn list(&self, archive_file: PathBuf) -> Result<Vec<Entry>> {
let mut result = vec![];
let mut reader = match delharc::parse_file(archive_file) {
Err(e) => return Err(ToteError::IO(e)),
Expand All @@ -35,7 +35,7 @@ impl ToteExtractor for LhaExtractor {
Ok(result)
}

fn perform(&self, archive_file: &PathBuf, opts: PathUtils) -> Result<()> {
fn perform(&self, archive_file: PathBuf, opts: PathUtils) -> Result<()> {
let mut reader = match delharc::parse_file(archive_file) {
Err(e) => return Err(ToteError::IO(e)),
Ok(h) => h,
Expand Down Expand Up @@ -123,7 +123,7 @@ mod tests {
fn test_list_archives() {
let file = PathBuf::from("testdata/test.lzh");
let extractor = LhaExtractor {};
match extractor.list(&file) {
match extractor.list(file) {
Ok(r) => {
let r = r.iter().map(|e| e.name.clone()).collect::<Vec<_>>();
assert_eq!(r.len(), 23);
Expand Down
6 changes: 3 additions & 3 deletions src/extractor/rar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::extractor::{Entry, PathUtils, ToteExtractor};
pub(super) struct RarExtractor {}

impl ToteExtractor for RarExtractor {
fn list(&self, archive_file: &PathBuf) -> Result<Vec<Entry>> {
fn list(&self, archive_file: PathBuf) -> Result<Vec<Entry>> {
let mut r = vec![];
for entry in unrar::Archive::new(&archive_file)
.open_for_listing()
Expand All @@ -23,7 +23,7 @@ impl ToteExtractor for RarExtractor {
Ok(r)
}

fn perform(&self, archive_file: &PathBuf, opts: PathUtils) -> Result<()> {
fn perform(&self, archive_file: PathBuf, opts: PathUtils) -> Result<()> {
let archive = unrar::Archive::new(&archive_file);
let mut file = archive.open_for_processing().unwrap();
while let Some(header) = file.read_header().unwrap() {
Expand Down Expand Up @@ -79,7 +79,7 @@ mod tests {
fn test_list_archives() {
let extractor = RarExtractor {};
let file = PathBuf::from("testdata/test.rar");
match extractor.list(&file) {
match extractor.list(file) {
Ok(r) => {
let r = r.iter().map(|e| e.name.clone()).collect::<Vec<_>>();
assert_eq!(r.len(), 18);
Expand Down
6 changes: 3 additions & 3 deletions src/extractor/sevenz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::extractor::{Entry, PathUtils, ToteExtractor};
pub(super) struct SevenZExtractor {}

impl ToteExtractor for SevenZExtractor {
fn list(&self, archive_file: &PathBuf) -> Result<Vec<Entry>> {
fn list(&self, archive_file: PathBuf) -> Result<Vec<Entry>> {
let mut reader = File::open(archive_file).unwrap();
let len = reader.metadata().unwrap().len();
match Archive::read(&mut reader, len, Password::empty().as_ref()) {
Expand All @@ -25,7 +25,7 @@ impl ToteExtractor for SevenZExtractor {
}
}

fn perform(&self, archive_file: &PathBuf, opts: PathUtils) -> Result<()> {
fn perform(&self, archive_file: PathBuf, opts: PathUtils) -> Result<()> {
let file = match File::open(archive_file) {
Ok(file) => file,
Err(e) => return Err(ToteError::IO(e)),
Expand Down Expand Up @@ -83,7 +83,7 @@ mod tests {
fn test_list() {
let file = PathBuf::from("testdata/test.7z");
let extractor = SevenZExtractor {};
match extractor.list(&file) {
match extractor.list(file) {
Ok(r) => {
let r = r.iter().map(|e| e.name.clone()).collect::<Vec<_>>();
assert_eq!(r.len(), 21);
Expand Down
30 changes: 15 additions & 15 deletions src/extractor/tar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ pub(super) struct TarXzExtractor {}
pub(super) struct TarZstdExtractor {}

impl ToteExtractor for TarExtractor {
fn list(&self, archive_file: &PathBuf) -> Result<Vec<ToteEntry>> {
fn list(&self, archive_file: PathBuf) -> Result<Vec<ToteEntry>> {
match open_tar_file(archive_file, |f| f) {
Ok(archive) => list_tar(archive),
Err(e) => Err(e),
}
}
fn perform(&self, archive_file: &PathBuf, opts: PathUtils) -> Result<()> {
fn perform(&self, archive_file: PathBuf, opts: PathUtils) -> Result<()> {
match open_tar_file(archive_file, |f| f) {
Err(e) => Err(e),
Ok(archive) => extract_tar(archive, opts),
Expand All @@ -40,13 +40,13 @@ impl ToteExtractor for TarExtractor {
}

impl ToteExtractor for TarGzExtractor {
fn list(&self, archive_file: &PathBuf) -> Result<Vec<ToteEntry>> {
fn list(&self, archive_file: PathBuf) -> Result<Vec<ToteEntry>> {
match open_tar_file(archive_file, flate2::read::GzDecoder::new) {
Ok(archive) => list_tar(archive),
Err(e) => Err(e),
}
}
fn perform(&self, archive_file: &PathBuf, opts: PathUtils) -> Result<()> {
fn perform(&self, archive_file: PathBuf, opts: PathUtils) -> Result<()> {
match open_tar_file(archive_file, flate2::read::GzDecoder::new) {
Ok(archive) => extract_tar(archive, opts),
Err(e) => Err(e),
Expand All @@ -59,13 +59,13 @@ impl ToteExtractor for TarGzExtractor {
}

impl ToteExtractor for TarBz2Extractor {
fn list(&self, archive_file: &PathBuf) -> Result<Vec<ToteEntry>> {
fn list(&self, archive_file: PathBuf) -> Result<Vec<ToteEntry>> {
match open_tar_file(archive_file, bzip2::read::BzDecoder::new) {
Ok(archive) => list_tar(archive),
Err(e) => Err(e),
}
}
fn perform(&self, archive_file: &PathBuf, opts: PathUtils) -> Result<()> {
fn perform(&self, archive_file: PathBuf, opts: PathUtils) -> Result<()> {
match open_tar_file(archive_file, bzip2::read::BzDecoder::new) {
Err(e) => Err(e),
Ok(archive) => extract_tar(archive, opts),
Expand All @@ -78,13 +78,13 @@ impl ToteExtractor for TarBz2Extractor {
}

impl ToteExtractor for TarXzExtractor {
fn list(&self, archive_file: &PathBuf) -> Result<Vec<ToteEntry>> {
fn list(&self, archive_file: PathBuf) -> Result<Vec<ToteEntry>> {
match open_tar_file(archive_file, XzDecoder::new) {
Err(e) => Err(e),
Ok(archive) => list_tar(archive),
}
}
fn perform(&self, archive_file: &PathBuf, opts: PathUtils) -> Result<()> {
fn perform(&self, archive_file: PathBuf, opts: PathUtils) -> Result<()> {
match open_tar_file(archive_file, XzDecoder::new) {
Err(e) => Err(e),
Ok(archive) => extract_tar(archive, opts),
Expand All @@ -97,13 +97,13 @@ impl ToteExtractor for TarXzExtractor {
}

impl ToteExtractor for TarZstdExtractor {
fn list(&self, archive_file: &PathBuf) -> Result<Vec<ToteEntry>> {
fn list(&self, archive_file: PathBuf) -> Result<Vec<ToteEntry>> {
match open_tar_file(archive_file, |f| zstd::Decoder::new(f).unwrap()) {
Err(e) => Err(e),
Ok(archive) => list_tar(archive),
}
}
fn perform(&self, archive_file: &PathBuf, opts: PathUtils) -> Result<()> {
fn perform(&self, archive_file: PathBuf, opts: PathUtils) -> Result<()> {
match open_tar_file(archive_file, |f| zstd::Decoder::new(f).unwrap()) {
Err(e) => Err(e),
Ok(archive) => extract_tar(archive, opts),
Expand All @@ -115,7 +115,7 @@ impl ToteExtractor for TarZstdExtractor {
}
}

fn open_tar_file<F, R: Read>(file: &PathBuf, opener: F) -> Result<Archive<R>>
fn open_tar_file<F, R: Read>(file: PathBuf, opener: F) -> Result<Archive<R>>
where
F: FnOnce(File) -> R,
{
Expand Down Expand Up @@ -186,7 +186,7 @@ mod tests {
fn test_list_tar_file() {
let file = PathBuf::from("testdata/test.tar");
let extractor = TarExtractor {};
match extractor.list(&file) {
match extractor.list(file) {
Ok(r) => {
let r = r.iter().map(|e| e.name.clone()).collect::<Vec<_>>();
assert_eq!(r.len(), 16);
Expand Down Expand Up @@ -220,7 +220,7 @@ mod tests {
fn test_list_tarbz2_file() {
let file = PathBuf::from("testdata/test.tar.bz2");
let extractor = TarBz2Extractor {};
match extractor.list(&file) {
match extractor.list(file) {
Ok(r) => {
let r = r.iter().map(|e| e.name.clone()).collect::<Vec<_>>();
assert_eq!(r.len(), 16);
Expand All @@ -237,7 +237,7 @@ mod tests {
fn test_list_targz_file() {
let file = PathBuf::from("testdata/test.tar.gz");
let extractor = TarGzExtractor {};
match extractor.list(&file) {
match extractor.list(file) {
Ok(r) => {
let r = r.iter().map(|e| e.name.clone()).collect::<Vec<_>>();
assert_eq!(r.len(), 16);
Expand All @@ -254,7 +254,7 @@ mod tests {
fn test_list_tarzstd_file() {
let file = PathBuf::from("testdata/test.tar.zst");
let extractor = TarZstdExtractor {};
match extractor.list(&file) {
match extractor.list(file) {
Ok(r) => {
let r = r.iter().map(|e| e.name.clone()).collect::<Vec<_>>();
assert_eq!(r.len(), 16);
Expand Down
6 changes: 3 additions & 3 deletions src/extractor/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::Result;
pub(super) struct ZipExtractor {}

impl ToteExtractor for ZipExtractor {
fn list(&self, archive_file: &PathBuf) -> Result<Vec<Entry>> {
fn list(&self, archive_file: PathBuf) -> Result<Vec<Entry>> {
let zip_file = File::open(archive_file).unwrap();
let mut zip = zip::ZipArchive::new(zip_file).unwrap();

Expand All @@ -23,7 +23,7 @@ impl ToteExtractor for ZipExtractor {
Ok(result)
}

fn perform(&self, archive_file: &PathBuf, opts: PathUtils) -> Result<()> {
fn perform(&self, archive_file: PathBuf, opts: PathUtils) -> Result<()> {
let zip_file = File::open(archive_file).unwrap();
let mut zip = zip::ZipArchive::new(zip_file).unwrap();
for i in 0..zip.len() {
Expand Down Expand Up @@ -89,7 +89,7 @@ mod tests {
fn test_list_archives() {
let file = PathBuf::from("testdata/test.zip");
let extractor = ZipExtractor {};
match extractor.list(&file) {
match extractor.list(file) {
Ok(r) => {
assert_eq!(r.len(), 19);
assert_eq!(
Expand Down

0 comments on commit ab8e0c2

Please sign in to comment.