Crates.io | auto-context |
lib.rs | auto-context |
version | |
source | src |
created_at | 2025-02-02 16:45:36.647449 |
updated_at | 2025-02-02 17:02:43.152535 |
description | Auto-add context to anyhow errors (without nightly) |
homepage | |
repository | https://github.com/enricozb/auto-context |
max_upload_size | |
id | 1539701 |
Cargo.toml error: | TOML parse error at line 17, column 1 | 17 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
Auto-add context to anyhow errors (without nightly).
use anyhow::{Context, Result};
struct Test {}
#[auto_context::auto_context]
impl Test {
fn some_method(self) -> Result<()> {
anyhow::bail!("some error")
}
fn some_function(_: i32) -> Result<()> {
Test {}.some_method()?;
Ok(())
}
}
#[auto_context::auto_context]
fn main() -> Result<()> {
Test::some_function(123)?;
Ok(())
}
fails with
Error: Test::some_function(..) @ auto-context/src/lib.rs::23
Caused by:
0: .some_method() @ auto-context/src/lib.rs::15
1: some error
The auto_context proc macro can be used to annotate any item. This includes functions, methods, and struct/trait impls.
Context is added to every [try expression] (every use of a ?
). Different
kinds of expressions result in different context formats:
.method_name(args)
some::func(args)
xyz
(.. some expr ..)
where args
is ..
if arguments are present and is empty otherwise.