From f4068e5f779524fd0d73b9732cfd44758e98ae7c Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Sun, 1 Sep 2024 14:57:01 +0200 Subject: [PATCH] Don't repeat yourself --- src/tui/app/machine/matches.rs | 51 +++++++++++++--------------------- 1 file changed, 20 insertions(+), 31 deletions(-) diff --git a/src/tui/app/machine/matches.rs b/src/tui/app/machine/matches.rs index ea4bced..46f240d 100644 --- a/src/tui/app/machine/matches.rs +++ b/src/tui/app/machine/matches.rs @@ -6,7 +6,7 @@ use crate::tui::app::{ IAppInteractMatches, MatchOption, WidgetState, }; -use super::browse::{FetchError, FetchReceiver}; +use super::browse::FetchReceiver; impl AppArtistMatches { fn len(&self) -> usize { @@ -66,10 +66,7 @@ impl AppMachine { } pub fn app_matches(inner: AppInner, matches_rx: FetchReceiver) -> App { - match AppMatches::new(matches_rx).next_matches_info() { - Ok(state) => AppMachine::matches(inner, state).into(), - Err(err) => AppMachine::error(inner, format!("fetch failed: {err}")).into(), - } + AppMachine::matches(inner, AppMatches::new(matches_rx)).fetch() } } @@ -117,17 +114,8 @@ impl IAppInteractMatches for AppMachine { self.into() } - fn select(mut self) -> Self::APP { - match self.state.next_matches_info() { - Ok(state) => { - self.state = state; - match self.state.current { - Some(_) => self.into(), - None => AppMachine::browse(self.inner).into(), - } - } - Err(err) => AppMachine::error(self.inner, format!("fetch failed: {err}")).into(), - } + fn select(self) -> Self::APP { + self.fetch() } fn abort(self) -> Self::APP { @@ -143,23 +131,24 @@ trait IAppInteractMatchesPrivate where Self: Sized, { - fn next_matches_info(self) -> Result; + fn fetch(self) -> App; } -impl IAppInteractMatchesPrivate for AppMatches { - fn next_matches_info(mut self) -> Result { - (self.current, self.state) = match self.matches_rx.recv() { - Ok(fetch_result) => { - let mut next_match = fetch_result?; - next_match.push_cannot_have_mbid(); - let mut state = WidgetState::default(); - state.list.select(Some(0)); - (Some(next_match), state) - } - Err(_) => (None, WidgetState::default()), - }; - - Ok(self) +impl IAppInteractMatchesPrivate for AppMachine { + fn fetch(mut self) -> App { + match self.state.matches_rx.recv() { + Ok(fetch_result) => match fetch_result { + Ok(mut next_match) => { + next_match.push_cannot_have_mbid(); + self.state.current = Some(next_match); + self.state.state.list.select(Some(0)); + AppMachine::matches(self.inner, self.state).into() + } + Err(err) => AppMachine::error(self.inner, format!("fetch failed: {err}")).into(), + }, + // only happens when the sender disconnects which means it finished its job + Err(_) => AppMachine::browse(self.inner).into(), + } } }