Clean up a bit
Some checks failed
Cargo CI / Build and Test (pull_request) Failing after 2m22s
Cargo CI / Lint (pull_request) Successful in 1m5s

This commit is contained in:
Wojciech Kozlowski 2024-10-06 22:15:46 +02:00
parent 37c4350149
commit 69d941f1c5
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,46 +176,45 @@ 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),
}
}
fn apply_album_results(
inner: &mut AppInner,
fetch_albums: Vec<AlbumMeta>,
) -> Result<(), musichoard::Error> {
let coll = inner.music_hoard.get_collection(); let coll = inner.music_hoard.get_collection();
let artist_state = inner.selection.state_artist(coll).unwrap(); let artist_state = inner.selection.state_artist(coll).unwrap();
let artist = &coll[artist_state.index]; let artist = &coll[artist_state.index];
let artist_id = &coll[artist_state.index].meta.id.clone(); let selection = KeySelection::get(coll, &inner.selection);
let old_albums = &artist.albums;
let mut to_be_added = vec![]; let artist_id = &artist.meta.id.clone();
for new in new_albums.into_iter() { for new in Self::new_albums(fetch_albums, &artist.albums).into_iter() {
let mut exists = false; inner.music_hoard.add_album(artist_id, new)?;
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 coll = inner.music_hoard.get_collection();
to_be_added.push(new); inner.selection.select_by_id(coll, selection);
}
}
let previous =
KeySelection::get(inner.music_hoard.get_collection(), &inner.selection);
for new in to_be_added.into_iter() {
inner.music_hoard.add_album(&artist_id, new)?;
}
inner
.selection
.select_by_id(inner.music_hoard.get_collection(), previous);
Ok(()) Ok(())
} }
fn new_albums(fetch_albums: Vec<AlbumMeta>, albums: &[Album]) -> Vec<AlbumMeta> {
let mut new_albums = vec![];
for alb in fetch_albums.into_iter() {
let existing = albums.iter().find(|old| Self::album_match(&old.meta, &alb));
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(
inner: AppInner, inner: AppInner,
@ -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())
}) })