From b40e010b599210b08f849dadd31e9c29ba32efa7 Mon Sep 17 00:00:00 2001 From: Pedro de Oliveira Date: Wed, 10 May 2023 22:00:57 +0100 Subject: [PATCH] Change the path argument to a PathBuf, and adjust the code accordingly. --- src/lib.rs | 12 ++++++------ src/main.rs | 3 ++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3d268d1..a199765 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,9 +6,9 @@ use std::path::{Path, PathBuf}; /// /// # Arguments /// -/// * `path` - The path to the directory. +/// * `path` - A reference to a PathBuf representing the path to the directory. /// * `wildcard` - The wildcard pattern to match against file names. -pub fn get_file_list(path: &str, wildcard: &str) -> Vec { +pub fn get_file_list(path: &PathBuf, wildcard: &str) -> Vec { let pattern = Path::new(path).join(wildcard); let dir_entries: Vec> = glob(&pattern.to_string_lossy()).unwrap().collect(); dir_entries @@ -108,16 +108,16 @@ fn full_name_test() { #[test] fn file_list_test() { - const DIRECTORY: &str = "."; + let directory = PathBuf::from("."); const WILDCARD: &str = "*.toml"; - assert_eq!(get_file_list(DIRECTORY, WILDCARD), vec!["Cargo.toml"]); + assert_eq!(get_file_list(&directory, WILDCARD), vec!["Cargo.toml"]); } #[test] fn empty_file_list_test() { - const DIRECTORY: &str = "src"; + let directory = PathBuf::from("src"); const WILDCARD: &str = "*.exe"; - assert_eq!(get_file_list(DIRECTORY, WILDCARD), Vec::::new()); + assert_eq!(get_file_list(&directory, WILDCARD), Vec::::new()); } diff --git a/src/main.rs b/src/main.rs index 70776d7..fd88171 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ use clap::Parser; use rmv::{get_file_list, replace_name}; +use std::path::PathBuf; /// Command line parser with clap #[derive(Parser)] @@ -15,7 +16,7 @@ use rmv::{get_file_list, replace_name}; struct Args { /// Path to the directory where the files to be renamed are located. #[arg(short = 'i', long)] - path: String, + path: PathBuf, /// Regular expression pattern to match against the file names in the directory. ///