rudy_dwarf/
error.rs

1use std::{fmt, sync::Arc};
2
3use crate::die::DieAccessError;
4
5#[derive(Debug, Clone)]
6pub enum Error {
7    Gimli(gimli::Error),
8    Resolution(String),
9    DieAccess(Arc<DieAccessError>),
10    Custom(Arc<anyhow::Error>), // Io(Arc<std::io::Error>),
11                                // ObjectParseError(object::read::Error),
12                                // MemberFileNotFound(String),
13}
14
15unsafe impl salsa::Update for Error {
16    unsafe fn maybe_update(old_pointer: *mut Self, new_value: Self) -> bool {
17        unsafe { *old_pointer = new_value };
18        true
19    }
20}
21
22impl fmt::Display for Error {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        match self {
25            Error::DieAccess(error) => write!(f, "{error}"),
26            Error::Gimli(error) => write!(f, "Gimli error: {error}"),
27            Error::Resolution(error) => write!(f, "Resolution error: {error}"),
28            Error::Custom(error) => write!(f, "{error}"),
29        }
30    }
31}
32
33impl PartialEq for Error {
34    fn eq(&self, _other: &Self) -> bool {
35        // we'll consider _all_ errors equal for now
36        // since we only really care about if it was an error or not
37        true
38    }
39}
40
41impl std::error::Error for Error {
42    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
43        match self {
44            Error::DieAccess(error) => Some(error),
45            Error::Gimli(error) => Some(error),
46            Error::Resolution(_) => None,
47            Error::Custom(error) => error.source(),
48        }
49    }
50}
51
52impl From<gimli::Error> for Error {
53    fn from(err: gimli::Error) -> Self {
54        Error::Gimli(err)
55    }
56}
57
58impl From<String> for Error {
59    fn from(err: String) -> Self {
60        Error::Resolution(err)
61    }
62}
63
64impl From<anyhow::Error> for Error {
65    fn from(err: anyhow::Error) -> Self {
66        Error::Custom(Arc::new(err))
67    }
68}
69
70impl From<DieAccessError> for Error {
71    fn from(err: DieAccessError) -> Self {
72        Error::DieAccess(Arc::new(err))
73    }
74}