use std::collections::HashSet; use std::fs; use expanduser::expanduser; use walkdir::WalkDir; use crate::config::Config; pub fn load(config: &Config) -> HashSet { let root = expanduser(&config.music_root).ok().filter(|p| p.is_dir()); let Some(root_path) = root else { return HashSet::new(); }; let mut new_albums: HashSet = HashSet::new(); for entry in WalkDir::new(&root_path) { let entry = match entry { Ok(e) if e.path().is_dir() => e, _ => continue, }; if !contains_audio(&entry, config) { continue; } if let Some(rel_str) = entry .path() .strip_prefix(&root_path) .ok() .and_then(|p| p.to_str()) { new_albums.insert(rel_str.to_string()); } } 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}"))) }