Convenience

This commit is contained in:
Wojciech Kozlowski 2024-02-17 23:32:06 +01:00
parent 8ca6c4d36b
commit a68063e7c8

View File

@ -33,10 +33,55 @@ pub struct AppMachine<MH: IMusicHoard, STATE> {
pub struct AppBrowse;
impl<MH: IMusicHoard> AppMachine<MH, AppBrowse> {
fn browse(inner: AppInner<MH>) -> Self {
AppMachine {
inner,
state: AppBrowse,
}
}
}
impl<MH: IMusicHoard> From<AppMachine<MH, AppBrowse>> for App<MH> {
fn from(value: AppMachine<MH, AppBrowse>) -> Self {
AppState::Browse(value)
}
}
pub struct AppInfo;
impl<MH: IMusicHoard> AppMachine<MH, AppInfo> {
fn info(inner: AppInner<MH>) -> Self {
AppMachine {
inner,
state: AppInfo,
}
}
}
impl<MH: IMusicHoard> From<AppMachine<MH, AppInfo>> for App<MH> {
fn from(value: AppMachine<MH, AppInfo>) -> Self {
AppState::Info(value)
}
}
pub struct AppReload;
impl<MH: IMusicHoard> AppMachine<MH, AppReload> {
fn reload(inner: AppInner<MH>) -> Self {
AppMachine {
inner,
state: AppReload,
}
}
}
impl<MH: IMusicHoard> From<AppMachine<MH, AppReload>> for App<MH> {
fn from(value: AppMachine<MH, AppReload>) -> Self {
AppState::Reload(value)
}
}
pub struct AppSearch {
string: String,
orig: ListSelection,
@ -48,14 +93,67 @@ struct AppSearchMemo {
char: bool,
}
impl<MH: IMusicHoard> AppMachine<MH, AppSearch> {
fn search(inner: AppInner<MH>, orig: ListSelection) -> Self {
AppMachine {
inner,
state: AppSearch {
string: String::new(),
orig,
memo: vec![],
},
}
}
}
impl<MH: IMusicHoard> From<AppMachine<MH, AppSearch>> for App<MH> {
fn from(value: AppMachine<MH, AppSearch>) -> Self {
AppState::Search(value)
}
}
pub struct AppError {
string: String,
}
impl<MH: IMusicHoard> AppMachine<MH, AppError> {
fn error<S: Into<String>>(inner: AppInner<MH>, string: S) -> Self {
AppMachine {
inner,
state: AppError {
string: string.into(),
},
}
}
}
impl<MH: IMusicHoard> From<AppMachine<MH, AppError>> for App<MH> {
fn from(value: AppMachine<MH, AppError>) -> Self {
AppState::Error(value)
}
}
pub struct AppCritical {
string: String,
}
impl<MH: IMusicHoard> AppMachine<MH, AppCritical> {
fn critical<S: Into<String>>(inner: AppInner<MH>, string: S) -> Self {
AppMachine {
inner,
state: AppCritical {
string: string.into(),
},
}
}
}
impl<MH: IMusicHoard> From<AppMachine<MH, AppCritical>> for App<MH> {
fn from(value: AppMachine<MH, AppCritical>) -> Self {
AppState::Critical(value)
}
}
impl<MH: IMusicHoard> App<MH> {
pub fn new(mut music_hoard: MH) -> Self {
let init_result = Self::init(&mut music_hoard);
@ -66,16 +164,8 @@ impl<MH: IMusicHoard> App<MH> {
selection,
};
match init_result {
Ok(()) => Self::Browse(AppMachine {
inner,
state: AppBrowse,
}),
Err(err) => Self::Critical(AppMachine {
inner,
state: AppCritical {
string: err.to_string(),
},
}),
Ok(()) => AppMachine::browse(inner).into(),
Err(err) => AppMachine::critical(inner, err.to_string()).into(),
}
}
@ -137,53 +227,42 @@ impl<MH: IMusicHoard> IAppInteractBrowse for AppMachine<MH, AppBrowse> {
match self.inner.music_hoard.save_to_database() {
Ok(_) => {
self.inner.running = false;
AppState::Browse(self)
self.into()
}
Err(err) => AppState::Error(AppMachine {
inner: self.inner,
state: AppError {
string: err.to_string(),
},
}),
Err(err) => AppMachine::error(self.inner, err.to_string()).into(),
}
}
fn increment_category(mut self) -> Self::APP {
self.inner.selection.increment_category();
AppState::Browse(self)
self.into()
}
fn decrement_category(mut self) -> Self::APP {
self.inner.selection.decrement_category();
AppState::Browse(self)
self.into()
}
fn increment_selection(mut self, delta: Delta) -> Self::APP {
self.inner
.selection
.increment_selection(self.inner.music_hoard.get_collection(), delta);
AppState::Browse(self)
self.into()
}
fn decrement_selection(mut self, delta: Delta) -> Self::APP {
self.inner
.selection
.decrement_selection(self.inner.music_hoard.get_collection(), delta);
AppState::Browse(self)
self.into()
}
fn show_info_overlay(self) -> Self::APP {
AppState::Info(AppMachine {
inner: self.inner,
state: AppInfo,
})
AppMachine::info(self.inner).into()
}
fn show_reload_menu(self) -> Self::APP {
AppState::Reload(AppMachine {
inner: self.inner,
state: AppReload,
})
AppMachine::reload(self.inner).into()
}
fn begin_search(mut self) -> Self::APP {
@ -191,18 +270,11 @@ impl<MH: IMusicHoard> IAppInteractBrowse for AppMachine<MH, AppBrowse> {
self.inner
.selection
.reset_artist(self.inner.music_hoard.get_collection());
AppState::Search(AppMachine {
inner: self.inner,
state: AppSearch {
string: String::new(),
orig,
memo: vec![],
},
})
AppMachine::search(self.inner, orig).into()
}
fn no_op(self) -> Self::APP {
AppState::Browse(self)
self.into()
}
}
@ -210,14 +282,11 @@ impl<MH: IMusicHoard> IAppInteractInfo for AppMachine<MH, AppInfo> {
type APP = App<MH>;
fn hide_info_overlay(self) -> Self::APP {
AppState::Browse(AppMachine {
inner: self.inner,
state: AppBrowse,
})
AppMachine::browse(self.inner).into()
}
fn no_op(self) -> Self::APP {
AppState::Info(self)
self.into()
}
}
@ -243,14 +312,11 @@ impl<MH: IMusicHoard> IAppInteractReload for AppMachine<MH, AppReload> {
}
fn hide_reload_menu(self) -> Self::APP {
AppState::Browse(AppMachine {
inner: self.inner,
state: AppBrowse,
})
AppMachine::browse(self.inner).into()
}
fn no_op(self) -> Self::APP {
AppState::Reload(self)
self.into()
}
}
@ -265,17 +331,9 @@ impl<MH: IMusicHoard> IAppInteractReloadPrivate<MH> for AppMachine<MH, AppReload
self.inner
.selection
.select_by_id(self.inner.music_hoard.get_collection(), previous);
AppState::Browse(AppMachine {
inner: self.inner,
state: AppBrowse,
})
AppMachine::browse(self.inner).into()
}
Err(err) => AppState::Error(AppMachine {
inner: self.inner,
state: AppError {
string: err.to_string(),
},
}),
Err(err) => AppMachine::error(self.inner, err.to_string()).into(),
}
}
}
@ -291,7 +349,7 @@ impl<MH: IMusicHoard> IAppInteractSearch for AppMachine<MH, AppSearch> {
.selection
.incremental_artist_search(collection, &self.state.string, false);
self.state.memo.push(AppSearchMemo { index, char: true });
AppState::Search(self)
self.into()
}
fn search_next(mut self) -> Self::APP {
@ -304,7 +362,7 @@ impl<MH: IMusicHoard> IAppInteractSearch for AppMachine<MH, AppSearch> {
);
self.state.memo.push(AppSearchMemo { index, char: false });
}
AppState::Search(self)
self.into()
}
fn step_back(mut self) -> Self::APP {
@ -315,26 +373,20 @@ impl<MH: IMusicHoard> IAppInteractSearch for AppMachine<MH, AppSearch> {
}
self.inner.selection.select_artist(collection, memo.index);
}
AppState::Search(self)
self.into()
}
fn finish_search(self) -> Self::APP {
AppState::Browse(AppMachine {
inner: self.inner,
state: AppBrowse,
})
AppMachine::browse(self.inner).into()
}
fn cancel_search(mut self) -> Self::APP {
self.inner.selection.select_by_list(self.state.orig);
AppState::Browse(AppMachine {
inner: self.inner,
state: AppBrowse,
})
AppMachine::browse(self.inner).into()
}
fn no_op(self) -> Self::APP {
AppState::Search(self)
self.into()
}
}
@ -342,10 +394,7 @@ impl<MH: IMusicHoard> IAppInteractError for AppMachine<MH, AppError> {
type APP = App<MH>;
fn dismiss_error(self) -> Self::APP {
AppState::Browse(AppMachine {
inner: self.inner,
state: AppBrowse,
})
AppMachine::browse(self.inner).into()
}
}
@ -353,7 +402,7 @@ impl<MH: IMusicHoard> IAppInteractCritical for AppMachine<MH, AppCritical> {
type APP = App<MH>;
fn no_op(self) -> Self::APP {
AppState::Critical(self)
self.into()
}
}
@ -505,12 +554,10 @@ mod tests {
let app = App::new(music_hoard);
assert!(app.is_running());
let app = App::Error(AppMachine {
inner: app.unwrap_browse().inner,
state: AppError {
string: String::from("get rekt"),
},
});
let app = App::Error(AppMachine::error(
app.unwrap_browse().inner,
String::from("get rekt"),
));
let error = app.unwrap_error();
@ -534,12 +581,10 @@ mod tests {
let app = App::new(music_hoard(COLLECTION.to_owned()));
assert!(app.is_running());
let app = App::Error(AppMachine {
inner: app.unwrap_browse().inner,
state: AppError {
string: String::from("get rekt"),
},
});
let app = App::Error(AppMachine::error(
app.unwrap_browse().inner,
String::from("get rekt"),
));
let app = app.force_quit();
assert!(!app.is_running());