sphinx/debug/symbol/
errors.rs

1use core::fmt;
2use std::rc::Rc;
3use std::error::Error;
4
5use crate::utils;
6use crate::debug::symbol::DebugSymbol;
7
8
9#[derive(Debug)]
10pub enum ErrorKind {
11    EOFReached, // hit EOF before reaching the indicated end of symbol
12    Other,
13}
14
15#[derive(Debug)]
16pub struct SymbolResolutionError {
17    kind: ErrorKind,
18    symbol: DebugSymbol,
19    cause: Option<Rc<dyn Error>>,
20}
21
22impl SymbolResolutionError {
23    pub fn new(symbol: DebugSymbol, kind: ErrorKind) -> Self {
24        Self {
25            symbol, kind, cause: None,
26        }
27    }
28    
29    pub fn caused_by(symbol: DebugSymbol, error: Rc<dyn Error>) -> Self {
30        Self {
31            symbol,
32            kind: ErrorKind::Other,
33            cause: Some(error),
34        }
35    }
36    
37    pub fn kind(&self) -> &ErrorKind { &self.kind }
38}
39
40impl Error for SymbolResolutionError {
41    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
42        self.cause.as_ref().map(|o| o.as_ref())
43    }
44}
45
46impl fmt::Display for SymbolResolutionError {
47    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
48        
49        let inner_message = match self.kind() {
50            ErrorKind::EOFReached => Some("EOF reached while extracting symbol"),
51            ErrorKind::Other => None,
52        };
53        
54        let message = format!("could not resolve symbol ${}:{}", self.symbol.start, self.symbol.end());
55        utils::format_error(fmt, message.as_str(), inner_message, self.source())
56    }
57}