error_mancer

Crates.ioerror_mancer
lib.rserror_mancer
version
sourcesrc
created_at2024-11-10 18:39:44.308924
updated_at2024-11-23 20:11:24.081211
descriptionQuickly define custom error enums for a function.
homepage
repositoryhttps://github.com/vivax3794/error_mancer
max_upload_size
id1443135
Cargo.toml error:TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Viv (vivax3794)

documentation

README

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

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. 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.

Trait Implementation Example

use error_mancer::*;

#[errors]
impl other_crate::Trait for MyStruct {
    #[errors]
    fn some_method(&self) -> anyhow::Result<()> {
        // This would cause 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. Instead, it encourages using them in tandem to define errors for use with error_mancer.
Commit count: 37

cargo fmt