Compare commits

...

3 Commits

Author SHA1 Message Date
6b90f470be Simplify
Some checks failed
Cargo CI / Build and Test (pull_request) Failing after 1m47s
Cargo CI / Lint (pull_request) Successful in 1m8s
2024-09-28 14:11:07 +02:00
c130519adf Small clean up 2024-09-28 12:15:42 +02:00
b0ac9bf2c1 Simplify fetch 2024-09-28 12:05:16 +02:00
2 changed files with 111 additions and 135 deletions

View File

@ -4,9 +4,9 @@ use std::{
};
use musichoard::collection::{
album::AlbumMeta,
album::{Album, AlbumMeta},
artist::{Artist, ArtistId, ArtistMeta},
musicbrainz::{IMusicBrainzRef, MbRefOption, Mbid},
musicbrainz::{IMusicBrainzRef, MbArtistRef, MbRefOption, Mbid},
};
use crate::tui::{
@ -158,21 +158,10 @@ impl AppMachine<FetchState> {
) -> Result<(), FetchError> {
let requests = match artist.meta.info.musicbrainz {
MbRefOption::Some(ref arid) => {
let arid = arid.mbid();
let albums = artist.albums.iter();
albums
.filter(|album| matches!(album.meta.info.musicbrainz, MbRefOption::None))
.map(|album| {
MbParams::search_release_group(
artist.meta.id.clone(),
arid.clone(),
album.meta.clone(),
)
})
.collect()
Self::fetch_albums_requests(&artist.meta.id, arid, &artist.albums)
}
MbRefOption::CannotHaveMbid => VecDeque::new(),
MbRefOption::None => VecDeque::from([MbParams::search_artist(artist.meta.clone())]),
MbRefOption::None => Self::fetch_artist_request(&artist.meta),
};
if requests.is_empty() {
return Err(FetchError::NothingToFetch);
@ -180,6 +169,25 @@ impl AppMachine<FetchState> {
Ok(musicbrainz.submit_background_job(result_sender, requests)?)
}
fn fetch_albums_requests(
artist: &ArtistId,
arid: &MbArtistRef,
albums: &[Album],
) -> VecDeque<MbParams> {
let arid = arid.mbid();
albums
.iter()
.filter(|album| matches!(album.meta.info.musicbrainz, MbRefOption::None))
.map(|album| {
MbParams::search_release_group(artist.clone(), arid.clone(), album.meta.clone())
})
.collect()
}
fn fetch_artist_request(meta: &ArtistMeta) -> VecDeque<MbParams> {
VecDeque::from([MbParams::search_artist(meta.clone())])
}
fn submit_lookup_artist_job(
musicbrainz: &dyn IMbJobSender,
result_sender: ResultSender,

View File

@ -1,8 +1,8 @@
use std::cmp;
use musichoard::collection::{
album::AlbumMeta,
artist::{ArtistId, ArtistMeta},
album::{AlbumInfo, AlbumMeta},
artist::{ArtistInfo, ArtistMeta},
musicbrainz::{MbRefOption, Mbid},
};
@ -12,64 +12,70 @@ use crate::tui::{
AlbumMatches, AppPublicState, AppState, ArtistMatches, IAppInteractMatch, ListOption,
MatchOption, MatchStateInfo, MatchStatePublic, WidgetState,
},
lib::{
interface::musicbrainz::api::{Lookup, Match},
IMusicHoard,
},
lib::interface::musicbrainz::api::{Lookup, Match},
};
macro_rules! item_option_artist_set {
trait GetInfoMeta {
type InfoType;
}
impl GetInfoMeta for ArtistMeta {
type InfoType = ArtistInfo;
}
impl GetInfoMeta for AlbumMeta {
type InfoType = AlbumInfo;
}
trait GetInfo {
type InfoType;
fn into_info(self, info: Self::InfoType) -> InfoOption<Self::InfoType>;
}
enum InfoOption<T> {
Info(T),
NeedInput,
}
macro_rules! impl_match_option_artist_into_info {
($holder:ident) => {
impl MatchOption<$holder<ArtistMeta>> {
fn set(
self,
music_hoard: &mut dyn IMusicHoard,
meta: &ArtistMeta,
) -> Result<(), musichoard::Error> {
let mut info = meta.info.clone();
info.musicbrainz = match self {
MatchOption::Some(m) => m.item.info.musicbrainz,
MatchOption::CannotHaveMbid => MbRefOption::CannotHaveMbid,
MatchOption::ManualInputMbid => panic!(),
};
music_hoard.set_artist_info(&meta.id, info)
impl GetInfo for MatchOption<$holder<ArtistMeta>> {
type InfoType = ArtistInfo;
fn into_info(self, mut info: Self::InfoType) -> InfoOption<Self::InfoType> {
match self {
MatchOption::Some(option) => info.musicbrainz = option.item.info.musicbrainz,
MatchOption::CannotHaveMbid => info.musicbrainz = MbRefOption::CannotHaveMbid,
MatchOption::ManualInputMbid => return InfoOption::NeedInput,
}
InfoOption::Info(info)
}
}
};
}
item_option_artist_set!(Lookup);
item_option_artist_set!(Match);
impl_match_option_artist_into_info!(Match);
impl_match_option_artist_into_info!(Lookup);
macro_rules! item_option_album_set {
macro_rules! impl_match_option_album_into_info {
($holder:ident) => {
impl MatchOption<$holder<AlbumMeta>> {
fn set(
self,
music_hoard: &mut dyn IMusicHoard,
artist: &ArtistId,
meta: &AlbumMeta,
) -> Result<(), musichoard::Error> {
let mut info = meta.info.clone();
(info.musicbrainz, info.primary_type, info.secondary_types) = match self {
MatchOption::Some(m) => (
m.item.info.musicbrainz,
m.item.info.primary_type,
m.item.info.secondary_types,
),
MatchOption::CannotHaveMbid => (MbRefOption::CannotHaveMbid, None, Vec::new()),
MatchOption::ManualInputMbid => panic!(),
};
music_hoard.set_album_info(artist, &meta.id, info)
impl GetInfo for MatchOption<$holder<AlbumMeta>> {
type InfoType = AlbumInfo;
fn into_info(self, mut info: Self::InfoType) -> InfoOption<Self::InfoType> {
match self {
MatchOption::Some(option) => info = option.item.info,
MatchOption::CannotHaveMbid => info.musicbrainz = MbRefOption::CannotHaveMbid,
MatchOption::ManualInputMbid => return InfoOption::NeedInput,
}
InfoOption::Info(info)
}
}
};
}
item_option_album_set!(Lookup);
item_option_album_set!(Match);
impl_match_option_album_into_info!(Match);
impl_match_option_album_into_info!(Lookup);
impl<T: PartialEq> ListOption<T> {
impl<T> ListOption<T> {
fn len(&self) -> usize {
match self {
ListOption::Lookup(list) => list.len(),
@ -90,40 +96,24 @@ impl<T: PartialEq> ListOption<T> {
ListOption::Search(list) => list.push(MatchOption::ManualInputMbid),
}
}
fn is_manual_input_mbid(&self, index: usize) -> bool {
match self {
ListOption::Lookup(list) => list.get(index) == Some(&MatchOption::ManualInputMbid),
ListOption::Search(list) => list.get(index) == Some(&MatchOption::ManualInputMbid),
}
}
}
impl ListOption<ArtistMeta> {
fn set(
self,
music_hoard: &mut dyn IMusicHoard,
meta: &ArtistMeta,
index: usize,
) -> Result<(), musichoard::Error> {
match self {
ListOption::Lookup(mut list) => list.swap_remove(index).set(music_hoard, meta),
ListOption::Search(mut list) => list.swap_remove(index).set(music_hoard, meta),
}
}
trait ExtractInfo {
type InfoType;
fn extract_info(&mut self, index: usize, info: Self::InfoType) -> InfoOption<Self::InfoType>;
}
impl ListOption<AlbumMeta> {
fn set(
self,
music_hoard: &mut dyn IMusicHoard,
artist: &ArtistId,
meta: &AlbumMeta,
index: usize,
) -> Result<(), musichoard::Error> {
impl<T: GetInfoMeta> ExtractInfo for ListOption<T>
where
MatchOption<Match<T>>: GetInfo<InfoType = T::InfoType>,
MatchOption<Lookup<T>>: GetInfo<InfoType = T::InfoType>,
{
type InfoType = T::InfoType;
fn extract_info(&mut self, index: usize, info: Self::InfoType) -> InfoOption<Self::InfoType> {
match self {
ListOption::Lookup(mut list) => list.swap_remove(index).set(music_hoard, artist, meta),
ListOption::Search(mut list) => list.swap_remove(index).set(music_hoard, artist, meta),
ListOption::Lookup(ref mut list) => list.swap_remove(index).into_info(info),
ListOption::Search(ref mut list) => list.swap_remove(index).into_info(info),
}
}
}
@ -140,14 +130,6 @@ impl ArtistMatches {
fn push_manual_input_mbid(&mut self) {
self.list.push_manual_input_mbid();
}
fn is_manual_input_mbid(&self, index: usize) -> bool {
self.list.is_manual_input_mbid(index)
}
fn set(self, music_hoard: &mut dyn IMusicHoard, index: usize) -> Result<(), musichoard::Error> {
self.list.set(music_hoard, &self.matching, index)
}
}
impl AlbumMatches {
@ -162,15 +144,6 @@ impl AlbumMatches {
fn push_manual_input_mbid(&mut self) {
self.list.push_manual_input_mbid();
}
fn is_manual_input_mbid(&self, index: usize) -> bool {
self.list.is_manual_input_mbid(index)
}
fn set(self, music_hoard: &mut dyn IMusicHoard, index: usize) -> Result<(), musichoard::Error> {
self.list
.set(music_hoard, &self.artist, &self.matching, index)
}
}
impl MatchStateInfo {
@ -194,20 +167,6 @@ impl MatchStateInfo {
Self::Album(a) => a.push_manual_input_mbid(),
}
}
fn is_manual_input_mbid(&self, index: usize) -> bool {
match self {
Self::Artist(a) => a.is_manual_input_mbid(index),
Self::Album(a) => a.is_manual_input_mbid(index),
}
}
fn set(self, music_hoard: &mut dyn IMusicHoard, index: usize) -> Result<(), musichoard::Error> {
match self {
Self::Artist(a) => a.set(music_hoard, index),
Self::Album(a) => a.set(music_hoard, index),
}
}
}
pub struct MatchState {
@ -260,6 +219,11 @@ impl AppMachine<MatchState> {
}
}
}
fn get_input(mut self) -> App {
self.input.replace(Input::default());
self.into()
}
}
impl From<AppMachine<MatchState>> for App {
@ -307,23 +271,27 @@ impl IAppInteractMatch for AppMachine<MatchState> {
if let Some(index) = self.state.state.list.selected() {
// selected() implies current exists
if self
.state
.current
.as_ref()
.unwrap()
.is_manual_input_mbid(index)
{
self.input.replace(Input::default());
return self.into();
}
let mh = &mut self.inner.music_hoard;
let result = match self.state.current.as_mut().unwrap() {
MatchStateInfo::Artist(ref mut matches) => {
let info: ArtistInfo = matches.matching.info.clone();
match matches.list.extract_info(index, info) {
InfoOption::Info(info) => mh.set_artist_info(&matches.matching.id, info),
InfoOption::NeedInput => return self.get_input(),
}
}
MatchStateInfo::Album(matches) => {
let info: AlbumInfo = matches.matching.info.clone();
match matches.list.extract_info(index, info) {
InfoOption::Info(info) => {
mh.set_album_info(&matches.artist, &matches.matching.id, info)
}
InfoOption::NeedInput => return self.get_input(),
}
}
};
if let Err(err) = self
.state
.current
.unwrap()
.set(&mut *self.inner.music_hoard, index)
{
if let Err(err) = result {
return AppMachine::error_state(self.inner, err.to_string()).into();
}
}