Skip to content

Commit 8935516

Browse files
authored
Merge pull request #198 from Holzhaus/merge-discs
feat: Merge discs for same release title
2 parents 3ce2965 + cf24b8d commit 8935516

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

src/scanner.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::util::walk_dir;
1818
use crate::Cache;
1919
use crate::{Config, TaggedFile, TaggedFileCollection};
2020
use futures::FutureExt;
21-
use regex::Regex;
21+
use regex::RegexBuilder;
2222
use std::collections::{HashMap, HashSet};
2323
use std::path::PathBuf;
2424
use tokio::runtime::{Builder, Runtime};
@@ -138,7 +138,10 @@ impl Drop for Scanner {
138138
/// Find track collections in the given path.
139139
fn find_track_paths(input_path: PathBuf) -> impl Iterator<Item = (PathBuf, Vec<TaggedFile>)> {
140140
let supported_extensions = HashSet::from(["mp3", "flac"]);
141-
let disc_pattern = Regex::new(r"^(?:CD|Disc)\s*\d+$").unwrap();
141+
let disc_pattern = RegexBuilder::new(r"^(?:CD|Disc)\s*\d+$")
142+
.case_insensitive(true)
143+
.build()
144+
.unwrap();
142145

143146
let mut grouped_tracks: HashMap<PathBuf, Vec<TaggedFile>> = HashMap::new();
144147

src/taggedfilecollection.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::util;
1919
use crate::Config;
2020
use crate::TaggedFile;
2121
use itertools::Itertools;
22+
use regex::{Regex, RegexBuilder};
2223
use std::borrow::Cow;
2324
use std::collections::HashMap;
2425

@@ -117,6 +118,23 @@ fn find_most_common_tag_value<'a>(
117118
MostCommonItem::find(tracks.filter_map(|tagged_file| tagged_file.first_tag_value(key)))
118119
}
119120

121+
/// Strip disc suffix from the end of the `Cow` string.
122+
fn strip_disc_suffix<'a>(title: Cow<'a, str>, pattern: &Regex) -> Cow<'a, str> {
123+
match pattern.find(&title) {
124+
Some(m) => {
125+
let start = m.start();
126+
match title {
127+
Cow::Borrowed(s) => Cow::Borrowed(&s[..start]),
128+
Cow::Owned(mut s) => {
129+
s.truncate(start);
130+
Cow::Owned(s)
131+
}
132+
}
133+
}
134+
None => title,
135+
}
136+
}
137+
120138
/// A collection of tracks on the local disk.
121139
#[derive(Debug)]
122140
pub struct TaggedFileCollection {
@@ -342,7 +360,19 @@ impl FromIterator<TaggedFile> for TaggedFileCollection {
342360

343361
impl ReleaseLike for TaggedFileCollection {
344362
fn release_title(&self) -> Option<Cow<'_, str>> {
345-
self.find_consensual_tag_value(&TagKey::Album)
363+
let disc_pattern = RegexBuilder::new(r"\s*[(\[-]\s*(?:CD|Disc)\s*\d+\s*[)\]]?\s*$")
364+
.case_insensitive(true)
365+
.build()
366+
.unwrap();
367+
368+
MostCommonItem::find(
369+
self.media
370+
.iter()
371+
.flat_map(|media| media.tracks.iter())
372+
.filter_map(|tagged_file| tagged_file.first_tag_value(&TagKey::Album))
373+
.map(|title| strip_disc_suffix(title, &disc_pattern)),
374+
)
375+
.and_then(MostCommonItem::into_concensus)
346376
}
347377

348378
fn release_artist(&self) -> Option<Cow<'_, str>> {

0 commit comments

Comments
 (0)