Skip to content

ZXY595/fnerror

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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<(), MyError> /* or Result<T> to use the default ident of error type ,
which is pascal case of the function name + "Error", like `FooError` */
{
    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):

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

Status

  • Parse AST to generate error types.
  • Generate error implementations using thiserror crate.
  • Support generic error types (experimental).
  • Support custom error name.
  • Replace panics with errors.
  • Support other error implementations.
  • Support more formatting options.

About

A proc-macro for generating error for functions.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages