1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use backtrace::Backtrace;
use std::{error::Error as StdError, fmt::Debug};

pub type CrateResult = Result<(), Error>;

pub struct Error {
    source: Option<Box<dyn StdError>>,
    bt: Backtrace,
    message: Option<String>,
}

impl<T> From<T> for Error
where
    T: std::error::Error + 'static,
{
    fn from(src: T) -> Self {
        Error {
            message: Some(src.to_string()),
            source: Some(Box::new(src)),
            bt: Backtrace::new(),
        }
    }
}

impl Debug for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut src: Option<&dyn StdError> = self.source.as_deref();
        let mut indentation_levels = 0;

        if let Some(message) = &self.message {
            f.write_str(message)?;
        }

        while let Some(source) = src {
            f.write_str("\n")?;

            for _ in 0..=indentation_levels {
                f.write_str("  ")?;
            }

            f.write_fmt(format_args!("Caused by: {source}\n"))?;

            indentation_levels += 1;
            src = source.source();
        }

        f.write_fmt(format_args!("{:?}\n", self.bt))
    }
}