Create search_for function utilty to help us to find the cover image or the lyrices

This commit is contained in:
Anas Elgarhy 2023-02-08 20:07:56 +02:00
parent a9bec7479c
commit be412d5f57
No known key found for this signature in database
GPG key ID: 0501802A1D496528
3 changed files with 105 additions and 27 deletions

94
Cargo.lock generated
View file

@ -208,8 +208,8 @@ dependencies = [
"clap",
"lrc",
"notify-rust",
"test-context",
"typed-builder",
"walkdir",
]
[[package]]
@ -493,12 +493,48 @@ dependencies = [
"spin",
]
[[package]]
name = "futures"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13e2792b0ff0340399d58445b88fd9770e3489eff258a4cbc1523418f12abf84"
dependencies = [
"futures-channel",
"futures-core",
"futures-executor",
"futures-io",
"futures-sink",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-channel"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e5317663a9089767a1ec00a487df42e0ca174b61b4483213ac24448e4664df5"
dependencies = [
"futures-core",
"futures-sink",
]
[[package]]
name = "futures-core"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec90ff4d0fe1f57d600049061dc6bb68ed03c7d2fbd697274c41805dcb3f8608"
[[package]]
name = "futures-executor"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8de0a35a6ab97ec8869e32a2473f4b1324459e14c29275d14b10cb1fd19b50e"
dependencies = [
"futures-core",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-io"
version = "0.3.26"
@ -520,6 +556,17 @@ dependencies = [
"waker-fn",
]
[[package]]
name = "futures-macro"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95a73af87da33b5acf53acfebdc339fe592ecf5357ac7c0a7734ab9d8c876a70"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "futures-sink"
version = "0.3.26"
@ -538,9 +585,13 @@ version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c1d6de3acfef38d2be4b1f543f553131788603495be83da675e180c8d6b7bd1"
dependencies = [
"futures-channel",
"futures-core",
"futures-io",
"futures-macro",
"futures-sink",
"futures-task",
"memchr",
"pin-project-lite",
"pin-utils",
"slab",
@ -1235,15 +1286,6 @@ dependencies = [
"windows-sys 0.42.0",
]
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]]
name = "scoped_threadpool"
version = "0.1.9"
@ -1426,6 +1468,27 @@ dependencies = [
"windows-sys 0.42.0",
]
[[package]]
name = "test-context"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "055831a02a4f5aa28fede67f2902014273eb8c21b958ac5ebbd59b71ef30dbc3"
dependencies = [
"async-trait",
"futures",
"test-context-macros",
]
[[package]]
name = "test-context-macros"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8901a55b0a7a06ebc4a674dcca925170da8e613fa3b163a1df804ed10afb154d"
dependencies = [
"quote",
"syn",
]
[[package]]
name = "thiserror"
version = "1.0.38"
@ -1591,17 +1654,6 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca"
[[package]]
name = "walkdir"
version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56"
dependencies = [
"same-file",
"winapi",
"winapi-util",
]
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"

View file

@ -9,7 +9,6 @@ edition = "2021"
lrc = { version = "0.1.7", optional = true }
notify-rust = { version = "4.7.0", features = ["images"] }
typed-builder = "0.12.0"
walkdir = "2.3.2"
[dependencies.clap]
version = "4.1.4"
@ -19,3 +18,6 @@ features = ["wrap_help", "cargo", "usage", "derive", "suggestions", "color"]
lyrics = ["lrc"]
default = ["lyrics"]
[dev-dependencies]
test-context = "0.1.4"

View file

@ -1,10 +1,34 @@
use std::path::Path;
use crate::cmus;
pub fn search_for_cover_image(search_directory: &str, max_depth: u8) -> Option<String> {
None
/// Search in the track directory for the cover image or the lyrics(depending on the `regx`).
/// If the cover image or the lyrics is not found, search in the parent directory, and so on, until the max depth is reached.
/// If the cover image or the lyrics is not found, return `None`.
pub fn search_for(search_directory: &str, max_depth: u8, regx: &[&str]) -> std::io::Result<Option<String>> {
// 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(|&regx| file_name.contains(regx)) {
let path = entry.path();
let Some(path) = path.to_str() else { continue; };
return Ok(Some(path.to_string()));
}
}
}
}
}
// If the max depth is reached, return `None`.
if max_depth == 0 {
Ok(None)
} else { // If the max depth is not reached, search in the parent directory (recursively).
let Some(parent) = Path::new(search_directory).parent() else { return Ok(None); };
let Some(parent) = parent.to_str() else { return Ok(None); };
search_for(parent, max_depth - 1, regx)
}
}
/// Replace all the placeholders in the template with their matching value.