diff --git a/src/core/collection/artist.rs b/src/core/collection/artist.rs index a9b3b66..436d371 100644 --- a/src/core/collection/artist.rs +++ b/src/core/collection/artist.rs @@ -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, primary_by_singleton: HashMap, - primary_by_title: HashMap>, - secondary_by_title: HashMap>, + primary_by_title: TitleMap, + secondary_by_title: TitleMap, merged: Vec, } @@ -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, + ); } } diff --git a/src/core/collection/merge.rs b/src/core/collection/merge.rs index 9153a9f..df3566f 100644 --- a/src/core/collection/merge.rs +++ b/src/core/collection/merge.rs @@ -82,16 +82,35 @@ where } } +#[derive(Debug)] +pub struct TitleMap(HashMap>); + +impl Default for TitleMap { + fn default() -> Self { + TitleMap(HashMap::default()) + } +} + +impl TitleMap { + 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> { + self.0.remove(key) + } +} + pub struct MergeCollections; impl MergeCollections { - pub fn merge_by_name(mut primary: HashMap>, secondary: It) -> Vec - where - T: Merge, - It: IntoIterator)>, - { + pub fn merge_by_name(mut primary: TitleMap, secondary: TitleMap) -> Vec { 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 } } diff --git a/src/core/musichoard/base.rs b/src/core/musichoard/base.rs index 59c4dd2..450f510 100644 --- a/src/core/musichoard/base.rs +++ b/src/core/musichoard/base.rs @@ -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 IMusicHoardBasePrivate for MusicHoard } fn merge_collections(&self) -> Collection { - let mut primary = HashMap::>::new(); - let mut secondary = HashMap::>::new(); + let mut primary = TitleMap::::new(); + let mut secondary = TitleMap::::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);