This commit is contained in:
Wojciech Kozlowski 2025-01-02 21:51:31 +01:00
parent eccec67592
commit 42cf4bf1e5
3 changed files with 10 additions and 21 deletions

View File

@ -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<MbAlbumRef>;
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)]

View File

@ -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<Album>,
secondary_without_id: HashMap<String, Vec<Album>>,
) {
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();
}
}

View File

@ -96,7 +96,7 @@ where
{
pub fn merge_by_name(primary_items: &mut Vec<T>, 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);