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
53
54
55
56
57
58
59
60
61
// Guillaume Valadon <guillaume@valadon.net>
// binutils - lib.rs

pub mod bfd;
pub mod helpers;
pub mod instruction;
pub mod mach;
pub mod opcodes;
pub mod section;
pub mod utils;

extern crate libc;

use std::fmt;

// Specific errors
#[derive(Debug)]
pub enum Error {
    BfdError(u32, String),
    DisassembleInfoError(String),
    SectionError(String),
    CommonError(String),
    NulError(String),
    Utf8Error(std::str::Utf8Error),
    NullPointerError(String),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::BfdError(tag, ref msg) => write!(f, "{} ({})", msg, tag),
            Error::DisassembleInfoError(ref msg) => write!(f, "{}", msg),
            Error::SectionError(ref section) => write!(f, "Can't find '{}' section!", section),
            Error::CommonError(ref msg) => write!(f, "{}", msg),
            Error::NulError(ref error) => write!(f, "{}", error),
            Error::Utf8Error(ref error) => write!(f, "{}", error),
            Error::NullPointerError(ref error) => write!(f, "{}", error),
        }
    }
}

// Needed to use the ? operator on Cstring::new()
impl From<std::ffi::NulError> for Error {
    fn from(error: std::ffi::NulError) -> Self {
        Error::NulError(format!("{}", error))
    }
}

// Needed to use the ? operator on Cstring::from_bytes_with_nul()
impl From<std::ffi::FromBytesWithNulError> for Error {
    fn from(error: std::ffi::FromBytesWithNulError) -> Self {
        Error::NulError(format!("{}", error))
    }
}

// Needed to use the ? operator on Cstr.to_str()
impl From<std::str::Utf8Error> for Error {
    fn from(error: std::str::Utf8Error) -> Self {
        Error::Utf8Error(error)
    }
}