handle-this 0.2.4

Ergonomic error handling with try/catch/throw/inspect/finally syntax and automatic stack traces
Documentation
//! Common test utilities for matrix tests - AUTO-GENERATED
//! Generated by scripts/generate_full_matrix.py

use std::io::{self, ErrorKind};
use handle_this::{Handled, Result};

/// Returns Ok(42)
pub fn io_ok() -> Result<i32> {
    Ok(42)
}

/// Returns Err with io::Error NotFound
pub fn io_not_found() -> Result<i32> {
    Err(Handled::from(io::Error::new(ErrorKind::NotFound, "not found")))
}

/// Returns error chain: StringError at root, io::Error NotFound in chain
/// Used for testing `catch any io::Error` which searches the chain
pub fn chained_io_error() -> Handled {
    let inner = Handled::from(io::Error::new(ErrorKind::NotFound, "inner not found"));
    let outer = Handled::msg("outer error");
    outer.chain_after(inner)
}

/// Returns error chain: io::Error PermissionDenied at root, NotFound in chain
/// Used for testing `catch all io::Error` which collects all io::Errors
pub fn multi_io_chain() -> Handled {
    let inner = Handled::from(io::Error::new(ErrorKind::NotFound, "inner not found"));
    let outer = Handled::from(io::Error::new(ErrorKind::PermissionDenied, "permission denied"));
    outer.chain_after(inner)
}

/// Iterator that yields all Err values (for try for tests where handlers run)
pub fn iter_all_fail() -> impl Iterator<Item = Result<i32>> {
    vec![
        Err(Handled::from(io::Error::new(ErrorKind::Other, "fail 1"))),
        Err(Handled::from(io::Error::new(ErrorKind::Other, "fail 2"))),
        Err(Handled::from(io::Error::new(ErrorKind::Other, "fail 3"))),
    ].into_iter()
}

/// Iterator that yields all Ok values (for try all tests)
pub fn iter_all_ok() -> impl Iterator<Item = Result<i32>> {
    vec![Ok(1), Ok(2), Ok(3)].into_iter()
}

/// Iterator where second item succeeds (for try for early exit)
pub fn iter_second_ok() -> impl Iterator<Item = Result<i32>> {
    vec![
        Err(Handled::from(io::Error::new(ErrorKind::Other, "fail"))),
        Ok(42),
    ].into_iter()
}

/// Async version of io_ok
pub async fn async_io_ok() -> Result<i32> {
    Ok(42)
}

/// Async version of io_not_found
pub async fn async_io_not_found() -> Result<i32> {
    Err(Handled::from(io::Error::new(ErrorKind::NotFound, "not found")))
}

/// Helper for then chains - wraps value in Ok
pub fn ok_val<T>(v: T) -> Result<T> {
    Ok(v)
}