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
use std::error;

#[derive(Debug)]
struct Location {
    file: &'static str,
    line: u32,
}

#[derive(Debug)]
pub struct TIError {
    cause: String,
    location: Option<Location>,
}

impl error::Error for TIError {}

impl TIError {
    #[allow(dead_code)]
    pub(crate) fn new<C: Into<String>>(cause: C) -> Self {
        Self {
            cause: cause.into(),
            location: None,
        }
    }

    #[allow(dead_code)]
    pub(crate) fn new_with_location<C: Into<String>>(
        cause: C,
        file: &'static str,
        line: u32,
    ) -> Self {
        Self {
            cause: cause.into(),
            location: Some(Location { file, line }),
        }
    }
}

impl std::fmt::Display for TIError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self.location {
            Some(ref location) => {
                write!(f, "{} at {}#{}", self.cause, location.file, location.line)
            }
            None => write!(f, "{}", self.cause),
        }
    }
}