thistrace 0.1.0

Callsite provenance (file/line/col) for thiserror #[from] conversions via #[track_caller]
Documentation
use std::io;

use thistrace::{traceable, DisplayTrace, HasTrace, OneLineTrace};

#[traceable]
#[derive(thiserror::Error, Debug)]
enum AppError {
    #[error("io")]
    Io(#[from] io::Error),
}

fn inner() -> Result<(), io::Error> {
    Err(io::Error::new(io::ErrorKind::Other, "boom"))
}

fn outer() -> Result<(), AppError> {
    inner()?;
    Ok(())
}

#[test]
fn captures_callsite_for_from_conversion() {
    let err = outer().unwrap_err();
    let t = err.trace().expect("trace should exist for #[from] variant");
    assert_eq!(t.frames().len(), 1);

    let frame = t.frames()[0];
    assert!(frame.file.ends_with("traceable_from.rs"));
    assert!(frame.line > 0);

    // Ensure it formats and includes the "at ..." line.
    let printed = format!("{}", DisplayTrace::new(&err));
    assert!(printed.contains("io"));
    assert!(printed.contains(" at "));
    assert!(printed.contains("traceable_from.rs"));

    let one_line = format!("{}", OneLineTrace::new(&err));
    assert!(!one_line.contains('\n'));
}