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
50
51
52
53
54
55
56
57
use core::fmt;
use std::rc::Rc;
use std::error::Error;
use crate::utils;
use crate::debug::symbol::DebugSymbol;
#[derive(Debug)]
pub enum ErrorKind {
EOFReached,
Other,
}
#[derive(Debug)]
pub struct SymbolResolutionError {
kind: ErrorKind,
symbol: DebugSymbol,
cause: Option<Rc<dyn Error>>,
}
impl SymbolResolutionError {
pub fn new(symbol: DebugSymbol, kind: ErrorKind) -> Self {
Self {
symbol, kind, cause: None,
}
}
pub fn caused_by(symbol: DebugSymbol, error: Rc<dyn Error>) -> Self {
Self {
symbol,
kind: ErrorKind::Other,
cause: Some(error),
}
}
pub fn kind(&self) -> &ErrorKind { &self.kind }
}
impl Error for SymbolResolutionError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.cause.as_ref().map(|o| o.as_ref())
}
}
impl fmt::Display for SymbolResolutionError {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
let inner_message = match self.kind() {
ErrorKind::EOFReached => Some("EOF reached while extracting symbol"),
ErrorKind::Other => None,
};
let message = format!("could not resolve symbol ${}:{}", self.symbol.start, self.symbol.end());
utils::format_error(fmt, message.as_str(), inner_message, self.source())
}
}