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,
};
use super::browse::{FetchError, FetchReceiver};
use super::browse::FetchReceiver;
impl AppArtistMatches {
fn len(&self) -> usize {
@ -66,10 +66,7 @@ impl AppMachine<AppMatches> {
}
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<AppMatches> {
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<Self, FetchError>;
fn fetch(self) -> App;
}
impl IAppInteractMatchesPrivate for AppMatches {
fn next_matches_info(mut self) -> Result<Self, FetchError> {
(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<AppMatches> {
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(),
}
}
}