Improve the search_for
utilty function and reduce the nested code
This commit is contained in:
parent
a5dd196937
commit
c71992fc52
2 changed files with 11 additions and 12 deletions
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
mod arguments;
|
mod arguments;
|
||||||
mod cmus;
|
mod cmus;
|
||||||
|
mod utils;
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
|
||||||
|
|
22
src/utils.rs
22
src/utils.rs
|
@ -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(|®x| file_name.contains(regx)) {
|
if regx.iter().any(|®x| 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(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue