2024-11-29 21:06:14 +01:00
|
|
|
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) ,
|
|
|
|
} ;
|
|
|
|
}
|
2024-11-30 16:00:44 +01:00
|
|
|
|
|
|
|
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 ;
|
|
|
|
}
|