Introduce error handling

This commit is contained in:
Reinout Meliesie 2024-11-29 21:06:14 +01:00
parent 5d9893d304
commit 32c1d2ab67
Signed by: zedfrigg
GPG key ID: 3AFCC06481308BC6
4 changed files with 54 additions and 7 deletions

31
src/error.rs Normal file
View file

@ -0,0 +1,31 @@
use std :: { * , any :: * , future :: * } ;
# [ derive (Debug) ] pub enum ZoodexError {
CollectionFileError ,
}
pub type Result <Success> = result :: Result < Success , ZoodexError > ;
impl From < Box < dyn Any + Send > > for ZoodexError {
fn from ( error : Box < dyn Any + Send > ) -> Self {
* error . downcast () . unwrap ()
}
}
pub async fn async_result_context <
AsyncFunction : Future < Output = Result <Success> > ,
SuccessHandler : Fn (Success) ,
FailureHandler : Fn (ZoodexError) ,
Success ,
> (
function : AsyncFunction ,
on_success : SuccessHandler ,
on_failure : FailureHandler ,
) {
match function . await {
Ok (value) => on_success (value) ,
Err (error) => on_failure (error) ,
} ;
}

View file

@ -1,4 +1,5 @@
mod collection ; mod collection ;
mod error ;
mod persistence ; mod persistence ;
mod ui ; mod ui ;
mod utility ; mod utility ;
@ -8,7 +9,7 @@ use {
libadwaita :: { * , prelude :: * } , libadwaita :: { * , prelude :: * } ,
} ; } ;
use crate :: { persistence :: * , ui :: * } ; use crate :: { error :: * , persistence :: * , ui :: * } ;
@ -25,10 +26,14 @@ fn on_activate ( app : & Application ) {
ui . show_window () ; ui . show_window () ;
spawn_future_local ( async move { spawn_future_local ( async move {
let collection = spawn_blocking ( || async_result_context ( async {
read_collection_file () . unwrap () let collection = spawn_blocking ( || read_collection_file () )
) . await . unwrap () ; . await ? ? ;
ui . render_collection (collection) ;
ui . render_collection (collection) ; Ok (())
} , |_| () , |error| {
ui . close_window () ;
} ) . await ;
} ) ; } ) ;
} }

View file

@ -1,6 +1,6 @@
use { fallible_iterator :: * , rusqlite :: * } ; use { fallible_iterator :: * , rusqlite :: { Connection , Error :: * } } ;
use crate :: collection :: * ; use crate :: { collection :: * , error :: { * , ZoodexError :: * } } ;
@ -49,3 +49,12 @@ pub fn read_collection_file () -> Result <Collection> {
series : series . collect () ? , series : series . collect () ? ,
} ) } )
} }
impl From < rusqlite :: Error > for ZoodexError {
fn from ( error : rusqlite :: Error ) -> Self {
match error {
SqliteFailure ( _ , _ ) => CollectionFileError ,
_ => panic ! () ,
}
}
}

View file

@ -48,6 +48,8 @@ impl UI {
pub fn show_window ( & self ) { self . window . set_visible (true) } pub fn show_window ( & self ) { self . window . set_visible (true) }
pub fn close_window ( & self ) { self . window . close () }
pub fn render_collection ( & self , collection : Collection ) { pub fn render_collection ( & self , collection : Collection ) {
self . films_component . set_films ( collection . films ) ; self . films_component . set_films ( collection . films ) ;
self . series_component . set_series ( collection . series ) ; self . series_component . set_series ( collection . series ) ;