1use capstone_sys::cs_err::*;
4
5use core::fmt;
6use core::result;
7
8macro_rules! capstone_error_def {
10 ( $( $( #[$attr:meta] )* => $rust_variant:ident = $cs_variant:ident; )* ) => {
11 #[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
12 pub enum Error {
14 $(
15 $(
16 #[$attr]
17 )*
18 $rust_variant,
19 )*
20
21 UnknownCapstoneError,
23
24 InvalidM68kBitfieldRegister,
26
27 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 => OutOfMemory = CS_ERR_MEM;
47 => UnsupportedArch = CS_ERR_ARCH;
49 => InvalidHandle = CS_ERR_HANDLE;
51 => InvalidCsh = CS_ERR_CSH;
53 => InvalidMode = CS_ERR_MODE;
55 => InvalidOption = CS_ERR_OPTION;
57 => DetailOff = CS_ERR_DETAIL;
59 => UninitializedMemSetup = CS_ERR_MEMSETUP;
61 => UnsupportedVersion = CS_ERR_VERSION;
63 => IrrelevantDataInDiet = CS_ERR_DIET;
65 => IrrelevantDataInSkipData = CS_ERR_SKIPDATA;
67 => UnsupportedX86Att = CS_ERR_X86_ATT;
69 => UnsupportedX86Intel = CS_ERR_X86_INTEL;
71 => 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}