Skip to content

Commit

Permalink
ID3v2: Use genres method in Accessor genre getter
Browse files Browse the repository at this point in the history
  • Loading branch information
sublipri committed Nov 4, 2023
1 parent d35bf04 commit ad501ae
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion src/id3/v2/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,6 @@ impl Accessor for Id3v2Tag {
title => "TIT2";
artist => "TPE1";
album => "TALB";
genre => "TCON";
);

fn track(&self) -> Option<u32> {
Expand Down Expand Up @@ -760,6 +759,31 @@ impl Accessor for Id3v2Tag {
}
}

fn genre(&self) -> Option<Cow<'_, str>> {
let mut genres = self.genres()?.peekable();
let first = genres.next()?;

if genres.peek().is_none() {
return Some(Cow::Borrowed(first));
};

let mut joined = String::from(first);
for genre in genres {
joined.push_str(" / ");
joined.push_str(genre);
}

Some(Cow::Owned(joined))
}

fn set_genre(&mut self, value: String) {
self.insert(new_text_frame(GENRE_ID, value, FrameFlags::default()));
}

fn remove_genre(&mut self) {
let _ = self.remove(&GENRE_ID);
}

fn year(&self) -> Option<u32> {
if let Some(Frame {
value: FrameValue::Text(TextInformationFrame { value, .. }),
Expand Down Expand Up @@ -2542,6 +2566,29 @@ mod tests {
tag
}

#[test]
fn genre_text() {
let tag = id3v2_tag_with_genre("Dream Pop");
assert_eq!(tag.genre(), Some(Cow::Borrowed("Dream Pop")));
}
#[test]
fn genre_id_brackets() {
let tag = id3v2_tag_with_genre("(21)");
assert_eq!(tag.genre(), Some(Cow::Borrowed("Ska")));
}

#[test]
fn genre_id_numeric() {
let tag = id3v2_tag_with_genre("21");
assert_eq!(tag.genre(), Some(Cow::Borrowed("Ska")));
}

#[test]
fn genre_id_multiple_joined() {
let tag = id3v2_tag_with_genre("(51)(39)");
assert_eq!(tag.genre(), Some(Cow::Borrowed("Techno-Industrial / Noise")));
}

#[test]
fn genres_id_multiple() {
let tag = id3v2_tag_with_genre("(51)(39)");
Expand Down

0 comments on commit ad501ae

Please # to comment.