Skip to main content

memhop/
error.rs

1use std::fmt;
2
3/// memhop's generic Result wrapper
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// memhop's generic Error wrapper
7#[derive(Debug)]
8pub enum Error {
9    /// IO error. May include errors such as EACCES on some platforms
10    Io(std::io::Error),
11    /// CString parsing error
12    FromVecWithNul(std::ffi::FromVecWithNulError),
13    /// CString parsing error
14    Utf8(std::str::Utf8Error),
15    /// CString parsing error
16    FromUtf8(std::string::FromUtf8Error),
17    /// Int parsing error
18    ParseInt(std::num::ParseIntError),
19    /// Invalid data/state encountered when parsing OS-specific data
20    InvalidData,
21
22    #[cfg(target_os = "linux")]
23    /// Procmaps error
24    Procmaps(procmaps::Error),
25
26    #[cfg(target_os = "macos")]
27    /// Regex error while parsing memory maps
28    Regex(regex::Error),
29    #[cfg(target_os = "macos")]
30    /// Mach kernel error
31    Mach(i32),
32    #[cfg(target_os = "macos")]
33    /// Libproc error
34    Libproc,
35
36    #[cfg(target_os = "windows")]
37    /// Layout error
38    Layout(std::alloc::LayoutError),
39}
40
41// Generation of an error is completely separate from how it is displayed.
42// There's no need to be concerned about cluttering complex logic with the display style.
43//
44// Note that we don't store any extra info about the errors. This means we can't state
45// which string failed to parse without modifying our types to carry that information.
46impl fmt::Display for Error {
47    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
48        match self {
49            Self::Io(x) => write!(f, "IO error; err = {:?}", x),
50            Self::FromVecWithNul(x) => write!(f, "FromVecWithNul error; err = {:?}", x),
51            Self::Utf8(x) => write!(f, "UTF8 error; err = {:?}", x),
52            Self::FromUtf8(x) => write!(f, "FromUTF8 error; err = {:?}", x),
53            Self::ParseInt(x) => write!(f, "ParseInt error; err = {:?}", x),
54            Self::InvalidData => write!(f, "invalid data encountered"),
55
56            #[cfg(target_os = "linux")]
57            Self::Procmaps(x) => write!(f, "procmaps error; err = {:?}", x),
58
59            #[cfg(target_os = "macos")]
60            Self::Regex(x) => write!(f, "regex error; err = {:?}", x),
61            #[cfg(target_os = "macos")]
62            Self::Mach(x) => write!(f, "mach error code {}", x),
63            #[cfg(target_os = "macos")]
64            Self::Libproc => write!(f, "libproc error"),
65
66            #[cfg(target_os = "windows")]
67            Self::Layout(x) => write!(f, "Layout error; err = {:?}", x),
68        }
69    }
70}