diff --git a/src/db.rs b/src/db.rs index eeb5458..e1454c6 100644 --- a/src/db.rs +++ b/src/db.rs @@ -25,6 +25,7 @@ pub struct FileMetadata { pub duration: u32, } +#[derive(Default)] pub struct Metadata { pub title: String, pub artist: String, diff --git a/src/tags.rs b/src/tags.rs index d6a8060..fb050a6 100644 --- a/src/tags.rs +++ b/src/tags.rs @@ -15,80 +15,66 @@ const MAX_GENRE_VAL: usize = 192; pub fn read(track: &String) -> db::Metadata { let mut meta = db::Metadata { - title: String::new(), - artist: String::new(), - album: String::new(), - album_artist: String::new(), - genre: String::new(), duration: 180, + ..db::Metadata::default() }; - let path = Path::new(track); - match Probe::open(path) { - Ok(probe) => { - match probe.read(true) { - Ok(file) => { - let tag = match file.primary_tag() { - Some(primary_tag) => primary_tag, - None => file.first_tag().expect("Error: No tags found!"), - }; - meta.title = tag.title().unwrap_or("").to_string(); - meta.artist = tag.artist().unwrap_or("").to_string(); - meta.album = tag.album().unwrap_or("").to_string(); - meta.album_artist = tag - .get_string(&ItemKey::AlbumArtist) - .unwrap_or("") - .to_string(); - meta.genre = tag.genre().unwrap_or("").to_string(); - // Check whether MP3 as numeric genre, and if so covert to text - if file.file_type().eq(&lofty::FileType::MP3) { - match tag.genre() { - Some(genre) => { - let test = &genre.parse::(); - match test { - Ok(val) => { - let idx: usize = *val as usize; - if idx < MAX_GENRE_VAL { - meta.genre = lofty::id3::v1::GENRES[idx].to_string(); - } - } - Err(_) => { - // Check for "(number)text" - let re = Regex::new(r"^\([0-9]+\)").unwrap(); - if re.is_match(&genre) { - match genre.find(")") { - Some(end) => { - let test = &genre - .to_string() - .substring(1, end) - .parse::(); - match test { - Ok(val) => { - let idx: usize = *val as usize; - if idx < MAX_GENRE_VAL { - meta.genre = lofty::id3::v1::GENRES - [idx] - .to_string(); - } - } - Err(_) => {} - } - } - None => {} + if let Ok(file) = lofty::read_from_path(Path::new(track), true) { + let tag = match file.primary_tag() { + Some(primary_tag) => primary_tag, + None => file.first_tag().expect("Error: No tags found!"), + }; + + meta.title = tag.title().unwrap_or_default().to_string(); + meta.artist = tag.artist().unwrap_or_default().to_string(); + meta.album = tag.album().unwrap_or_default().to_string(); + meta.album_artist = tag + .get_string(&ItemKey::AlbumArtist) + .unwrap_or_default() + .to_string(); + meta.genre = tag.genre().unwrap_or_default().to_string(); + + // Check whether MP3 as numeric genre, and if so covert to text + if file.file_type().eq(&lofty::FileType::MP3) { + match tag.genre() { + Some(genre) => { + let test = genre.parse::(); + match test { + Ok(val) => { + let idx: usize = val as usize; + if idx < MAX_GENRE_VAL { + meta.genre = lofty::id3::v1::GENRES[idx].to_string(); + } + } + Err(_) => { + // Check for "(number)text" + let re = Regex::new(r"^\([0-9]+\)").unwrap(); + if re.is_match(&genre) { + match genre.find(")") { + Some(end) => { + let test = + genre.to_string().substring(1, end).parse::(); + + if let Ok(val) = test { + let idx: usize = val as usize; + if idx < MAX_GENRE_VAL { + meta.genre = + lofty::id3::v1::GENRES[idx].to_string(); } } } + None => {} } } - None => {} } } - meta.duration = file.properties().duration().as_secs() as u32; } - Err(_) => {} + None => {} } } - Err(_) => {} + + meta.duration = file.properties().duration().as_secs() as u32; } + meta }