Skip to content

Commit

Permalink
Add support for getting/setting comment (#27)
Browse files Browse the repository at this point in the history
* Add support for getting/setting comment

Currently mack[0] only supports ID3 tags, and I'd like to allow it to
also support FLAC and M4A. It currently uses the comment field as
presented by rust-id3, but audiotags doesn't expose this. This commit
adds support to that effect.

0: https://github.com/cdown/mack

* Update src/components/id3_tag.rs

Co-authored-by: Alex <69764315+Serial-ATA@users.noreply.github.com>

---------

Co-authored-by: pinkforest(she/her) <36498018+pinkforest@users.noreply.github.com>
Co-authored-by: Alex <69764315+Serial-ATA@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 3, 2023
1 parent 6ae6919 commit b6b3df1
Show file tree
Hide file tree
Showing 6 changed files with 57 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 @@ -16,6 +16,7 @@ pub struct AnyTag<'a> {
pub total_discs: Option<u16>,
pub genre: Option<&'a str>,
pub composer: Option<&'a str>,
pub comment: Option<&'a str>,
}

impl AudioTagConfig for AnyTag<'_> {
Expand Down Expand Up @@ -71,6 +72,9 @@ impl<'a> AnyTag<'a> {
pub fn composer(&self) -> Option<&str> {
self.composer
}
pub fn comment(&self) -> Option<&str> {
self.comment
}
}

impl AnyTag<'_> {
Expand Down
11 changes: 11 additions & 0 deletions src/components/flac_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ impl<'a> From<&'a FlacTag> for AnyTag<'a> {
total_discs: inp.total_discs(),
genre: inp.genre(),
composer: inp.composer(),
comment: inp.comment(),
..Self::default()
};

Expand Down Expand Up @@ -250,6 +251,16 @@ impl AudioTagEdit for FlacTag {
fn remove_genre(&mut self) {
self.remove("GENRE");
}

fn comment(&self) -> Option<&str> {
self.get_first("COMMENT")
}
fn set_comment(&mut self, v: String) {
self.set_first("COMMENT", &v);
}
fn remove_comment(&mut self) {
self.remove("COMMENT");
}
}

impl AudioTagWrite for FlacTag {
Expand Down
20 changes: 20 additions & 0 deletions src/components/id3_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl<'a> From<&'a Id3v2Tag> for AnyTag<'a> {
total_discs: inp.total_discs(),
genre: inp.genre(),
composer: inp.composer(),
comment: inp.comment(),
}
}
}
Expand Down Expand Up @@ -247,6 +248,25 @@ impl AudioTagEdit for Id3v2Tag {
fn remove_genre(&mut self) {
self.inner.remove_genre();
}

fn comment(&self) -> Option<&str> {
for comment in self.inner.comments() {
if comment.description.is_empty() {
return Some(comment.text.as_str());
}
}
None
}
fn set_comment(&mut self, comment: String) {
self.inner.add_frame(id3::frame::Comment {
lang: "XXX".to_string(),
description: "".to_string(),
text: comment,
});
}
fn remove_comment(&mut self) {
self.inner.remove("COMM");
}
}

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 @@ -24,6 +24,7 @@ impl<'a> From<&'a Mp4Tag> for AnyTag<'a> {
let total_discs = b;
let genre = inp.genre();
let composer = inp.composer();
let comment = inp.comment();
Self {
config: inp.config,
title,
Expand All @@ -39,6 +40,7 @@ impl<'a> From<&'a Mp4Tag> for AnyTag<'a> {
total_discs,
genre,
composer,
comment,
}
}
}
Expand Down Expand Up @@ -283,6 +285,16 @@ impl AudioTagEdit for Mp4Tag {
fn remove_genre(&mut self) {
self.inner.remove_genres();
}

fn comment(&self) -> Option<&str> {
self.inner.comment()
}
fn set_comment(&mut self, comment: String) {
self.inner.set_comment(comment);
}
fn remove_comment(&mut self) {
self.inner.remove_comments();
}
}

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 @@ -134,6 +134,10 @@ pub trait AudioTagEdit: AudioTagConfig {
fn genre(&self) -> Option<&str>;
fn set_genre(&mut self, genre: &str);
fn remove_genre(&mut self);

fn comment(&self) -> Option<&str>;
fn set_comment(&mut self, genre: String);
fn remove_comment(&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 @@ -62,6 +62,12 @@ macro_rules! test_file {
tags.remove_genre();
assert!(tags.genre().is_none());
tags.remove_genre();

tags.set_comment("foo song comment".to_string());
assert_eq!(tags.comment(), Some("foo song comment"));
tags.remove_comment();
assert!(tags.comment().is_none());
tags.remove_comment();
}
};
}
Expand Down

0 comments on commit b6b3df1

Please sign in to comment.