From 42cf4bf1e58efdadbbcb510b8fcf38c05a19318b Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Thu, 2 Jan 2025 21:51:31 +0100 Subject: [PATCH] Fix code --- src/core/collection/album.rs | 8 +++++++- src/core/collection/artist.rs | 21 ++------------------- src/core/collection/merge.rs | 2 +- 3 files changed, 10 insertions(+), 21 deletions(-) diff --git a/src/core/collection/album.rs b/src/core/collection/album.rs index 3a7e52f..b69f820 100644 --- a/src/core/collection/album.rs +++ b/src/core/collection/album.rs @@ -4,7 +4,7 @@ use std::{ }; use crate::core::collection::{ - merge::{Merge, MergeSorted}, + merge::{Merge, MergeName, MergeSorted}, musicbrainz::{MbAlbumRef, MbRefOption}, track::{Track, TrackFormat}, }; @@ -57,6 +57,12 @@ impl AlbumLibId { /// Unique database identifier. Use MBID for this purpose. pub type AlbumDbId = MbRefOption; +impl MergeName for Album { + fn name(&self) -> &str { + &self.meta.id.title + } +} + // There are crates for handling dates, but we don't need much complexity beyond year-month-day. /// The album's release date. #[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord)] diff --git a/src/core/collection/artist.rs b/src/core/collection/artist.rs index ada3dd4..6b43a0c 100644 --- a/src/core/collection/artist.rs +++ b/src/core/collection/artist.rs @@ -5,7 +5,7 @@ use std::{ use crate::core::collection::{ album::{Album, AlbumLibId}, - merge::{Merge, MergeName}, + merge::{Merge, MergeCollections, MergeName}, musicbrainz::{MbArtistRef, MbRefOption}, }; @@ -87,23 +87,6 @@ impl Artist { } secondary_without_id } - - fn merge_albums_by_title( - primary_albums: &mut Vec, - secondary_without_id: HashMap>, - ) { - for (title, mut secondary_albums) in secondary_without_id.into_iter() { - let mut iter = primary_albums.iter_mut(); - match iter.find(|album| album.meta.id.title == title) { - Some(ref mut primary_album) => { - // We do not support merging multiple DB albums with same title yet. - assert_eq!(secondary_albums.len(), 1); - primary_album.merge_in_place(secondary_albums.pop().unwrap()) - } - None => primary_albums.append(&mut secondary_albums), - } - } - } } impl PartialOrd for Artist { @@ -123,7 +106,7 @@ impl Merge for Artist { self.meta.merge_in_place(other.meta); let other_without_id = Artist::merge_albums_by_lib_id(&mut self.albums, other.albums); - Artist::merge_albums_by_title(&mut self.albums, other_without_id); + MergeCollections::merge_by_name(&mut self.albums, other_without_id); self.albums.sort_unstable(); } } diff --git a/src/core/collection/merge.rs b/src/core/collection/merge.rs index bf9f061..51b5b79 100644 --- a/src/core/collection/merge.rs +++ b/src/core/collection/merge.rs @@ -96,7 +96,7 @@ where { pub fn merge_by_name(primary_items: &mut Vec, secondary: IT) { for (name, mut secondary_items) in secondary.into_iter() { - match primary_items.iter_mut().find(|item| item.name() == &name) { + match primary_items.iter_mut().find(|item| item.name() == name) { Some(ref mut primary_item) => { // We do not support merging multiple DB items with same name yet. assert_eq!(secondary_items.len(), 1);