Clean up a bit

This commit is contained in:
Wojciech Kozlowski 2024-10-06 22:15:46 +02:00
parent 73aab07d9b
commit 799de9c003
2 changed files with 40 additions and 33 deletions

View File

@ -46,6 +46,14 @@ pub enum MbRefOption<T> {
} }
impl<T> MbRefOption<T> { impl<T> MbRefOption<T> {
pub fn is_some(&self) -> bool {
matches!(self, MbRefOption::Some(_))
}
pub fn is_none(&self) -> bool {
matches!(self, MbRefOption::None)
}
pub fn or(self, optb: MbRefOption<T>) -> MbRefOption<T> { pub fn or(self, optb: MbRefOption<T>) -> MbRefOption<T> {
match (&self, &optb) { match (&self, &optb) {
(MbRefOption::Some(_), _) | (MbRefOption::CannotHaveMbid, MbRefOption::None) => self, (MbRefOption::Some(_), _) | (MbRefOption::CannotHaveMbid, MbRefOption::None) => self,

View File

@ -5,7 +5,7 @@ use std::{
}; };
use musichoard::collection::{ use musichoard::collection::{
album::{Album, AlbumId}, album::{Album, AlbumId, AlbumMeta},
artist::{Artist, ArtistId, ArtistMeta}, artist::{Artist, ArtistId, ArtistMeta},
musicbrainz::{IMusicBrainzRef, MbArtistRef, MbRefOption, Mbid}, musicbrainz::{IMusicBrainzRef, MbArtistRef, MbRefOption, Mbid},
}; };
@ -176,45 +176,44 @@ impl AppMachine<FetchState> {
list: EntityList, list: EntityList,
) -> Result<(), musichoard::Error> { ) -> Result<(), musichoard::Error> {
match list { match list {
EntityList::Album(new_albums) => { EntityList::Album(fetch_albums) => Self::apply_album_results(inner, fetch_albums),
let coll = inner.music_hoard.get_collection(); }
let artist_state = inner.selection.state_artist(coll).unwrap(); }
let artist = &coll[artist_state.index]; fn apply_album_results(
let artist_id = &coll[artist_state.index].meta.id.clone(); inner: &mut AppInner,
let old_albums = &artist.albums; fetch_albums: Vec<AlbumMeta>,
) -> Result<(), musichoard::Error> {
let coll = inner.music_hoard.get_collection();
let mut to_be_added = vec![]; let artist_state = inner.selection.state_artist(coll).unwrap();
for new in new_albums.into_iter() { let artist = &coll[artist_state.index];
let mut exists = false; let selection = KeySelection::get(coll, &inner.selection);
for old in old_albums.iter() {
if matches!(old.meta.info.musicbrainz, MbRefOption::Some(_)) {
if new.info.musicbrainz == old.meta.info.musicbrainz {
exists = true;
break;
}
}
}
if !exists { let artist_id = &artist.meta.id.clone();
to_be_added.push(new); for new in Self::new_albums(fetch_albums, &artist.albums).into_iter() {
} inner.music_hoard.add_album(artist_id, new)?;
} }
let previous = let coll = inner.music_hoard.get_collection();
KeySelection::get(inner.music_hoard.get_collection(), &inner.selection); inner.selection.select_by_id(coll, selection);
for new in to_be_added.into_iter() { Ok(())
inner.music_hoard.add_album(&artist_id, new)?; }
}
inner fn new_albums(fetch_albums: Vec<AlbumMeta>, albums: &[Album]) -> Vec<AlbumMeta> {
.selection let mut new_albums = vec![];
.select_by_id(inner.music_hoard.get_collection(), previous); for alb in fetch_albums.into_iter() {
let existing = albums.iter().find(|old| Self::album_match(&old.meta, &alb));
Ok(()) if existing.is_none() {
new_albums.push(alb);
} }
} }
new_albums
}
fn album_match(old: &AlbumMeta, new: &AlbumMeta) -> bool {
old.info.musicbrainz.is_some() && (old.info.musicbrainz == new.info.musicbrainz)
} }
pub fn app_lookup_artist( pub fn app_lookup_artist(
@ -288,7 +287,7 @@ impl AppMachine<FetchState> {
let arid = arid.mbid(); let arid = arid.mbid();
albums albums
.iter() .iter()
.filter(|album| matches!(album.meta.info.musicbrainz, MbRefOption::None)) .filter(|album| album.meta.info.musicbrainz.is_none())
.map(|album| { .map(|album| {
MbParams::search_release_group(artist.clone(), arid.clone(), album.meta.clone()) MbParams::search_release_group(artist.clone(), arid.clone(), album.meta.clone())
}) })