#error #enums #framework #error-message #simple

macro mabe

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

14 releases (6 stable)

new 1.3.1 Dec 17, 2024
1.3.0 Dec 16, 2024
0.4.0 Nov 24, 2024
0.3.1 Nov 18, 2024
0.1.2 Oct 26, 2024

#596 in Debugging

Download history 311/week @ 2024-10-24 34/week @ 2024-10-31 8/week @ 2024-11-07 428/week @ 2024-11-14 181/week @ 2024-11-21 121/week @ 2024-11-28 265/week @ 2024-12-05 358/week @ 2024-12-12

949 downloads per month

Apache-2.0

37KB
440 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

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.

The project follows the Koseka Project Guidelines, which provide standardized rules and recommendations for project development. Make sure to read these guidelines first before contributing to the project in any way. Additionally, please refer to the DEVELOPMENT file for setup instructions and guidance on developing, testing, and building 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 2024 Amon Rayfa.

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

Dependencies

~1.5MB
~37K SLoC