pseudo-backtrace 0.2.1

Utilities for constructing stack-like error chains with caller locations
Documentation
use pseudo_backtrace::StackError;

#[track_caller]
fn location() -> &'static core::panic::Location<'static> {
    core::panic::Location::caller()
}

#[derive(Debug, StackError)]
struct Inner {
    #[stack_error(std)]
    source: std::io::Error,
    #[location]
    location: &'static core::panic::Location<'static>,
}
impl core::fmt::Display for Inner {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "inner")
    }
}
impl core::error::Error for Inner {}

#[derive(Debug, StackError)]
struct Outer {
    // named field `source` is inferred as stacked
    source: Inner,
    #[location]
    location: &'static core::panic::Location<'static>,
}
impl core::fmt::Display for Outer {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "outer")
    }
}
impl core::error::Error for Outer {}

fn main() {
    let _ = Outer {
        source: Inner {
            source: std::io::Error::other("oh"),
            location: location(),
        },
        location: location(),
    };
}