Use types to enforce normalisation
Some checks failed
Cargo CI / Build and Test (pull_request) Failing after 32s
Cargo CI / Lint (pull_request) Failing after 28s

This commit is contained in:
Wojciech Kozlowski 2025-01-03 16:34:29 +01:00
parent 711e2ed2ee
commit 7b219933c1
3 changed files with 44 additions and 37 deletions

View File

@ -6,7 +6,7 @@ use std::{
use crate::core::collection::{
album::{Album, AlbumLibId},
merge::{Merge, MergeCollections},
merge::{Merge, MergeCollections, TitleMap},
musicbrainz::{MbArtistRef, MbRefOption},
string::{self, NormalString},
};
@ -65,8 +65,8 @@ impl Merge for Artist {
struct MergeAlbums {
primary_by_lib_id: HashMap<u32, Album>,
primary_by_singleton: HashMap<NormalString, Album>,
primary_by_title: HashMap<NormalString, Vec<Album>>,
secondary_by_title: HashMap<NormalString, Vec<Album>>,
primary_by_title: TitleMap<Album>,
secondary_by_title: TitleMap<Album>,
merged: Vec<Album>,
}
@ -104,16 +104,13 @@ impl MergeAlbums {
self.merge_album_by_lib_id(secondary_album);
}
for (_, primary_album) in self.primary_by_lib_id.drain() {
self.primary_by_title
.entry(string::normalize_string(&primary_album.meta.id.title))
.or_default()
.push(primary_album);
self.primary_by_title.insert(
string::normalize_string(&primary_album.meta.id.title),
primary_album,
);
}
for (title, primary_album) in self.primary_by_singleton.drain() {
self.primary_by_title
.entry(title.clone())
.or_default()
.push(primary_album);
self.primary_by_title.insert(title, primary_album);
}
}
@ -134,10 +131,10 @@ impl MergeAlbums {
}
secondary_album.meta.id.lib_id = AlbumLibId::None;
self.secondary_by_title
.entry(string::normalize_string(&secondary_album.meta.id.title))
.or_default()
.push(secondary_album);
self.secondary_by_title.insert(
string::normalize_string(&secondary_album.meta.id.title),
secondary_album,
);
}
}

View File

@ -82,16 +82,35 @@ where
}
}
#[derive(Debug)]
pub struct TitleMap<T>(HashMap<NormalString, Vec<T>>);
impl<T> Default for TitleMap<T> {
fn default() -> Self {
TitleMap(HashMap::default())
}
}
impl<T> TitleMap<T> {
pub fn new() -> Self {
TitleMap(HashMap::new())
}
pub fn insert(&mut self, key: NormalString, item: T) {
self.0.entry(key).or_default().push(item);
}
fn remove(&mut self, key: &NormalString) -> Option<Vec<T>> {
self.0.remove(key)
}
}
pub struct MergeCollections;
impl MergeCollections {
pub fn merge_by_name<T, It>(mut primary: HashMap<NormalString, Vec<T>>, secondary: It) -> Vec<T>
where
T: Merge,
It: IntoIterator<Item = (NormalString, Vec<T>)>,
{
pub fn merge_by_name<T: Merge>(mut primary: TitleMap<T>, secondary: TitleMap<T>) -> Vec<T> {
let mut merged = vec![];
for (title, mut secondary_items) in secondary.into_iter() {
for (title, mut secondary_items) in secondary.0.into_iter() {
match primary.remove(&title) {
Some(mut primary_items) => {
// We do not support merging multiple items with same name yet. Support will be
@ -105,7 +124,7 @@ impl MergeCollections {
None => merged.extend(secondary_items),
}
}
merged.extend(primary.into_values().flatten());
merged.extend(primary.0.into_values().flatten());
merged
}
}

View File

@ -1,12 +1,9 @@
use std::collections::HashMap;
use crate::core::{
collection::{
album::{Album, AlbumId},
artist::{Artist, ArtistId},
merge::MergeCollections,
string::{self, NormalString},
Collection,
merge::{MergeCollections, TitleMap},
string, Collection,
},
musichoard::{Error, MusicHoard},
};
@ -60,21 +57,15 @@ impl<Database, Library> IMusicHoardBasePrivate for MusicHoard<Database, Library>
}
fn merge_collections(&self) -> Collection {
let mut primary = HashMap::<NormalString, Vec<Artist>>::new();
let mut secondary = HashMap::<NormalString, Vec<Artist>>::new();
let mut primary = TitleMap::<Artist>::new();
let mut secondary = TitleMap::<Artist>::new();
for artist in self.library_cache.iter().cloned() {
primary
.entry(string::normalize_string(&artist.meta.id.name))
.or_default()
.push(artist);
primary.insert(string::normalize_string(&artist.meta.id.name), artist);
}
for artist in self.database_cache.iter().cloned() {
secondary
.entry(string::normalize_string(&artist.meta.id.name))
.or_default()
.push(artist);
secondary.insert(string::normalize_string(&artist.meta.id.name), artist);
}
let mut collection = MergeCollections::merge_by_name(primary, secondary);