#error #error-handling #reduce #amount #extension #helps #handwriting

macro justerror

Extension to thiserror that helps reduce the amount of handwriting

3 releases (stable)

1.1.0 Feb 13, 2023
1.0.0 Dec 7, 2022
0.1.0 Nov 15, 2022

#844 in Procedural macros

Download history 109/week @ 2024-07-21 164/week @ 2024-07-28 168/week @ 2024-08-04 88/week @ 2024-08-11 108/week @ 2024-08-18 174/week @ 2024-08-25 174/week @ 2024-09-01 165/week @ 2024-09-08 107/week @ 2024-09-15 205/week @ 2024-09-22 138/week @ 2024-09-29 166/week @ 2024-10-06 255/week @ 2024-10-13 261/week @ 2024-10-20 278/week @ 2024-10-27 323/week @ 2024-11-03

1,126 downloads per month

MIT license

18KB
349 lines

justerror

github crates.io docs.rs

This macro piggybacks on thiserror crate and is supposed to reduce the amount of handwriting when you want errors in your app to be described via explicit types (rather than anyhow).

Installation

Add to Cargo.toml:

justerror = "0.1"

Add to main.rs:

#[macro_use]
extern crate justerror;

Usage

This macro takes a subject struct or enum and applies thiserror attributes with predefined #[error] messages.

Generally, you can attach #[Error] macro to an error type and be done with it.

#[Error]
enum EnumError {
    Foo,
    Bar {
        a: &'static str,
        b: usize
    },
}

eprintln!("{}", EnumError::Bar { a: "Hey!", b: 42 });

// EnumError::Bar
// === ↴
// a: Hey!
// b: 42

Macro accepts two optional arguments:

  • desc: string
  • fmt: display | debug | "<custom format>"

Both can be applied at the root level.

#[Error(desc = "My emum error description", fmt = debug)]
enum EnumError {
    Foo(usize),
}

And at the variant level.

#[Error(desc = "My emum error description", fmt = debug)]
enum EnumError {
    #[error(desc = "Foo error description", fmt = display)]
    Foo(usize),
}

fmt can also be applied to individual fields.

#[Error(desc = "My emum error description", fmt = debug)]
enum EnumError {
    #[error(desc = "Foo error description", fmt = display)]
    Foo(#[fmt(">5")] usize),
}

See tests for more examples.

License

MIT.

Dependencies

~1.5MB
~37K SLoC