Skip to content

Commit 46c224a

Browse files
committed
Optimising get_paths_matching_glob
1 parent 96ef605 commit 46c224a

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

src/main.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use std::str::FromStr;
1010

1111
//use crate::hasher::hash_file_crc32;
1212
use blake2::{Blake2b512, Blake2s256};
13-
use glob::GlobResult;
1413
use md5::Md5;
1514
use pico_args::Arguments;
1615
use rayon::prelude::*;
@@ -209,28 +208,20 @@ fn get_paths_matching_glob(config: &ConfigSettings) -> anyhow::Result<Vec<String
209208
require_literal_leading_dot: false,
210209
};
211210

212-
// we've already checked config.supplied_path is not None
213-
//assert!(config.supplied_path.is_some());
214-
215-
// have to clone to unwrap the string, because the struct is borrowed
216-
let pattern = config.supplied_path.clone().ok_or(anyhow::anyhow!(
211+
let pattern = config.supplied_path.as_ref().ok_or_else(|| anyhow::anyhow!(
217212
"Supplied path is None, but should have been Some"
218213
))?;
219214

220-
let temp_paths = glob::glob_with(&pattern, glob_settings)?;
221-
222-
// filter out non-files
223-
let path_globs: Vec<GlobResult> = temp_paths
224-
.filter(|x| x.as_ref().unwrap().is_file())
225-
.collect();
226-
227-
// convert to vector of strings
228-
let paths: Vec<String> = path_globs
229-
.into_iter()
230-
.map(|x| x.unwrap().to_string_lossy().to_string())
231-
.collect();
232-
233-
Ok(paths)
215+
Ok(glob::glob_with(pattern, glob_settings)?
216+
.filter_map(|entry| {
217+
match entry {
218+
Ok(path) if path.is_file() => {
219+
Some(path.to_string_lossy().into_owned())
220+
}
221+
_ => None,
222+
}
223+
})
224+
.collect())
234225
}
235226

236227
/// output all file hashes matching a pattern, directly to stdout. Single-threaded

0 commit comments

Comments
 (0)