motec_i2/
error.rs

1use std::error::Error;
2use std::fmt;
3use std::io;
4use std::str::Utf8Error;
5
6pub type I2Result<T> = Result<T, I2Error>;
7
8#[derive(Debug)]
9pub enum I2Error {
10    IOError(io::Error),
11
12    // Parsing Errors
13    InvalidHeaderMarker { found: u32, expected: u32 },
14    UnrecognizedDatatype { _type: u16, size: u16 },
15    NonUtf8String(Utf8Error),
16}
17
18impl fmt::Display for I2Error {
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        match self {
21            I2Error::IOError(e) => write!(f, "Underlying IO Error: {}", e),
22            I2Error::InvalidHeaderMarker { found, expected } => write!(
23                f,
24                "Invalid Header Marker found {}, expected {}",
25                found, expected
26            ),
27            I2Error::UnrecognizedDatatype { _type, size } => write!(
28                f,
29                "Unrecognized Datatype found (_type: {}, size: {})",
30                _type, size
31            ),
32            I2Error::NonUtf8String(e) => write!(f, "Attempted to decode non utf8 string: {}", e),
33        }
34    }
35}
36
37impl Error for I2Error {}
38
39impl From<io::Error> for I2Error {
40    fn from(e: io::Error) -> Self {
41        I2Error::IOError(e)
42    }
43}
44
45impl From<Utf8Error> for I2Error {
46    fn from(e: Utf8Error) -> Self {
47        I2Error::NonUtf8String(e)
48    }
49}