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