aboutsummaryrefslogtreecommitdiff
path: root/src/albums.rs
blob: c03e953a3d544c4319d8af0ad219bc18336c3a60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use std::collections::HashSet;
use std::path::PathBuf;

use expanduser::expanduser;
use walkdir::WalkDir;

use crate::config::Config;

pub fn load(config: &Config) -> HashSet<String> {
    let root = expanduser(&config.music_root).ok().filter(|p| p.is_dir());
    let Some(root_path) = root else {
        return HashSet::new();
    };

    let mut album_dirs: HashSet<PathBuf> = HashSet::new();

    for entry in WalkDir::new(&root_path) {
        let Ok(entry) = entry else { continue };

        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 is_audio_file
            && let Some(parent) = path.parent()
            && parent != root_path
        {
            album_dirs.insert(parent.to_path_buf());
        }
    }

    album_dirs
        .iter()
        .filter_map(|p| p.strip_prefix(&root_path).ok()?.to_str().map(String::from))
        .collect()
}