#enums #framework #simple #error

macro mabe

A simple framework for creating debug-friendly error enums in Rust

16 releases (8 stable)

Uses new Rust 2024

1.5.0 Jan 31, 2026
1.4.0 Jan 31, 2026
1.3.1 Dec 17, 2024
0.4.0 Nov 24, 2024
0.1.2 Oct 26, 2024

#775 in Debugging

Apache-2.0

37KB
462 lines

Mabe

Contributions, corrections, and requests can be made through GitHub, and the documentation is available on the platforms linked bellow.

Thank you for your interest in the project, enjoy your reading! 🚀

GitHub: created in GitHub: last commit GitHub: milestones GitHub: CI/CD
Crates.io: size Crates.io: dependents

WARNING

This crate is not published on crates.io anymore. If you want the latest version of this crate, go to GitHub.

Introduction

Mabe is a simple framework for creating debug-friendly error enums in Rust. Each variant in the enum can encapsulate an error and a debug message, and errors are presented in a structured format, displaying the messages defined for the variant. This allows for a more detailed and clear debugging process.

Getting Started

In order to use Mabe in your project, you need to add it as a dependency in your Cargo.toml file:

[dependencies]
mabe = "1"

You can now use the Mabe derive macro and its attributes to create your own error enums by following the examples below.

Examples

Here is a simple example of how to create a debug-friendly error enum:

use mabe::Mabe;

#[derive(Mabe)]
pub enum ServerError {
    #[error("You are not authorized to access this resource.")]
    #[debug("Try using a different account.")]
    Unauthorized,
}

let error = ServerError::Unauthorized;
println!("{}", error);
Output:
[error] You are not authorized to access this resource.
[debug] Try using a different account.

You can also interpolate the values of variant fields in the error and debug messages as shown below:

use mabe::Mabe;

#[derive(Mabe)]
pub enum ServerError {
    // Interpolates the values of the 1st and 2nd field in the error message.
    #[error("Network failure. --> Code {0}: {1}.")]
    NetworkFailure(u32, String),

    // Interpolates the value of the `cause` field in the error message.
    #[error("Connection lost. --> {cause}.")]
    // Interpolates the value of the `retry_in` field in the debug message.
    #[debug("Retry in {retry_in} seconds.")]
    ConnectionLost { cause: String, retry_in: u32 }
}

let error1 = ServerError::NetworkFailure(404, "Not Found".to_string());
println!("{}", error1);

let error2 = ServerError::ConnectionLost { cause: "Server down".to_string(), retry_in: 10 };
println!("{}", error2);
Output:
[error] Network failure. --> Code 404: Not Found.

[error] Connection lost. --> Server down.
[debug] Retry in 10 seconds.

Contributing

This project is open to contributions and suggestions, and any help or feedback is highly appreciated. There is no code of conduct, but please be respectful and considerate when engaging with the community.

This project adheres to the Koseka Standard, which provides standardized versioning and contribution rules. So, make sure to read it first before contributing to the project in any way. Additionally, please refer to the DEVELOPMENT file for setup instructions and guidance on developing and testing the project.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, shall be licensed as bellow, without any additional terms or conditions.

License

Copyright 2026 Amon Rayfa.

This project is licensed under the Apache License (Version 2.0).

Dependencies

~1.5MB
~39K SLoC