oom/
errors.rs

1use std::fmt::{Debug, Display, Formatter};
2
3use crate::{Caller, Traceback};
4
5#[derive(Clone, PartialEq, Eq)]
6pub struct Error {
7    message: String,
8    callers: Vec<Caller>,
9}
10impl Error {
11    pub fn new<T: Display>(message: T) -> Self {
12        let message = message.to_string();
13        Error {
14            message,
15            callers: Vec::new(),
16        }
17    }
18}
19impl std::error::Error for Error {}
20impl Traceback for Error {
21    fn message(&self) -> String {
22        self.message.to_string()
23    }
24
25    fn with(&self, caller: Caller) -> Self {
26        let mut error = self.clone();
27        error.callers.insert(0, caller);
28        error
29    }
30
31    fn callers(&self) -> Vec<Caller> {
32        self.callers.to_vec()
33    }
34}
35impl Display for Error {
36    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
37        write!(
38            f,
39            "\x1b[0m{}\n\n\x1b[1;48;5;198m\x1b[1;38;5;235mreason:\x1b[0m {}",
40            "TODO",
41            self.highlight_message(),
42        )
43    }
44}
45impl Debug for Error {
46    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
47        write!(
48            f,
49            "\x1b[1;38;5;202min source:\n{}\n\n\x1b[1;38;5;220mStacktrace:\n{}\n",
50            self.to_string(),
51            self.callers_to_string(4)
52        )
53    }
54}
55
56pub type Result<T> = std::result::Result<T, Error>;