danceinterpreter_rs/dataloading/
id3tagreader.rs

1use crate::dataloading::songinfo::SongInfo;
2use iced::widget::image::Handle;
3use id3::frame::PictureType;
4use id3::Result;
5use id3::{Tag, TagLike};
6use std::path::Path;
7
8pub fn read_song_info_from_filepath(file: impl AsRef<Path>) -> Result<SongInfo> {
9    let tag = Tag::read_from_path(file)?;
10
11    let album_art = tag
12        .pictures()
13        // use front cover if available
14        .find(|pic| pic.picture_type == PictureType::CoverFront)
15        // otherwise try to use any picture
16        .or(tag.pictures().next())
17        .cloned();
18
19    Ok(SongInfo::new(
20        tag.track().unwrap_or(0),
21        tag.title().unwrap_or("").to_string(),
22        tag.artist().unwrap_or("").to_string(),
23        tag.genre().unwrap_or("").to_string(),
24        album_art.map(|img| Handle::from_bytes(img.data)),
25    ))
26}
27#[allow(dead_code)]
28pub fn read_song_info_from_files(file_list: &[impl AsRef<Path>]) -> Vec<Result<SongInfo>> {
29    file_list.iter().map(read_song_info_from_filepath).collect()
30}