Introduce error handling
This commit is contained in:
parent
5d9893d304
commit
32c1d2ab67
4 changed files with 54 additions and 7 deletions
31
src/error.rs
Normal file
31
src/error.rs
Normal 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) ,
|
||||
} ;
|
||||
}
|
15
src/main.rs
15
src/main.rs
|
@ -1,4 +1,5 @@
|
|||
mod collection ;
|
||||
mod error ;
|
||||
mod persistence ;
|
||||
mod ui ;
|
||||
mod utility ;
|
||||
|
@ -8,7 +9,7 @@ use {
|
|||
libadwaita :: { * , prelude :: * } ,
|
||||
} ;
|
||||
|
||||
use crate :: { persistence :: * , ui :: * } ;
|
||||
use crate :: { error :: * , persistence :: * , ui :: * } ;
|
||||
|
||||
|
||||
|
||||
|
@ -25,10 +26,14 @@ fn on_activate ( app : & Application ) {
|
|||
ui . show_window () ;
|
||||
|
||||
spawn_future_local ( async move {
|
||||
let collection = spawn_blocking ( ||
|
||||
read_collection_file () . unwrap ()
|
||||
) . await . unwrap () ;
|
||||
|
||||
async_result_context ( async {
|
||||
let collection = spawn_blocking ( || read_collection_file () )
|
||||
. await ? ? ;
|
||||
ui . render_collection (collection) ;
|
||||
|
||||
Ok (())
|
||||
} , |_| () , |error| {
|
||||
ui . close_window () ;
|
||||
} ) . await ;
|
||||
} ) ;
|
||||
}
|
||||
|
|
|
@ -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 () ? ,
|
||||
} )
|
||||
}
|
||||
|
||||
impl From < rusqlite :: Error > for ZoodexError {
|
||||
fn from ( error : rusqlite :: Error ) -> Self {
|
||||
match error {
|
||||
SqliteFailure ( _ , _ ) => CollectionFileError ,
|
||||
_ => panic ! () ,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,6 +48,8 @@ impl UI {
|
|||
|
||||
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 ) {
|
||||
self . films_component . set_films ( collection . films ) ;
|
||||
self . series_component . set_series ( collection . series ) ;
|
||||
|
|
Loading…
Add table
Reference in a new issue