Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for getting/setting comment #27

Merged
merged 2 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -249,6 +250,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