use handle_this::{handle, Handled};
use std::io;
type Result<T> = std::result::Result<T, Handled>;
fn main() {
let result: Result<i32> = handle! {
try { Err(io::Error::other("test"))? }
throw e { format!("transformed: {}", e) }
catch _e { 42 }
};
assert_eq!(result.unwrap(), 42, "throw + untyped catch should be Ok(42)");
println!("Test 1 passed: throw + untyped catch = Ok(42)");
let result: Result<i32> = handle! {
try { Err(io::Error::other("test"))? }
throw e { format!("transformed: {}", e) }
};
assert!(result.is_err(), "throw only should be Err");
println!("Test 2 passed: throw only = Err");
let result: Result<i32> = handle! {
try { Err(io::Error::other("test"))? }
throw e { format!("transformed: {}", e) }
catch io::Error(_e) { 42 }
};
assert!(result.is_err(), "throw + typed catch (wrong type) should be Err");
println!("Test 4 passed: throw + typed catch (wrong type) = Err");
println!("\nAll tests passed!");
}