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 { impl ArtistInfo {
pub fn new() -> Self {
ArtistInfo {
musicbrainz: MbRefOption::None,
properties: HashMap::new(),
}
}
pub fn set_musicbrainz_ref(&mut self, mbref: MbRefOption<MbArtistRef>) { pub fn set_musicbrainz_ref(&mut self, mbref: MbRefOption<MbArtistRef>) {
self.musicbrainz = mbref self.musicbrainz = mbref
} }

View File

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

View File

@ -2,7 +2,7 @@ use std::cmp;
use musichoard::collection::{ use musichoard::collection::{
album::AlbumMeta, album::AlbumMeta,
artist::{ArtistId, ArtistMeta}, artist::{ArtistId, ArtistInfo, ArtistMeta},
musicbrainz::{MbRefOption, Mbid}, musicbrainz::{MbRefOption, Mbid},
}; };
@ -26,12 +26,13 @@ macro_rules! item_option_artist_set {
music_hoard: &mut dyn IMusicHoard, music_hoard: &mut dyn IMusicHoard,
meta: &ArtistMeta, meta: &ArtistMeta,
) -> Result<(), musichoard::Error> { ) -> Result<(), musichoard::Error> {
let mbref = match self { let mut info = ArtistInfo::new();
info.musicbrainz = match self {
MatchOption::Some(m) => m.item.info.musicbrainz, MatchOption::Some(m) => m.item.info.musicbrainz,
MatchOption::CannotHaveMbid => MbRefOption::CannotHaveMbid, MatchOption::CannotHaveMbid => MbRefOption::CannotHaveMbid,
MatchOption::ManualInputMbid => panic!(), 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::{ use musichoard::{
collection::{ collection::{
album::{AlbumId, AlbumPrimaryType, AlbumSecondaryType}, album::{AlbumId, AlbumPrimaryType, AlbumSecondaryType},
artist::ArtistId, artist::{ArtistId, ArtistInfo},
musicbrainz::{MbAlbumRef, MbArtistRef, MbRefOption}, musicbrainz::{MbAlbumRef, MbRefOption},
Collection, Collection,
}, },
interface::{database::IDatabase, library::ILibrary}, interface::{database::IDatabase, library::ILibrary},
@ -21,11 +21,8 @@ pub trait IMusicHoard {
fn reload_database(&mut self) -> Result<(), musichoard::Error>; fn reload_database(&mut self) -> Result<(), musichoard::Error>;
fn get_collection(&self) -> &Collection; fn get_collection(&self) -> &Collection;
fn set_artist_musicbrainz( fn set_artist_info(&mut self, id: &ArtistId, info: ArtistInfo)
&mut self, -> Result<(), musichoard::Error>;
id: &ArtistId,
mbref: MbRefOption<MbArtistRef>,
) -> Result<(), musichoard::Error>;
fn set_album_musicbrainz( fn set_album_musicbrainz(
&mut self, &mut self,
artist_id: &ArtistId, artist_id: &ArtistId,
@ -60,12 +57,12 @@ impl<Database: IDatabase, Library: ILibrary> IMusicHoard for MusicHoard<Database
<Self as IMusicHoardBase>::get_collection(self) <Self as IMusicHoardBase>::get_collection(self)
} }
fn set_artist_musicbrainz( fn set_artist_info(
&mut self, &mut self,
id: &ArtistId, id: &ArtistId,
mbref: MbRefOption<MbArtistRef>, info: ArtistInfo,
) -> Result<(), musichoard::Error> { ) -> 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( fn set_album_musicbrainz(