Skip to content

Commit

Permalink
refactoring: Extract function (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
exoego authored Oct 20, 2021
1 parent 5f7d777 commit 4404b91
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/archive/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use std::{
env, fs,
io::{self, prelude::*},
path::Component,
path::{Path, PathBuf},
};

Expand All @@ -12,7 +11,7 @@ use zip::{self, read::ZipFile, ZipArchive};

use crate::{
info, oof,
utils::{self, dir_is_empty, Bytes},
utils::{self, dir_is_empty, strip_cur_dir, Bytes},
};

use self::utf8::get_invalid_utf8_paths;
Expand Down Expand Up @@ -48,7 +47,7 @@ where
fs::create_dir_all(&path)?;
}
}
let file_path = file_path.strip_prefix(Component::CurDir).unwrap_or_else(|_| file_path.as_path());
let file_path = strip_cur_dir(file_path.as_path());

info!("{:?} extracted. ({})", file_path.display(), Bytes::new(file.size()));

Expand Down
9 changes: 8 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ pub fn create_dir_if_non_existent(path: &Path) -> crate::Result<()> {
Ok(())
}

pub fn strip_cur_dir(source_path: &Path) -> PathBuf {
source_path
.strip_prefix(Component::CurDir)
.map(|path| path.to_path_buf())
.unwrap_or_else(|_| source_path.to_path_buf())
}

/// Changes the process' current directory to the directory that contains the
/// file pointed to by `filename` and returns the directory that the process
/// was in before this function was called.
Expand All @@ -46,7 +53,7 @@ pub fn user_wants_to_overwrite(path: &Path, flags: &oof::Flags) -> crate::Result
_ => {}
}

let path = path.strip_prefix(Component::CurDir).unwrap_or_else(|_| path);
let path = strip_cur_dir(path);
Confirmation::new("Do you want to overwrite 'FILE'?", Some("FILE")).ask(Some(&to_utf(path)))
}

Expand Down

0 comments on commit 4404b91

Please sign in to comment.