From c71992fc52cc33420a8483a9340ee177c5c1df9e Mon Sep 17 00:00:00 2001 From: Anas Elgarhy Date: Wed, 8 Feb 2023 21:30:31 +0200 Subject: [PATCH] Improve the `search_for` utilty function and reduce the nested code --- src/main.rs | 1 + src/utils.rs | 22 ++++++++++------------ 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9a664c4..72af618 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ mod arguments; mod cmus; +mod utils; use clap::Parser; diff --git a/src/utils.rs b/src/utils.rs index 10472dc..e448fc0 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -11,17 +11,15 @@ pub fn search_for( ) -> std::io::Result> { // Search in the track directory. for entry in std::fs::read_dir(search_directory)? { - if let Ok(entry) = entry { - if let Ok(file_type) = entry.file_type() { - if file_type.is_file() { - let Ok(file_name) = entry.file_name().into_string() else { continue; }; - // Check if the file name matches any of the regular expressions. - if regx.iter().any(|®x| file_name.contains(regx)) { - let path = entry.path(); - let Some(path) = path.to_str() else { continue; }; - return Ok(Some(path.to_string())); - } - } + let Ok(entry) = entry else { continue; }; + let Ok(file_type) = entry.file_type() else { continue; }; + if file_type.is_file() { + let Ok(file_name) = entry.file_name().into_string() else { continue; }; + // Check if the file name matches any of the regular expressions. + if regx.iter().any(|®x| file_name.contains(regx)) { + let path = entry.path(); + let Some(path) = path.to_str() else { continue; }; + return Ok(Some(path.to_string())); } } } @@ -79,7 +77,7 @@ mod tests { track: cmus::Track::from_str(include_str!( "../tests/samples/cmus-remote-output-with-all-tags.txt" )) - .unwrap(), + .unwrap(), } } }