From f4162b8b61a6f50c43bdce7b3dabf053553c01e8 Mon Sep 17 00:00:00 2001 From: shamardy Date: Fri, 6 Sep 2024 21:36:35 +0300 Subject: [PATCH] review fix: reduce duplication by extracting it to extract_file_identifiers function --- mm2src/mm2_io/src/fs.rs | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/mm2src/mm2_io/src/fs.rs b/mm2src/mm2_io/src/fs.rs index fbb3529d82..ca3dea83f9 100644 --- a/mm2src/mm2_io/src/fs.rs +++ b/mm2src/mm2_io/src/fs.rs @@ -202,27 +202,28 @@ async fn filter_files_with_extension(dir_path: &Path, extension: &str) -> IoResu Ok(entries) } +/// Helper function to extract file names or stems based on the provided extraction function. +fn extract_file_identifiers(entries: Vec, extractor: F) -> Vec +where + F: Fn(&Path) -> Option<&OsStr>, +{ + entries + .into_iter() + .filter_map(|path| extractor(&path).and_then(OsStr::to_str).map(ToOwned::to_owned)) + .collect() +} + /// Reads the file names with the specified extension from the given directory path. pub async fn read_file_names_with_extension(dir_path: &Path, extension: &str) -> IoResult> { let entries = filter_files_with_extension(dir_path, extension).await?; - let file_names: Vec = entries - .into_iter() - .filter_map(|path| path.file_name().and_then(|name| name.to_str().map(|s| s.to_string()))) - .collect(); - - Ok(file_names) + Ok(extract_file_identifiers(entries, Path::file_name)) } /// Reads the file stems with the specified extension from the given directory path. /// The stem is the file name without the extension. pub async fn read_file_stems_with_extension(dir_path: &Path, extension: &str) -> IoResult> { let entries = filter_files_with_extension(dir_path, extension).await?; - let file_names: Vec = entries - .into_iter() - .filter_map(|path| path.file_stem().and_then(|stem| stem.to_str().map(|s| s.to_string()))) - .collect(); - - Ok(file_names) + Ok(extract_file_identifiers(entries, Path::file_stem)) } /// Read the `dir_path` entries trying to deserialize each as the `T` type,