diff options
Diffstat (limited to 'src/albums.rs')
| -rw-r--r-- | src/albums.rs | 52 |
1 files changed, 19 insertions, 33 deletions
diff --git a/src/albums.rs b/src/albums.rs index 17966ff..c03e953 100644 --- a/src/albums.rs +++ b/src/albums.rs @@ -1,5 +1,5 @@ use std::collections::HashSet; -use std::fs; +use std::path::PathBuf; use expanduser::expanduser; use walkdir::WalkDir; @@ -12,45 +12,31 @@ pub fn load(config: &Config) -> HashSet<String> { return HashSet::new(); }; - let mut new_albums: HashSet<String> = HashSet::new(); + let mut album_dirs: HashSet<PathBuf> = HashSet::new(); for entry in WalkDir::new(&root_path) { - let entry = match entry { - Ok(e) if e.path().is_dir() => e, - _ => continue, - }; + let Ok(entry) = entry else { continue }; - if !contains_audio(&entry, config) { + let path = entry.path(); + if path.is_dir() { continue; - } + }; + + let is_audio_file = path + .extension() + .and_then(|ext| ext.to_str()) + .is_some_and(|ext| config.audio_exts.contains(&ext.to_lowercase())); - if let Some(rel_str) = entry - .path() - .strip_prefix(&root_path) - .ok() - .and_then(|p| p.to_str()) + if is_audio_file + && let Some(parent) = path.parent() + && parent != root_path { - new_albums.insert(rel_str.to_string()); + album_dirs.insert(parent.to_path_buf()); } } - new_albums -} - -fn contains_audio(entry: &walkdir::DirEntry, config: &Config) -> bool { - fs::read_dir(entry.path()).ok().is_some_and(|mut entries| { - entries.any(|e| { - e.as_ref() - .is_ok_and(|entry| is_audio_file(&entry.path(), config)) - }) - }) -} - -fn is_audio_file(path: &std::path::Path, config: &Config) -> bool { - !path.is_dir() - && path - .extension() - .and_then(|ext| ext.to_str()) - .map(str::to_lowercase) - .is_some_and(|ext| config.audio_exts.contains(&format!(".{ext}"))) + album_dirs + .iter() + .filter_map(|p| p.strip_prefix(&root_path).ok()?.to_str().map(String::from)) + .collect() } |
