11 unstable releases (3 breaking)

new 0.4.3 Feb 17, 2025
0.4.2 Feb 3, 2025
0.4.1 Nov 23, 2024
0.3.1 Nov 14, 2024
0.1.0 Nov 10, 2024

#2646 in Procedural macros

Download history 59/week @ 2024-11-06 577/week @ 2024-11-13 124/week @ 2024-11-20 21/week @ 2024-11-27 9/week @ 2024-12-04 9/week @ 2024-12-11 6/week @ 2025-01-08 94/week @ 2025-01-29 39/week @ 2025-02-05 53/week @ 2025-02-12

186 downloads per month
Used in error_mancer

MIT license

14KB
325 lines

error_mancer

Overview

The error_mancer crate adds a #[errors] attribute that allows you to easily define and restrict error types in functions. This approach makes error handling more concise and keeps the error definitions close to the relevant methods, simplifying maintenance and modification.

Example Usage

For example in the following code the #[errors] macro automatically defines a OpenFileError based on the macro and substitudes in the error type in the signature.

use std::io;

use error_mancer::*;

#[errors(io::Error, serde_json::Error)]
fn open_file() -> Result<SomeStruct, _> {
    let file = std::fs::File::open("hello.json")?;
    let data = serde_json::from_reader(file)?;

    Ok(data)
}

fn main() {
    match open_file() {
        Err(OpenFileError::Io(err)) => { /* Handle I/O error */ },  
        Err(OpenFileError::SerdeJson(err)) => { /* Handle JSON parsing error */ },
        Ok(data) => { /* Use data */ }
    }
}

The main benefit of this approach is that it moves the error enum definition much closer to the method, making it easier to modify.

anyhow support

Additionally, it supports generic error results like anyhow. In these cases, the return type is not modified, but the allowed return values are still restricted. This is particularly useful when implementing traits that require an anyhow::Result.

use error_mancer::*;

#[errors]
impl other_crate::Trait for MyStruct {
    #[errors]
    fn some_method(&self) -> anyhow::Result<()> {
        // This is a compiler error now!
        std::fs::open("hello.txt")?;
    }
}

Design Goals

  • Simplified Error Wrapper Enums: This crate aims to make defining trivial error wrapper enums much easier and more convenient.
  • Enforcing Error Restrictions: It aims to allow you to enforce error restrictions on anyhow::Result and similar Result types.
  • Compatibility with thiserror: This crate does not aim to replace thiserror or similar libraries. They are designed for public-facing errors where control over details is important. In contrast, this library is focused on minimizing boilerplate as much as possible, providing less control but offering sensible defaults for internal error enums.

In other words, what if anyhow was strongly typed on the possible errors?

Dependencies

~0.6–1.1MB
~21K SLoC