Some code cleanup

This commit is contained in:
Connor Johnstone
2026-03-04 23:28:29 -05:00
parent d59235707d
commit 70aedb49f2
4 changed files with 345 additions and 295 deletions

View File

@@ -3,46 +3,51 @@ use std::path::Path;
use lofty::file::TaggedFileExt;
use lofty::tag::ItemKey;
/// Extract the artist name from a music file.
/// Tags that can be read from a music file.
pub enum Tag {
ArtistName,
ArtistMbid,
TrackTitle,
TrackMbid,
}
impl Tag {
fn item_key(&self) -> ItemKey {
match self {
Tag::ArtistName => ItemKey::TrackArtist,
Tag::ArtistMbid => ItemKey::MusicBrainzArtistId,
Tag::TrackTitle => ItemKey::TrackTitle,
Tag::TrackMbid => ItemKey::MusicBrainzRecordingId,
}
}
}
fn read_tag(path: &Path, key: ItemKey) -> Result<Option<String>, lofty::error::LoftyError> {
let tagged_file = lofty::read_from_path(path)?;
let Some(tag) = tagged_file.primary_tag().or_else(|| tagged_file.first_tag()) else {
return Ok(None);
};
Ok(tag.get_string(key).map(String::from))
}
/// Read multiple tags from a music file in a single file open.
/// Returns a Vec in the same order as the input keys.
pub fn read_tags(path: &Path, keys: &[Tag]) -> Result<Vec<Option<String>>, lofty::error::LoftyError> {
let tagged_file = lofty::read_from_path(path)?;
let Some(tag) = tagged_file.primary_tag().or_else(|| tagged_file.first_tag()) else {
return Ok(vec![None; keys.len()]);
};
Ok(keys
.iter()
.map(|k| tag.get_string(k.item_key()).map(String::from))
.collect())
}
pub fn read_artist_name(path: &Path) -> Result<Option<String>, lofty::error::LoftyError> {
let tagged_file = lofty::read_from_path(path)?;
let Some(tag) = tagged_file.primary_tag().or_else(|| tagged_file.first_tag()) else {
return Ok(None);
};
Ok(tag.get_string(ItemKey::TrackArtist).map(String::from))
read_tag(path, ItemKey::TrackArtist)
}
/// Extract the MusicBrainz artist ID from a music file.
pub fn read_artist_mbid(path: &Path) -> Result<Option<String>, lofty::error::LoftyError> {
let tagged_file = lofty::read_from_path(path)?;
let Some(tag) = tagged_file.primary_tag().or_else(|| tagged_file.first_tag()) else {
return Ok(None);
};
Ok(tag.get_string(ItemKey::MusicBrainzArtistId).map(String::from))
read_tag(path, ItemKey::MusicBrainzArtistId)
}
/// Extract the track title from a music file.
pub fn read_track_title(path: &Path) -> Result<Option<String>, lofty::error::LoftyError> {
let tagged_file = lofty::read_from_path(path)?;
let Some(tag) = tagged_file.primary_tag().or_else(|| tagged_file.first_tag()) else {
return Ok(None);
};
Ok(tag.get_string(ItemKey::TrackTitle).map(String::from))
}
/// Extract the MusicBrainz recording ID from a music file.
pub fn read_track_mbid(path: &Path) -> Result<Option<String>, lofty::error::LoftyError> {
let tagged_file = lofty::read_from_path(path)?;
let Some(tag) = tagged_file.primary_tag().or_else(|| tagged_file.first_tag()) else {
return Ok(None);
};
Ok(tag.get_string(ItemKey::MusicBrainzRecordingId).map(String::from))
}