From 32c1d2ab67ccd6b88aa9bc782b76f1d7b62501b8 Mon Sep 17 00:00:00 2001 From: Reinout Meliesie Date: Fri, 29 Nov 2024 21:06:14 +0100 Subject: [PATCH] Introduce error handling --- src/error.rs | 31 +++++++++++++++++++++++++++++++ src/main.rs | 15 ++++++++++----- src/persistence.rs | 13 +++++++++++-- src/ui/mod.rs | 2 ++ 4 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 src/error.rs diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..4863924 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,31 @@ +use std :: { * , any :: * , future :: * } ; + + + +# [ derive (Debug) ] pub enum ZoodexError { + CollectionFileError , +} + +pub type Result = 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 > , + 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) , + } ; +} diff --git a/src/main.rs b/src/main.rs index a836b39..544fb94 100644 --- a/src/main.rs +++ b/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) ; - ui . render_collection (collection) ; + Ok (()) + } , |_| () , |error| { + ui . close_window () ; + } ) . await ; } ) ; } diff --git a/src/persistence.rs b/src/persistence.rs index 4a989b3..759c980 100644 --- a/src/persistence.rs +++ b/src/persistence.rs @@ -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 { series : series . collect () ? , } ) } + +impl From < rusqlite :: Error > for ZoodexError { + fn from ( error : rusqlite :: Error ) -> Self { + match error { + SqliteFailure ( _ , _ ) => CollectionFileError , + _ => panic ! () , + } + } +} diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 5065996..4bef6d1 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -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 ) ;