Use the new info struct

This commit is contained in:
Wojciech Kozlowski 2024-09-28 11:03:27 +02:00
parent 32b09b2f52
commit ec0076fb76
4 changed files with 41 additions and 35 deletions

View File

@ -101,7 +101,20 @@ impl ArtistMeta {
}
}
impl Default for ArtistInfo {
fn default() -> Self {
Self::new()
}
}
impl ArtistInfo {
pub fn new() -> Self {
ArtistInfo {
musicbrainz: MbRefOption::None,
properties: HashMap::new(),
}
}
pub fn set_musicbrainz_ref(&mut self, mbref: MbRefOption<MbArtistRef>) {
self.musicbrainz = mbref
}

View File

@ -1,13 +1,13 @@
use crate::{
collection::{
album::{AlbumPrimaryType, AlbumSecondaryType},
artist::ArtistInfo,
musicbrainz::{MbAlbumRef, MbRefOption},
},
core::{
collection::{
album::{Album, AlbumId, AlbumSeq},
artist::{Artist, ArtistId},
musicbrainz::MbArtistRef,
Collection,
},
interface::database::IDatabase,
@ -28,13 +28,12 @@ pub trait IMusicHoardDatabase {
) -> Result<(), Error>;
fn clear_artist_sort<Id: AsRef<ArtistId>>(&mut self, artist_id: Id) -> Result<(), Error>;
fn set_artist_musicbrainz<Id: AsRef<ArtistId>>(
fn set_artist_info<Id: AsRef<ArtistId>>(
&mut self,
artist_id: Id,
mbid: MbRefOption<MbArtistRef>,
info: ArtistInfo,
) -> Result<(), Error>;
fn clear_artist_musicbrainz<Id: AsRef<ArtistId>>(&mut self, artist_id: Id)
-> Result<(), Error>;
fn clear_artist_info<Id: AsRef<ArtistId>>(&mut self, artist_id: Id) -> Result<(), Error>;
fn add_to_artist_property<Id: AsRef<ArtistId>, S: AsRef<str> + Into<String>>(
&mut self,
@ -159,22 +158,17 @@ impl<Database: IDatabase, Library> IMusicHoardDatabase for MusicHoard<Database,
)
}
fn set_artist_musicbrainz<Id: AsRef<ArtistId>>(
fn set_artist_info<Id: AsRef<ArtistId>>(
&mut self,
artist_id: Id,
mbid: MbRefOption<MbArtistRef>,
info: ArtistInfo,
) -> Result<(), Error> {
self.update_artist(artist_id.as_ref(), |artist| {
artist.meta.info.set_musicbrainz_ref(mbid)
})
self.update_artist(artist_id.as_ref(), |artist| artist.meta.info = info)
}
fn clear_artist_musicbrainz<Id: AsRef<ArtistId>>(
&mut self,
artist_id: Id,
) -> Result<(), Error> {
fn clear_artist_info<Id: AsRef<ArtistId>>(&mut self, artist_id: Id) -> Result<(), Error> {
self.update_artist(artist_id.as_ref(), |artist| {
artist.meta.info.clear_musicbrainz_ref()
artist.meta.info = ArtistInfo::new()
})
}
@ -407,7 +401,7 @@ mod tests {
use mockall::{predicate, Sequence};
use crate::{
collection::musicbrainz::MbRefOption,
collection::musicbrainz::{MbArtistRef, MbRefOption},
core::{
collection::{album::AlbumDate, artist::ArtistId},
interface::database::{self, MockIDatabase},
@ -521,7 +515,7 @@ mod tests {
}
#[test]
fn set_clear_musicbrainz_url() {
fn set_clear_info() {
let mut database = MockIDatabase::new();
database.expect_load().times(1).returning(|| Ok(vec![]));
database.expect_save().times(3).returning(|_| Ok(()));
@ -535,27 +529,28 @@ mod tests {
let mut expected: MbRefOption<MbArtistRef> = MbRefOption::None;
assert_eq!(music_hoard.collection[0].meta.info.musicbrainz, expected);
let mbref = MbRefOption::Some(MbArtistRef::from_uuid_str(MBID).unwrap());
let mut info = ArtistInfo::new();
info.musicbrainz = MbRefOption::Some(MbArtistRef::from_uuid_str(MBID).unwrap());
// Setting a URL on an artist not in the collection is an error.
assert!(music_hoard
.set_artist_musicbrainz(&artist_id_2, mbref.clone())
.set_artist_info(&artist_id_2, info.clone())
.is_err());
assert_eq!(music_hoard.collection[0].meta.info.musicbrainz, expected);
// Setting a URL on an artist.
assert!(music_hoard
.set_artist_musicbrainz(&artist_id, mbref.clone())
.set_artist_info(&artist_id, info.clone())
.is_ok());
expected.replace(MbArtistRef::from_uuid_str(MBID).unwrap());
assert_eq!(music_hoard.collection[0].meta.info.musicbrainz, expected);
// Clearing URLs on an artist that does not exist is an error.
assert!(music_hoard.clear_artist_musicbrainz(&artist_id_2).is_err());
assert!(music_hoard.clear_artist_info(&artist_id_2).is_err());
assert_eq!(music_hoard.collection[0].meta.info.musicbrainz, expected);
// Clearing URLs.
assert!(music_hoard.clear_artist_musicbrainz(&artist_id).is_ok());
assert!(music_hoard.clear_artist_info(&artist_id).is_ok());
expected.take();
assert_eq!(music_hoard.collection[0].meta.info.musicbrainz, expected);
}

View File

@ -2,7 +2,7 @@ use std::cmp;
use musichoard::collection::{
album::AlbumMeta,
artist::{ArtistId, ArtistMeta},
artist::{ArtistId, ArtistInfo, ArtistMeta},
musicbrainz::{MbRefOption, Mbid},
};
@ -26,12 +26,13 @@ macro_rules! item_option_artist_set {
music_hoard: &mut dyn IMusicHoard,
meta: &ArtistMeta,
) -> Result<(), musichoard::Error> {
let mbref = match self {
let mut info = ArtistInfo::new();
info.musicbrainz = match self {
MatchOption::Some(m) => m.item.info.musicbrainz,
MatchOption::CannotHaveMbid => MbRefOption::CannotHaveMbid,
MatchOption::ManualInputMbid => panic!(),
};
music_hoard.set_artist_musicbrainz(&meta.id, mbref)
music_hoard.set_artist_info(&meta.id, info)
}
}
};

View File

@ -4,8 +4,8 @@ pub mod interface;
use musichoard::{
collection::{
album::{AlbumId, AlbumPrimaryType, AlbumSecondaryType},
artist::ArtistId,
musicbrainz::{MbAlbumRef, MbArtistRef, MbRefOption},
artist::{ArtistId, ArtistInfo},
musicbrainz::{MbAlbumRef, MbRefOption},
Collection,
},
interface::{database::IDatabase, library::ILibrary},
@ -21,11 +21,8 @@ pub trait IMusicHoard {
fn reload_database(&mut self) -> Result<(), musichoard::Error>;
fn get_collection(&self) -> &Collection;
fn set_artist_musicbrainz(
&mut self,
id: &ArtistId,
mbref: MbRefOption<MbArtistRef>,
) -> Result<(), musichoard::Error>;
fn set_artist_info(&mut self, id: &ArtistId, info: ArtistInfo)
-> Result<(), musichoard::Error>;
fn set_album_musicbrainz(
&mut self,
artist_id: &ArtistId,
@ -60,12 +57,12 @@ impl<Database: IDatabase, Library: ILibrary> IMusicHoard for MusicHoard<Database
<Self as IMusicHoardBase>::get_collection(self)
}
fn set_artist_musicbrainz(
fn set_artist_info(
&mut self,
id: &ArtistId,
mbref: MbRefOption<MbArtistRef>,
info: ArtistInfo,
) -> Result<(), musichoard::Error> {
<Self as IMusicHoardDatabase>::set_artist_musicbrainz(self, id, mbref)
<Self as IMusicHoardDatabase>::set_artist_info(self, id, info)
}
fn set_album_musicbrainz(