err_marks_the_spot_core/
lib.rs

1//!
2
3use ansi_term::Color;
4
5#[derive(Debug)]
6pub struct ErrorCtx {
7    location: &'static std::panic::Location<'static>,
8    backtrace: std::backtrace::Backtrace,
9}
10
11impl ErrorCtx {
12    #[track_caller]
13    pub fn new() -> Self {
14        Self {
15            location: std::panic::Location::caller(),
16            backtrace: std::backtrace::Backtrace::capture(),
17        }
18    }
19}
20
21#[rustfmt::skip]
22impl std::fmt::Display for ErrorCtx {
23    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24        let Self { location, backtrace } = self;
25        let error = Color::Red.paint("ERROR");
26        let file = {
27            let file = location.file();
28            Color::Blue.paint(format!("{file}"))
29        };
30        let line = {
31            let line = location.line();
32            Color::Green.paint(format!("{line}"))
33        };
34        let column = {
35            let column = location.column();
36            Color::Yellow.paint(format!("{column}"))
37        };
38        writeln!(f, "{error} detected @ {file}:{line}:{column}:")?;
39        writeln!(f, "{backtrace}")?;
40        Ok(())
41    }
42}