Improve the search_for utilty function and reduce the nested code

This commit is contained in:
Anas Elgarhy 2023-02-08 21:30:31 +02:00
parent a5dd196937
commit c71992fc52
No known key found for this signature in database
GPG key ID: 0501802A1D496528
2 changed files with 11 additions and 12 deletions

View file

@ -2,6 +2,7 @@
mod arguments; mod arguments;
mod cmus; mod cmus;
mod utils;
use clap::Parser; use clap::Parser;

View file

@ -11,17 +11,15 @@ pub fn search_for(
) -> std::io::Result<Option<String>> { ) -> std::io::Result<Option<String>> {
// Search in the track directory. // Search in the track directory.
for entry in std::fs::read_dir(search_directory)? { for entry in std::fs::read_dir(search_directory)? {
if let Ok(entry) = entry { let Ok(entry) = entry else { continue; };
if let Ok(file_type) = entry.file_type() { let Ok(file_type) = entry.file_type() else { continue; };
if file_type.is_file() { if file_type.is_file() {
let Ok(file_name) = entry.file_name().into_string() else { continue; }; let Ok(file_name) = entry.file_name().into_string() else { continue; };
// Check if the file name matches any of the regular expressions. // Check if the file name matches any of the regular expressions.
if regx.iter().any(|&regx| file_name.contains(regx)) { if regx.iter().any(|&regx| file_name.contains(regx)) {
let path = entry.path(); let path = entry.path();
let Some(path) = path.to_str() else { continue; }; let Some(path) = path.to_str() else { continue; };
return Ok(Some(path.to_string())); return Ok(Some(path.to_string()));
}
}
} }
} }
} }
@ -79,7 +77,7 @@ mod tests {
track: cmus::Track::from_str(include_str!( track: cmus::Track::from_str(include_str!(
"../tests/samples/cmus-remote-output-with-all-tags.txt" "../tests/samples/cmus-remote-output-with-all-tags.txt"
)) ))
.unwrap(), .unwrap(),
} }
} }
} }