From 6b7e3ad23228f7fa1569cd2953ab424f06e4fe84 Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Sat, 17 Feb 2024 22:53:33 +0100 Subject: [PATCH] Split states into own structs --- src/tui/app/app.rs | 79 +++++++++++++++++++++++++++------------------- src/tui/app/mod.rs | 1 - 2 files changed, 47 insertions(+), 33 deletions(-) diff --git a/src/tui/app/app.rs b/src/tui/app/app.rs index 6dc8361..8a2fc1e 100644 --- a/src/tui/app/app.rs +++ b/src/tui/app/app.rs @@ -18,15 +18,23 @@ struct AppInner { } pub type App = AppState< - AppGenericState, - AppGenericState, - AppGenericState, + AppBrowseState, + AppInfoState, + AppReloadState, AppSearchState, AppErrorState, - AppErrorState, + AppCriticalState, >; -pub struct AppGenericState { +pub struct AppBrowseState { + inner: AppInner, +} + +pub struct AppInfoState { + inner: AppInner, +} + +pub struct AppReloadState { inner: AppInner, } @@ -47,6 +55,11 @@ pub struct AppErrorState { string: String, } +pub struct AppCriticalState { + inner: AppInner, + string: String, +} + impl App { pub fn new(mut music_hoard: MH) -> Self { let init_result = Self::init(&mut music_hoard); @@ -57,8 +70,8 @@ impl App { selection, }; match init_result { - Ok(()) => Self::Browse(AppGenericState { inner }), - Err(err) => Self::Critical(AppErrorState { + Ok(()) => Self::Browse(AppBrowseState { inner }), + Err(err) => Self::Critical(AppCriticalState { inner, string: err.to_string(), }), @@ -73,32 +86,34 @@ impl App { fn inner_ref(&self) -> &AppInner { match self { - AppState::Browse(generic) | AppState::Info(generic) | AppState::Reload(generic) => { - &generic.inner - } + AppState::Browse(browse) => &browse.inner, + AppState::Info(info) => &info.inner, + AppState::Reload(reload) => &reload.inner, AppState::Search(search) => &search.inner, - AppState::Error(error) | AppState::Critical(error) => &error.inner, + AppState::Error(error) => &error.inner, + AppState::Critical(critical) => &critical.inner, } } fn inner_mut(&mut self) -> &mut AppInner { match self { - AppState::Browse(generic) | AppState::Info(generic) | AppState::Reload(generic) => { - &mut generic.inner - } + AppState::Browse(browse) => &mut browse.inner, + AppState::Info(info) => &mut info.inner, + AppState::Reload(reload) => &mut reload.inner, AppState::Search(search) => &mut search.inner, - AppState::Error(error) | AppState::Critical(error) => &mut error.inner, + AppState::Error(error) => &mut error.inner, + AppState::Critical(critical) => &mut critical.inner, } } } impl IAppInteract for App { - type BS = AppGenericState; - type IS = AppGenericState; - type RS = AppGenericState; + type BS = AppBrowseState; + type IS = AppInfoState; + type RS = AppReloadState; type SS = AppSearchState; type ES = AppErrorState; - type CS = AppErrorState; + type CS = AppCriticalState; fn is_running(&self) -> bool { self.inner_ref().running @@ -114,7 +129,7 @@ impl IAppInteract for App { } } -impl IAppInteractBrowse for AppGenericState { +impl IAppInteractBrowse for AppBrowseState { type APP = App; fn save_and_quit(mut self) -> Self::APP { @@ -155,11 +170,11 @@ impl IAppInteractBrowse for AppGenericState { } fn show_info_overlay(self) -> Self::APP { - AppState::Info(self) + AppState::Info(AppInfoState { inner: self.inner }) } fn show_reload_menu(self) -> Self::APP { - AppState::Reload(self) + AppState::Reload(AppReloadState { inner: self.inner }) } fn begin_search(mut self) -> Self::APP { @@ -180,11 +195,11 @@ impl IAppInteractBrowse for AppGenericState { } } -impl IAppInteractInfo for AppGenericState { +impl IAppInteractInfo for AppInfoState { type APP = App; fn hide_info_overlay(self) -> Self::APP { - AppState::Browse(AppGenericState { inner: self.inner }) + AppState::Browse(AppBrowseState { inner: self.inner }) } fn no_op(self) -> Self::APP { @@ -192,7 +207,7 @@ impl IAppInteractInfo for AppGenericState { } } -impl IAppInteractReload for AppGenericState { +impl IAppInteractReload for AppReloadState { type APP = App; fn reload_library(mut self) -> Self::APP { @@ -214,7 +229,7 @@ impl IAppInteractReload for AppGenericState { } fn hide_reload_menu(self) -> Self::APP { - AppState::Browse(AppGenericState { inner: self.inner }) + AppState::Browse(AppBrowseState { inner: self.inner }) } fn no_op(self) -> Self::APP { @@ -226,14 +241,14 @@ trait IAppInteractReloadPrivate { fn refresh(self, previous: IdSelection, result: Result<(), musichoard::Error>) -> App; } -impl IAppInteractReloadPrivate for AppGenericState { +impl IAppInteractReloadPrivate for AppReloadState { fn refresh(mut self, previous: IdSelection, result: Result<(), musichoard::Error>) -> App { match result { Ok(()) => { self.inner .selection .select_by_id(self.inner.music_hoard.get_collection(), previous); - AppState::Browse(AppGenericState { inner: self.inner }) + AppState::Browse(AppBrowseState { inner: self.inner }) } Err(err) => AppState::Error(AppErrorState { inner: self.inner, @@ -281,12 +296,12 @@ impl IAppInteractSearch for AppSearchState { } fn finish_search(self) -> Self::APP { - AppState::Browse(AppGenericState { inner: self.inner }) + AppState::Browse(AppBrowseState { inner: self.inner }) } fn cancel_search(mut self) -> Self::APP { self.inner.selection.select_by_list(self.orig); - AppState::Browse(AppGenericState { inner: self.inner }) + AppState::Browse(AppBrowseState { inner: self.inner }) } fn no_op(self) -> Self::APP { @@ -298,11 +313,11 @@ impl IAppInteractError for AppErrorState { type APP = App; fn dismiss_error(self) -> Self::APP { - AppState::Browse(AppGenericState { inner: self.inner }) + AppState::Browse(AppBrowseState { inner: self.inner }) } } -impl IAppInteractCritical for AppErrorState { +impl IAppInteractCritical for AppCriticalState { type APP = App; fn no_op(self) -> Self::APP { diff --git a/src/tui/app/mod.rs b/src/tui/app/mod.rs index 2cb6d31..d0f4caf 100644 --- a/src/tui/app/mod.rs +++ b/src/tui/app/mod.rs @@ -29,7 +29,6 @@ pub trait IAppInteract { fn state(self) -> AppState; } -// FIXME: move to returning specific states - for errors, return Result pub trait IAppInteractBrowse { type APP: IAppInteract;