Refactor to for-loop

This commit is contained in:
DJRHails 2020-05-19 12:40:19 +01:00 committed by David Peter
parent fef1b91feb
commit 42719624a8
1 changed files with 19 additions and 24 deletions

View File

@ -75,31 +75,26 @@ fn run() -> Result<ExitCode> {
.unwrap_or("");
// Get one or more root directories to search.
let mut dir_vec: Vec<_> = match matches
let passed_arguments = matches
.values_of_os("path")
.or_else(|| matches.values_of_os("search-path"))
{
Some(paths) => paths
.map(|path| {
let path_buffer = PathBuf::from(path);
if filesystem::is_dir(&path_buffer) {
Ok(path_buffer)
} else {
Err(anyhow!(
"Search path '{}' is not a directory.",
path_buffer.to_string_lossy()
))
}
})
.inspect(|res| {
if let Err(e) = res {
print_error(format!("{}", e))
}
})
.filter_map(Result::ok)
.collect::<Vec<_>>(),
None => vec![current_directory.to_path_buf()],
};
.or_else(|| matches.values_of_os("search-path"));
// Assign current directory to search vector.
let mut dir_vec: Vec<_> = vec![current_directory.to_path_buf()];
// Assign any valid arguments to search vector.
if let Some(paths) = passed_arguments {
dir_vec = vec![];
for path in paths {
let path_buffer = PathBuf::from(path);
if filesystem::is_dir(&path_buffer) {
dir_vec.push(path_buffer);
}
else {
print_error(format!("Search path '{}' is not a directory.", path_buffer.to_string_lossy()));
}
}
}
if matches.is_present("absolute-path") {
dir_vec = dir_vec