refactoring: Extract function (#116)

This commit is contained in:
TATSUNO Yasuhiro 2021-10-20 13:10:10 +09:00 committed by GitHub
parent 5f7d777342
commit 4404b91a23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 4 deletions

View File

@ -3,7 +3,6 @@
use std::{ use std::{
env, fs, env, fs,
io::{self, prelude::*}, io::{self, prelude::*},
path::Component,
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
@ -12,7 +11,7 @@ use zip::{self, read::ZipFile, ZipArchive};
use crate::{ use crate::{
info, oof, info, oof,
utils::{self, dir_is_empty, Bytes}, utils::{self, dir_is_empty, strip_cur_dir, Bytes},
}; };
use self::utf8::get_invalid_utf8_paths; use self::utf8::get_invalid_utf8_paths;
@ -48,7 +47,7 @@ where
fs::create_dir_all(&path)?; 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())); info!("{:?} extracted. ({})", file_path.display(), Bytes::new(file.size()));

View File

@ -23,6 +23,13 @@ pub fn create_dir_if_non_existent(path: &Path) -> crate::Result<()> {
Ok(()) 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 /// Changes the process' current directory to the directory that contains the
/// file pointed to by `filename` and returns the directory that the process /// file pointed to by `filename` and returns the directory that the process
/// was in before this function was called. /// was in before this function was called.
@ -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))) Confirmation::new("Do you want to overwrite 'FILE'?", Some("FILE")).ask(Some(&to_utf(path)))
} }