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
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use std::convert::From;
use std::error;
use std::fmt;
use std::io;

#[cfg(feature = "serialport")]
use serialport;

/// Result type used in the crate.
pub type Result<T> = std::result::Result<T, Error>;

/// Types of errors.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ErrorKind {
    /// IO error
    Io(io::ErrorKind),

    /// Invalid provided input.
    InvalidInput,

    /// The response from the Sabertooth is invalid.
    Response,

    Unknwown,

    /// Serial error. Its embedded kind is defined by the `serialport` crate.
    #[cfg(feature = "serialport")]
    Serial(serialport::ErrorKind),
}

#[derive(Debug)]
enum SubError {
    None,
    Io(io::Error),

    #[cfg(feature = "serialport")]
    Serial(serialport::Error),
}

/// Error type used in the crate.
#[derive(Debug)]
pub struct Error {
    /// The kind of error this is
    pub kind: ErrorKind,

    /// A description of the error suitable for end-users
    pub description: String,

    source: SubError,
}

impl Error {
    /// Instantiates a new error
    pub fn new<T: Into<String>>(kind: ErrorKind, description: T) -> Self {
        Error {
            kind,
            description: description.into(),
            source: SubError::None,
        }
    }
}

impl error::Error for Error {
    fn description(&self) -> &str {
        &self.description
    }

    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match &self.source {
            #[cfg(feature = "serialport")]
            SubError::Serial(err) => Some(err),
            _ => None,
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> std::result::Result<(), fmt::Error> {
        write!(fmt, "{}", &self.description)
    }
}

#[cfg(feature = "serialport")]
impl From<serialport::Error> for Error {
    fn from(err: serialport::Error) -> Error {
        Error {
            kind: ErrorKind::Serial(err.kind),
            description: err.description.clone(),
            source: SubError::Serial(err),
        }
    }
}

#[cfg(feature = "serialport")]
impl From<Error> for serialport::Error {
    fn from(err: Error) -> serialport::Error {
        let kind = match err.kind {
            ErrorKind::Serial(serial_kind) => serial_kind,
            _ => serialport::ErrorKind::Unknown,
        };
        serialport::Error::new(kind, err.description)
    }
}

impl From<io::Error> for Error {
    fn from(err: io::Error) -> Error {
        Error {
            kind: ErrorKind::Io(err.kind()),
            description: err.to_string(),
            source: SubError::Io(err),
        }
    }
}