Merge pull request #100 from figsoda/cleanup

Minor cleanups and refactors
This commit is contained in:
João Marcos Bezerra 2021-10-18 02:01:02 -03:00 committed by GitHub
commit 9d7c8a9abb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 11 additions and 38 deletions

View File

@ -1,13 +1,8 @@
#[macro_export]
macro_rules! info {
($writer:expr, $($arg:tt)*) => {
use crate::macros::_info_helper;
_info_helper();
println!($writer, $($arg)*);
};
($writer:expr) => {
_info_helper();
println!($writer);
($($arg:tt)*) => {
$crate::macros::_info_helper();
println!($($arg)*);
};
}

View File

@ -1,23 +1,11 @@
/// Util function to skip the two leading long flag hyphens.
pub fn trim_double_hyphen(flag_text: &str) -> &str {
let mut chars = flag_text.chars();
chars.nth(1); // Skipping 2 chars
chars.as_str()
}
// Currently unused
/// Util function to skip the single leading short flag hyphen.
pub fn trim_single_hyphen(flag_text: &str) -> &str {
let mut chars = flag_text.chars();
chars.next(); // Skipping 1 char
chars.as_str()
flag_text.get(2..).unwrap_or_default()
}
#[cfg(test)]
mod tests {
use super::trim_double_hyphen;
use super::trim_single_hyphen;
#[test]
fn _trim_double_hyphen() {
@ -25,9 +13,4 @@ mod tests {
assert_eq!(trim_double_hyphen("--verbose"), "verbose");
assert_eq!(trim_double_hyphen("--help"), "help");
}
fn _trim_single_hyphen() {
assert_eq!(trim_single_hyphen("-vv"), "vv");
assert_eq!(trim_single_hyphen("-h"), "h");
}
}

View File

@ -11,7 +11,7 @@ use crate::{dialogs::Confirmation, info, oof};
pub fn dir_is_empty(dir_path: &Path) -> bool {
let is_empty = |mut rd: ReadDir| rd.next().is_none();
dir_path.read_dir().ok().map(is_empty).unwrap_or_default()
dir_path.read_dir().map(is_empty).unwrap_or_default()
}
pub fn create_dir_if_non_existent(path: &Path) -> crate::Result<()> {
@ -45,12 +45,7 @@ pub fn user_wants_to_overwrite(path: &Path, flags: &oof::Flags) -> crate::Result
_ => {}
}
let file_path_str = to_utf(path);
const OVERWRITE_CONFIRMATION_QUESTION: Confirmation =
Confirmation::new("Do you want to overwrite 'FILE'?", Some("FILE"));
OVERWRITE_CONFIRMATION_QUESTION.ask(Some(&file_path_str))
Confirmation::new("Do you want to overwrite 'FILE'?", Some("FILE")).ask(Some(&to_utf(path)))
}
pub fn to_utf(os_str: impl AsRef<OsStr>) -> String {

View File

@ -118,9 +118,9 @@ fn test_compressing_and_decompressing_archive(format: &str) {
(0..quantity_of_files).map(|_| generate_random_file_content(&mut rng)).collect();
// Create them
let mut file_paths = create_files(&testing_dir_path, &contents_of_files);
let mut file_paths = create_files(testing_dir_path, &contents_of_files);
// Compress them
let compressed_archive_path = compress_files(&testing_dir_path, &file_paths, &format);
let compressed_archive_path = compress_files(testing_dir_path, &file_paths, format);
// Decompress them
let mut extracted_paths = extract_files(&compressed_archive_path);

View File

@ -20,11 +20,11 @@ fn test_compress_decompress_with_empty_dir(format: &str) {
let testing_dir_path = testing_dir.path();
let empty_dir_path: PathBuf = create_empty_dir(&testing_dir_path, "dummy_empty_dir_name");
let empty_dir_path: PathBuf = create_empty_dir(testing_dir_path, "dummy_empty_dir_name");
let mut file_paths: Vec<PathBuf> = vec![empty_dir_path];
let compressed_archive_path: PathBuf = compress_files(&testing_dir_path, &file_paths, &format);
let compressed_archive_path: PathBuf = compress_files(testing_dir_path, &file_paths, format);
let mut extracted_paths = extract_files(&compressed_archive_path);

View File

@ -22,7 +22,7 @@ pub fn compress_files(at: &Path, paths_to_compress: &[PathBuf], format: &str) ->
let archive_path = String::from("archive.") + format;
let archive_path = at.join(archive_path);
let command = Command::Compress { files: paths_to_compress.to_vec(), output_path: archive_path.to_path_buf() };
let command = Command::Compress { files: paths_to_compress.to_vec(), output_path: archive_path.clone() };
run(command, &oof::Flags::default()).expect("Failed to compress test dummy files");
archive_path