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 track numbering in ID3 TRCK style (NN/MM). #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Binary file added assets/meep.flac
Binary file not shown.
25 changes: 19 additions & 6 deletions src/components/flac_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,9 @@ impl AudioTagEdit for FlacTag {
}

fn track_number(&self) -> Option<u16> {
if let Some(Ok(n)) = self.get_first("TRACKNUMBER").map(|x| x.parse::<u16>()) {
Some(n)
} else {
None
}
self.track_number_pair().0
}

fn set_track_number(&mut self, v: u16) {
self.set_first("TRACKNUMBER", &v.to_string())
}
Expand All @@ -140,7 +137,8 @@ impl AudioTagEdit for FlacTag {
if let Some(Ok(n)) = self.get_first("TOTALTRACKS").map(|x| x.parse::<u16>()) {
Some(n)
} else {
None
// Support "NN/MM" in the style of ID3-style TRCK.
self.track_number_pair().1
}
}
fn set_total_tracks(&mut self, v: u16) {
Expand Down Expand Up @@ -214,3 +212,18 @@ impl AudioTagWrite for FlacTag {
Ok(())
}
}

impl FlacTag {
fn track_number_pair(&self) -> (Option<u16>, Option<u16>) {
if let Some(s) = self.get_first("TRACKNUMBER") {
let mut parts = s.split("/").fuse().map(|t| t.parse().ok());
// Manually unpack the first two items instead of using
// collect() so that we can avoid allocating a Vec.
let track = parts.next().flatten();
let total = parts.next().flatten();
(track, total)
} else {
(None, None)
}
}
}
11 changes: 11 additions & 0 deletions tests/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,14 @@ macro_rules! test_file {
test_file!(test_mp3, "assets/a.mp3");
test_file!(test_m4a, "assets/a.m4a");
test_file!(test_flac, "assets/a.flac");

#[test]
fn test_id3_style_track_number() {
// Verify that we can consume TRACKNUMER tags in the style of the
// ID3 TRCK tag. This is non-standard (for Vorbis comments) but
// other tools (such as easytags) will read it (but, AFAIK, not
// write it).
let tags = Tag::default().read_from_path("assets/meep.flac").unwrap();
assert_eq!(tags.track_number(), Some(1));
assert_eq!(tags.total_tracks(), Some(6));
}