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
use std::error;
use std::fmt;

/// Possible errors when reading/writing
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum SpError {
    /// The data we attempted to decode did not contain a valid enum variant
    UnknownEnumVariant,
    /// There is not enough space to write T into the writer or to read T from the reader
    NotEnoughSpace,
    /// An annotated count field's type is too small to fit the number of elements
    CountFieldOverflow,
    /// The data contained enough bytes but the contents were invalid
    InvalidBytes,
    /// A Rust reference cannot be created as the data is misaligned
    BadAlignment,
}
impl fmt::Display for SpError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            SpError::UnknownEnumVariant => write!(f, "Encountered invalid enum variant ID"),
            SpError::NotEnoughSpace => {
                write!(f, "Not enough bytes in the buffer to parse wanted type")
            }
            SpError::CountFieldOverflow => write!(
                f,
                "The count field's type is too small for the number of items !"
            ),
            SpError::InvalidBytes => write!(f, "Failed to parse the bytes into the wanted type"),
            SpError::BadAlignment => write!(f, "Input bytes are misaligned"),
        }
    }
}
impl error::Error for SpError {
    fn cause(&self) -> Option<&dyn error::Error> {
        Some(self)
    }
}