Crates.io | justerror |
lib.rs | justerror |
version | 1.1.0 |
source | src |
created_at | 2022-11-15 11:43:59.084221 |
updated_at | 2023-02-13 10:48:01.256455 |
description | Extension to `thiserror` that helps reduce the amount of handwriting |
homepage | |
repository | https://github.com/alexfedoseev/justerror |
max_upload_size | |
id | 715630 |
size | 25,499 |
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
).
Add to Cargo.toml
:
justerror = "0.1"
Add to main.rs
:
#[macro_use]
extern crate justerror;
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
: stringfmt
: 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.
MIT.