From 2ec2fda116c0a7f9e0379b7eea555f905a56aef8 Mon Sep 17 00:00:00 2001 From: Reinout Meliesie Date: Wed, 12 Feb 2025 11:34:38 +0100 Subject: [PATCH] Improve error handling --- src/data_manager.rs | 7 ++-- src/error.rs | 40 +++++++------------ src/main.rs | 8 ++-- .../collation_menu/sort_button.rs | 13 ++++-- 4 files changed, 32 insertions(+), 36 deletions(-) diff --git a/src/data_manager.rs b/src/data_manager.rs index bb7037b..a7fc953 100644 --- a/src/data_manager.rs +++ b/src/data_manager.rs @@ -24,8 +24,8 @@ impl DataManager { Ok ( Self { sqlite_client } ) } - pub async fn get_collection_overview ( & self ) -> CollectionOverview { - self . sqlite_client . conn ( |sqlite_connection| { + pub async fn get_collection_overview ( & self ) -> Result { + let collection_overview = self . sqlite_client . conn ( |sqlite_connection| { let films = sqlite_connection . prepare ( " select uuid , name , original_name , release_date , runtime_minutes , poster_file_path @@ -48,7 +48,8 @@ impl DataManager { . collect () ? ; Ok ( CollectionOverview { films , series } ) - } ) . await . unwrap () + } ) . await ? ; + Ok (collection_overview) } } diff --git a/src/error.rs b/src/error.rs index 6e9867f..7318a47 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,6 +1,5 @@ use std :: * ; use std :: any :: * ; -use std :: future :: * ; @@ -16,28 +15,19 @@ impl From < Box < dyn Any + Send > > for ZoodexError { } } -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) , - } ; -} +macro_rules ! async_result_context { ( + $ future : expr + $ ( , ok => $ on_success : expr ) ? + $ ( , err => $ on_failure : expr ) ? $ (,) ? +) => { + # [ allow (unreachable_patterns) ] match $ future . await { + $ ( Ok (value) => $ on_success (value) , ) ? + Ok (_) => {} , + $ ( Err (error) => $ on_failure (error) , ) ? + Err (_) => {} , + } +} } -pub async fn async_unit_result_context < - AsyncFunction : Future < Output = Result <()> > , - FailureHandler : Fn (ZoodexError) , -> ( - function : AsyncFunction , - on_failure : FailureHandler , -) { - async_result_context ( function , |_| () , on_failure ) . await ; -} + + +pub (crate) use async_result_context ; diff --git a/src/main.rs b/src/main.rs index fabd4ad..e8ec642 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,19 +42,19 @@ fn show_window ( application : & Application ) { ui . show_window () ; spawn_future_local ( async move { - async_unit_result_context ( + async_result_context ! ( async { let data_manager = DataManager :: new () . await ? ; - let collection = data_manager . get_collection_overview () . await ; + let collection = data_manager . get_collection_overview () . await ? ; ui . render_collection_overview (collection) . await ; Ok (()) } , - |error| { + err => |error| { match error { CollectionFileReadError => eprintln ! ("Could not read collection file") , } ; ui . close_window () ; } , - ) . await ; + ) ; } ) ; } diff --git a/src/ui/collatable_container/collation_menu/sort_button.rs b/src/ui/collatable_container/collation_menu/sort_button.rs index 0b83430..24cae61 100644 --- a/src/ui/collatable_container/collation_menu/sort_button.rs +++ b/src/ui/collatable_container/collation_menu/sort_button.rs @@ -58,7 +58,12 @@ fn on_media_sort_activated < A : MediaAdapter + 'static > ( on_sort : & impl Fn ( A :: Sorting ) , sort_icons : & [ Image ] , ) { - let ( sorting_property , _ ) = A :: get_property_descriptions () [ row as usize ] . clone () ; // TODO: Bounds checking + let row = row as usize ; + debug_assert ! ( + row <= A :: get_property_descriptions () . len () , + "Sorting menu has more rows than media adapter has property descriptions" , + ) ; + let ( sorting_property , _ ) = A :: get_property_descriptions () [row] . clone () ; let previous_sorting = * previous_sorting_mut . borrow () ; if sorting_property == previous_sorting . get_property () { @@ -66,20 +71,20 @@ fn on_media_sort_activated < A : MediaAdapter + 'static > ( Ascending => { let new_sorting = A :: Sorting :: new ( sorting_property , Descending ) ; previous_sorting_mut . replace (new_sorting) ; - sort_icons [ row as usize ] . set_icon_name ( Some ("view-sort-descending-symbolic") ) ; + sort_icons [row] . set_icon_name ( Some ("view-sort-descending-symbolic") ) ; on_sort (new_sorting) ; } , Descending => { let new_sorting = A :: Sorting :: new ( sorting_property , Ascending ) ; previous_sorting_mut . replace (new_sorting) ; - sort_icons [ row as usize ] . set_icon_name ( Some ("view-sort-ascending-symbolic") ) ; + sort_icons [row] . set_icon_name ( Some ("view-sort-ascending-symbolic") ) ; on_sort (new_sorting) ; } , } } else { let new_sorting = A :: Sorting :: new ( sorting_property , Ascending ) ; previous_sorting_mut . replace (new_sorting) ; - sort_icons [ row as usize ] . set_icon_name ( Some ("view-sort-ascending-symbolic") ) ; + sort_icons [row] . set_icon_name ( Some ("view-sort-ascending-symbolic") ) ; on_sort (new_sorting) ; } }