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
//! Defines the error type used throughout both the library crate
//! and the main command-line application when encountering exceptions.

use std::error::Error;
use std::fmt::{self, Display};

// type alias for a Result that encapsulates a BinError
pub type BinResult<R> = Result<R, BinError>;

/// Defines the error variants that can be encountered when executing.
#[derive(Debug)]
pub enum ErrorKind {
    ParseError,
    BinaryError,
    RuleEngineError,
    KernelCheckError,
    FileError,
    DumpError,
}

/// Defines the main error type used for any exception that occurs.
#[derive(Debug)]
pub struct BinError {
    pub kind: ErrorKind,
    pub msg: String,
}

impl Display for BinError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        write!(f, "\"{:?}: {}\"", self.kind, self.msg)
    }
}

impl From<std::io::Error> for BinError {
    fn from(error: std::io::Error) -> Self {
        Self {
            kind: ErrorKind::FileError,
            msg: error.to_string(),
        }
    }
}

impl From<goblin::error::Error> for BinError {
    fn from(error: goblin::error::Error) -> Self {
        Self {
            kind: ErrorKind::BinaryError,
            msg: error.to_string(),
        }
    }
}

impl Error for BinError {}