6 releases
| 0.1.5 | Jun 9, 2025 |
|---|---|
| 0.1.4 | Jan 10, 2025 |
| 0.1.1 | Oct 3, 2024 |
| 0.1.0 | Aug 13, 2024 |
#1120 in Procedural macros
62 downloads per month
6KB
68 lines
Add a context to the result returned from the annotated function, regardless of return path.
#[context_attr::anyhow("Doing thing")]
fn do_thing() -> anyhow::Result<()> {
if true {
anyhow::bail!("Can't do thing");
}
anyhow::bail!("Another error path");
}
This is most useful when there are multiple error points in a function, and annotating the context of the function at each point adds too much noise.
Format
You can call std::format to include function arguments in the message.
#[context_attr::anyhow(format!("The number: {x}"))]
fn add_one(x: u32) -> anyhow::Result<()> { todo!() }
Note: that the format string is eagerly created at the beginning of the function. There currently isn't a way around this, as arguments to the function may be moved.
Works with
The annotation works with:
- Async functions
- Functions in impl blocks
Dependencies
~235–700KB
~15K SLoC