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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use std::result;
use std::io;
use std::fmt;


#[derive(Debug)]
pub enum Error {
	Io(io::Error),
	Eval(eval::Error),
	InvalidConnection,
    InvalidResponse,
    Timeout,
    TooMuchData,
    IncompleteWrite,
    Read,
    // ISO-TP Frame
    InvalidFrame,

    NegativeResponse(u8),
    InvalidPacket,

    Yaml(serde_yaml::Error),
    InvalidPlatformId,
    InvalidModelId,
    InvalidRomId,
    NotLoaded,
    InvalidTableId,
    NoTableOffset,

    /// Received an empty packet
    EmptyPacket,
    
    #[cfg(feature = "j2534")]
    J2534(j2534::Error),
}

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

impl From<io::Error> for Error {
    fn from(error: io::Error) -> Self {
        Error::Io(error)
    }
}

impl From<serde_yaml::Error> for Error {
    fn from(err: serde_yaml::Error) -> Error {
        Error::Yaml(err)
    }
}

impl From<eval::Error> for Error {
    fn from(err: eval::Error) -> Error {
        Error::Eval(err)
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::Io(ref err) => write!(f, "IO error: {}", err),
            Error::Eval(ref err) => write!(f, "Eval error: {}", err),
            Error::InvalidConnection => write!(f, "Invalid connection"),
            Error::InvalidResponse => write!(f, "Invalid response"),
            Error::Timeout => write!(f, "Timed out"),
            Error::TooMuchData => write!(f, "Too much data"),
            Error::IncompleteWrite => write!(f, "Failed to finish writing data"),
            Error::Read => write!(f, "Read failed"),
            Error::InvalidFrame => write!(f, "Invalid frame"),
            Error::NegativeResponse(code) => write!(f, "Negative ISO-TP response received ({})", code),
            Error::InvalidPacket => write!(f, "Invalid packet received"),
            Error::Yaml(ref err) => write!(f, "Yaml error: {}", err),
            Error::InvalidPlatformId => write!(f, "Invalid platform id"),
            Error::InvalidModelId => write!(f, "Invalid model id"),
            Error::InvalidRomId => write!(f, "Invalid rom id"),
            Error::NotLoaded => write!(f, "Not loaded"),
            Error::InvalidTableId => write!(f, "Invalid table id"),
            Error::NoTableOffset => write!(f, "No table offset"),
            Error::EmptyPacket => write!(f, "Received an empty packet"),
            #[cfg(feature = "j2534")]
            Error::J2534(ref err) => write!(f, "J2534 error: {}", err),
            _ => write!(f, "unimplemented: {:?}", *self),
        }
    }
}