Skip to content

Commit

Permalink
Import @Dalvany's genre (#5)
Browse files Browse the repository at this point in the history
* Add genre

Add genre tag

* Add some tests

Co-authored-by: Dalvany <[email protected]>
  • Loading branch information
martpie and Dalvany authored May 25, 2022
1 parent 66d8994 commit edae1dc
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/anytag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct AnyTag<'a> {
pub total_tracks: Option<u16>,
pub disc_number: Option<u16>,
pub total_discs: Option<u16>,
pub genre:Option<&'a str>,
}

impl AudioTagConfig for AnyTag<'_> {
Expand Down Expand Up @@ -66,6 +67,9 @@ impl<'a> AnyTag<'a> {
pub fn total_discs(&self) -> Option<u16> {
self.total_tracks
}
pub fn genre(&self) -> Option<&str> {
self.genre.as_deref()
}
}

impl AnyTag<'_> {
Expand Down
10 changes: 10 additions & 0 deletions src/components/flac_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ impl AudioTagEdit for FlacTag {
self.set_first("TOTALDISCS", &v.to_string())
}

fn genre(&self) -> Option<&str> {
self.get_first("GENRE")
}
fn set_genre(&mut self, v: &str) {
self.set_first("GENRE", v);
}

fn remove_title(&mut self) {
self.remove("TITLE");
}
Expand Down Expand Up @@ -209,6 +216,9 @@ impl AudioTagEdit for FlacTag {
fn remove_total_discs(&mut self) {
self.remove("TOTALDISCS");
}
fn remove_genre(&mut self) {
self.remove("GENRE");
}
}

impl AudioTagWrite for FlacTag {
Expand Down
12 changes: 12 additions & 0 deletions src/components/id3_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ impl<'a> From<&'a Id3v2Tag> for AnyTag<'a> {
total_tracks: inp.total_tracks(),
disc_number: inp.disc_number(),
total_discs: inp.total_discs(),
genre: inp.genre(),
}
}
}
Expand All @@ -40,6 +41,7 @@ impl<'a> From<AnyTag<'a>> for Id3v2Tag {
inp.total_tracks().map(|v| t.set_total_tracks(v as u32));
inp.disc_number().map(|v| t.set_disc(v as u32));
inp.total_discs().map(|v| t.set_total_discs(v as u32));
inp.genre().map(|v| t.set_genre(v));
t
},
}
Expand Down Expand Up @@ -181,6 +183,16 @@ impl AudioTagEdit for Id3v2Tag {
fn remove_total_discs(&mut self) {
self.inner.remove_total_discs();
}

fn genre(&self) -> Option<&str> {
self.inner.genre()
}
fn set_genre(&mut self, v: &str) {
self.inner.set_genre(v);
}
fn remove_genre(&mut self) {
self.inner.remove_genre();
}
}

impl AudioTagWrite for Id3v2Tag {
Expand Down
12 changes: 12 additions & 0 deletions src/components/mp4_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ impl<'a> From<&'a Mp4Tag> for AnyTag<'a> {
let (a, b) = inp.disc();
let disc_number = a;
let total_discs = b;
let genre = inp.genre();
Self {
config: inp.config.clone(),
title,
Expand All @@ -35,6 +36,7 @@ impl<'a> From<&'a Mp4Tag> for AnyTag<'a> {
total_tracks,
disc_number,
total_discs,
genre,
}
}
}
Expand Down Expand Up @@ -203,6 +205,13 @@ impl AudioTagEdit for Mp4Tag {
self.inner.set_total_discs(total_discs)
}

fn genre(&self) -> Option<&str> {
self.inner.genre()
}
fn set_genre(&mut self, genre: &str) {
self.inner.set_genre(genre);
}

fn remove_title(&mut self) {
self.inner.remove_title();
}
Expand Down Expand Up @@ -239,6 +248,9 @@ impl AudioTagEdit for Mp4Tag {
fn remove_total_discs(&mut self) {
self.inner.remove_total_discs();
}
fn remove_genre(&mut self) {
self.inner.remove_genres();
}
}

impl AudioTagWrite for Mp4Tag {
Expand Down
4 changes: 4 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ pub trait AudioTagEdit: AudioTagConfig {
fn total_discs(&self) -> Option<u16>;
fn set_total_discs(&mut self, total_discs: u16);
fn remove_total_discs(&mut self);

fn genre(&self) -> Option<&str>;
fn set_genre(&mut self, genre:&str);
fn remove_genre(&mut self);
}

pub trait AudioTagWrite {
Expand Down
6 changes: 6 additions & 0 deletions tests/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ macro_rules! test_file {
tags.remove_album_cover();
assert!(tags.album_cover().is_none());
tags.remove_album_cover();

tags.set_genre("foo song genre");
assert_eq!(tags.genre(), Some("foo song genre"));
tags.remove_genre();
assert!(tags.genre().is_none());
tags.remove_genre();
}
};
}
Expand Down

0 comments on commit edae1dc

Please sign in to comment.