Don't repeat yourself
Some checks failed
Cargo CI / Build and Test (pull_request) Failing after 1m40s
Cargo CI / Lint (pull_request) Successful in 1m5s

This commit is contained in:
Wojciech Kozlowski 2024-09-01 14:57:01 +02:00
parent c007813421
commit f4068e5f77

View File

@ -6,7 +6,7 @@ use crate::tui::app::{
IAppInteractMatches, MatchOption, WidgetState, IAppInteractMatches, MatchOption, WidgetState,
}; };
use super::browse::{FetchError, FetchReceiver}; use super::browse::FetchReceiver;
impl AppArtistMatches { impl AppArtistMatches {
fn len(&self) -> usize { fn len(&self) -> usize {
@ -66,10 +66,7 @@ impl AppMachine<AppMatches> {
} }
pub fn app_matches(inner: AppInner, matches_rx: FetchReceiver) -> App { pub fn app_matches(inner: AppInner, matches_rx: FetchReceiver) -> App {
match AppMatches::new(matches_rx).next_matches_info() { AppMachine::matches(inner, AppMatches::new(matches_rx)).fetch()
Ok(state) => AppMachine::matches(inner, state).into(),
Err(err) => AppMachine::error(inner, format!("fetch failed: {err}")).into(),
}
} }
} }
@ -117,17 +114,8 @@ impl IAppInteractMatches for AppMachine<AppMatches> {
self.into() self.into()
} }
fn select(mut self) -> Self::APP { fn select(self) -> Self::APP {
match self.state.next_matches_info() { self.fetch()
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 abort(self) -> Self::APP { fn abort(self) -> Self::APP {
@ -143,23 +131,24 @@ trait IAppInteractMatchesPrivate
where where
Self: Sized, Self: Sized,
{ {
fn next_matches_info(self) -> Result<Self, FetchError>; fn fetch(self) -> App;
} }
impl IAppInteractMatchesPrivate for AppMatches { impl IAppInteractMatchesPrivate for AppMachine<AppMatches> {
fn next_matches_info(mut self) -> Result<Self, FetchError> { fn fetch(mut self) -> App {
(self.current, self.state) = match self.matches_rx.recv() { match self.state.matches_rx.recv() {
Ok(fetch_result) => { Ok(fetch_result) => match fetch_result {
let mut next_match = fetch_result?; Ok(mut next_match) => {
next_match.push_cannot_have_mbid(); next_match.push_cannot_have_mbid();
let mut state = WidgetState::default(); self.state.current = Some(next_match);
state.list.select(Some(0)); self.state.state.list.select(Some(0));
(Some(next_match), state) 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(),
} }
Err(_) => (None, WidgetState::default()),
};
Ok(self)
} }
} }