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
62
63
64
//! A custom GdbCommand error
//!

use core::fmt;
use core::result;
use std::{error, io};

#[derive(Debug)]
/// A custom GdbCommand error
pub enum Error {
    /// Gdb output parsing error
    ParseOutput(String),
    /// No executable/core found to run under gdb.
    NoFile(String),
    /// An IO based error
    IO(io::Error),
    /// Error parsing stacktrace
    StacktraceParse(String),
    /// Error parsing mapped files
    MappedFilesParse(String),
    /// An ParseInt based error
    IntParse(std::num::ParseIntError),
}

impl error::Error for Error {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match *self {
            Error::IO(ref io) => Some(io),
            Error::IntParse(ref pr) => Some(pr),
            Error::ParseOutput(_) => None,
            Error::NoFile(_) => None,
            Error::StacktraceParse(_) => None,
            Error::MappedFilesParse(_) => None,
        }
    }
}

impl From<io::Error> for Error {
    fn from(err: io::Error) -> Error {
        Error::IO(err)
    }
}

impl From<std::num::ParseIntError> for Error {
    fn from(err: std::num::ParseIntError) -> Error {
        Error::IntParse(err)
    }
}

impl fmt::Display for Error {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::IO(ref err) => write!(fmt, "{}", err),
            Error::IntParse(ref err) => write!(fmt, "{}", err),
            Error::ParseOutput(ref msg) => write!(fmt, "Gdb parsing output error: {}", msg),
            Error::NoFile(ref msg) => write!(fmt, "File not found: {}", msg),
            Error::StacktraceParse(ref msg) => write!(fmt, "Error parsing stack trace: {}", msg),
            Error::MappedFilesParse(ref msg) => write!(fmt, "Error parsing mapped files: {}", msg),
        }
    }
}

/// GdbCommand Result
pub type Result<T> = result::Result<T, Error>;