evobench_tools/utillib/
ctx.rs

1/// A shorter way to create a `anyhow::Result` with error context information
2///
3/// Instead of `.with_context(|| anyhow!("while doing {}", 1 + 1))`,
4/// this allows writing `.map_err(ctx!("while doing {}", 1 + 1))`.
5#[macro_export]
6macro_rules! ctx {
7    ($fmt:tt) => {
8        |e| anyhow::Context::context(Result::<(), _>::Err(e), format!($fmt))
9            .err().unwrap()
10    };
11    ($fmt:tt, $($arg:tt)*) => {
12        |e| anyhow::Context::context(Result::<(), _>::Err(e), format!($fmt, $($arg)*))
13            .err().unwrap()
14    };
15}