fnerror 0.1.0

A proc-macro for generating error impls for functions.
Documentation

fnerror

A Rust library for error handling when you want your functions to automatically generate errors.

Features

  • Define error types directly in function expressions.
  • Generate error types binding to the function.

Usage

Add the following to your Cargo.toml:

cargo add fnerror

You also need to add thiserror to your crate: (Will be unneeded in the future)

cargo add thiserror

Example

#[fnerror]
fn foo() -> Result<()> {
    bar().map_err(|e| {
        #[fnerr]
        Error2("{}", e as String)
    })?;
    baz().map_err(|e| {
        #[fnerr]
        Error3("{}, {}", e as &'static str, 123 as u8)
    })?;
    Ok(())
}

fn bar() -> Result<(), String> {
    Err("test2 error".to_string())
}

fn baz() -> Result<(), &'static str> {
    Err("test2 error")
}

Which expands to (with thiserror feature):

#[derive(Debug, ::thiserror::Error)]
pub enum FooError {
    #[error("{}", 0usize)]
    Error2(String),
    #[error("{}, {}",0usize 1usize)]
    Error3(&'static str, u8),
}
fn foo() -> ::std::result::Result<(), FooError> {
    bar().map_err(|e| FooError::Error2(e))?;
    baz().map_err(|e| FooError::Error3(e, 123))?;
    Ok(())
}

Status

  • Parse AST to generate error types.
  • Generate error implementations using thiserror crate.
  • Support generic error types.
  • Support other error implementations.
  • Support custom error name.
  • Support more formatting options.