From 3763abfb1785f3f5207a98824cd47e955ff90233 Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Sun, 6 Oct 2024 16:39:58 +0200 Subject: [PATCH] Streamline code --- src/tui/app/machine/fetch_state.rs | 107 +++++++++++------------------ 1 file changed, 41 insertions(+), 66 deletions(-) diff --git a/src/tui/app/machine/fetch_state.rs b/src/tui/app/machine/fetch_state.rs index 0a23524..dd8cbb2 100644 --- a/src/tui/app/machine/fetch_state.rs +++ b/src/tui/app/machine/fetch_state.rs @@ -60,17 +60,6 @@ impl FetchState { } } -enum FetchError { - NothingToFetch, - SubmitError(DaemonError), -} - -impl From for FetchError { - fn from(value: DaemonError) -> Self { - FetchError::SubmitError(value) - } -} - impl AppMachine { fn fetch_state(inner: AppInner, state: FetchState) -> Self { AppMachine::new(inner, state) @@ -81,48 +70,21 @@ impl AppMachine { } fn app_fetch_new(inner: AppInner) -> App { - let coll = inner.music_hoard.get_collection(); - - let artist = match inner.selection.state_artist(coll) { - Some(artist_state) => &coll[artist_state.index], - None => { - let err = "cannot fetch artist: no artist selected"; - return AppMachine::error_state(inner, err).into(); - } + let requests = match Self::search_job(&inner) { + Ok(requests) => requests, + Err(err) => return AppMachine::error_state(inner, err.to_string()).into(), }; + if requests.is_empty() { + return AppMachine::browse_state(inner).into(); + } + let (fetch_tx, fetch_rx) = mpsc::channel::(); let fetch = FetchState::search(fetch_rx); - let mb = &*inner.musicbrainz; - let result = match inner.selection.category() { - Category::Artist => Self::submit_search_artist_job(mb, fetch_tx, artist), - _ => { - let arid = match artist.meta.info.musicbrainz { - MbRefOption::Some(ref mbref) => mbref, - _ => { - let err = "cannot fetch album: artist has no MBID"; - return AppMachine::error_state(inner, err).into(); - } - }; - let album = match inner.selection.state_album(coll) { - Some(album_state) => &artist.albums[album_state.index], - None => { - let err = "cannot fetch album: no album selected"; - return AppMachine::error_state(inner, err).into(); - } - }; - let artist_id = &artist.meta.id; - Self::submit_search_release_group_job(mb, fetch_tx, artist_id, arid, album) - } - }; - - match result { + match inner.musicbrainz.submit_background_job(fetch_tx, requests) { Ok(()) => AppMachine::fetch_state(inner, fetch).into(), - Err(FetchError::NothingToFetch) => AppMachine::browse_state(inner).into(), - Err(FetchError::SubmitError(daemon_err)) => { - AppMachine::error_state(inner, daemon_err.to_string()).into() - } + Err(err) => AppMachine::error_state(inner, err.to_string()).into(), } } @@ -191,36 +153,49 @@ impl AppMachine { Self::app_fetch_next(inner, fetch) } - fn submit_search_artist_job( - musicbrainz: &dyn IMbJobSender, - result_sender: ResultSender, - artist: &Artist, - ) -> Result<(), FetchError> { - let requests = match artist.meta.info.musicbrainz { + fn search_job(inner: &AppInner) -> Result, &'static str> { + let coll = inner.music_hoard.get_collection(); + + let artist = match inner.selection.state_artist(coll) { + Some(artist_state) => &coll[artist_state.index], + None => return Err("cannot fetch: no artist selected"), + }; + + let requests = match inner.selection.category() { + Category::Artist => Self::search_artist_job(artist), + _ => { + let arid = match artist.meta.info.musicbrainz { + MbRefOption::Some(ref mbref) => mbref, + _ => return Err("cannot fetch album: artist has no MBID"), + }; + let album = match inner.selection.state_album(coll) { + Some(album_state) => &artist.albums[album_state.index], + None => return Err("cannot fetch album: no album selected"), + }; + let artist_id = &artist.meta.id; + Self::search_release_group_job(artist_id, arid, album) + } + }; + + Ok(requests) + } + + fn search_artist_job(artist: &Artist) -> VecDeque { + match artist.meta.info.musicbrainz { MbRefOption::Some(ref arid) => { Self::search_albums_requests(&artist.meta.id, arid, &artist.albums) } MbRefOption::CannotHaveMbid => VecDeque::new(), MbRefOption::None => Self::search_artist_request(&artist.meta), - }; - if requests.is_empty() { - return Err(FetchError::NothingToFetch); } - Ok(musicbrainz.submit_background_job(result_sender, requests)?) } - fn submit_search_release_group_job( - musicbrainz: &dyn IMbJobSender, - result_sender: ResultSender, + fn search_release_group_job( artist_id: &ArtistId, artist_mbid: &MbArtistRef, album: &Album, - ) -> Result<(), FetchError> { - let requests = Self::search_albums_requests(artist_id, artist_mbid, slice::from_ref(album)); - if requests.is_empty() { - return Err(FetchError::NothingToFetch); - } - Ok(musicbrainz.submit_background_job(result_sender, requests)?) + ) -> VecDeque { + Self::search_albums_requests(artist_id, artist_mbid, slice::from_ref(album)) } fn search_albums_requests(