capstone_git/
error.rs

1//! Capstone errors
2
3use capstone_sys::cs_err::*;
4
5use core::fmt;
6use core::result;
7
8/// Create `RustFeatures` struct definition, `new()`, and a getter for each field
9macro_rules! capstone_error_def {
10    ( $( $( #[$attr:meta] )* => $rust_variant:ident = $cs_variant:ident; )* ) => {
11        #[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
12        /// An error enum for this library
13        pub enum Error {
14            $(
15                $(
16                    #[$attr]
17                )*
18                $rust_variant,
19            )*
20
21            /// An unknown error not equal to a `CapstoneError`
22            UnknownCapstoneError,
23
24            /// Invalid M68K bitfield register
25            InvalidM68kBitfieldRegister,
26
27            /// Error with a custom message
28            CustomError(&'static str),
29        }
30
31        impl From<capstone_sys::cs_err::Type> for Error {
32            fn from(err: capstone_sys::cs_err::Type) -> Self {
33                match err {
34                    $(
35                        $cs_variant => Error::$rust_variant,
36                    )*
37                    _ => Error::UnknownCapstoneError,
38                }
39            }
40        }
41    }
42}
43
44capstone_error_def!(
45    /// Out-Of-Memory error: cs_open(), cs_disasm(), cs_disasm_iter()
46    => OutOfMemory = CS_ERR_MEM;
47    /// Unsupported Architecture: cs_open()
48    => UnsupportedArch = CS_ERR_ARCH;
49    /// Invalid Handle: cs_op_count(), cs_op_index()
50    => InvalidHandle = CS_ERR_HANDLE;
51    /// Invalid InvalidCsh argument: cs_close(), cs_errno(), cs_option()
52    => InvalidCsh = CS_ERR_CSH;
53    /// Invalid/unsupported mode: cs_open()
54    => InvalidMode = CS_ERR_MODE;
55    /// Invalid/unsupported option: cs_option()
56    => InvalidOption = CS_ERR_OPTION;
57    /// Information is unavailable because detail option is OFF
58    => DetailOff = CS_ERR_DETAIL;
59    /// Dynamic Memory management uninitialized (see CS_OPT_MEM)
60    => UninitializedMemSetup = CS_ERR_MEMSETUP;
61    /// Unsupported Version (bindings)
62    => UnsupportedVersion = CS_ERR_VERSION;
63    /// Access irrelevant data in "diet" engine
64    => IrrelevantDataInDiet = CS_ERR_DIET;
65    /// Access irrelevant data for "data" instruction in SKIPDATA Mode
66    => IrrelevantDataInSkipData = CS_ERR_SKIPDATA;
67    /// X86 AT&T syntax is unsupported (opt-out at compile time)
68    => UnsupportedX86Att = CS_ERR_X86_ATT;
69    /// X86 Intel syntax is unsupported (opt-out at compile time)
70    => UnsupportedX86Intel = CS_ERR_X86_INTEL;
71    /// X86 MASM syntax is unsupported (opt-out at compile time)
72    => UnsupportedX86Masm = CS_ERR_X86_MASM;
73);
74
75impl core::error::Error for Error {}
76
77pub type CsResult<T> = result::Result<T, Error>;
78
79impl fmt::Display for Error {
80    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
81        write!(fmt, "{}", self.description())
82    }
83}
84
85impl Error {
86    fn description(&self) -> &str {
87        use self::Error::*;
88        match *self {
89            OutOfMemory => "Out-Of-Memory error",
90            UnsupportedArch => "Unsupported architecture",
91            InvalidHandle => "Invalid handle",
92            InvalidCsh => "Invalid csh argument",
93            InvalidMode => "Invalid/unsupported mode",
94            InvalidOption => "Invalid/unsupported option",
95            DetailOff => "Information is unavailable because detail option is OFF",
96            UninitializedMemSetup => "Dynamic memory management uninitialized (see CS_OPT_MEM)",
97            UnsupportedVersion => "Unsupported version (bindings)",
98            IrrelevantDataInDiet => "Access irrelevant data in \"diet\" engine",
99            IrrelevantDataInSkipData => {
100                "Access irrelevant data for \"data\" instruction in SKIPDATA mode"
101            }
102            UnsupportedX86Att => "X86 AT&T syntax is unsupported (opt-out at compile time)",
103            UnsupportedX86Intel => "X86 Intel syntax is unsupported (opt-out at compile time)",
104            UnsupportedX86Masm => "X86 MASM syntax is unsupported (opt-out at compile time)",
105            UnknownCapstoneError => "Encountered Unknown Capstone Return Error",
106            InvalidM68kBitfieldRegister => {
107                "Invalid M68K Register, must be in d0-d7, a0-a7, fp0-fp7"
108            }
109            CustomError(msg) => msg,
110        }
111    }
112}
113
114#[cfg(test)]
115mod test {
116    use super::Error;
117    use capstone_sys::cs_err;
118
119    #[test]
120    fn test_error() {
121        let errors = [
122            Error::OutOfMemory,
123            Error::UnknownCapstoneError,
124            Error::CustomError("custom error"),
125            Error::from(cs_err::CS_ERR_ARCH),
126            Error::from(500 as cs_err::Type),
127        ];
128
129        for error in errors.iter() {
130            println!("{error}");
131        }
132    }
133}