fixed_width/
error.rs

1use crate::{de::DeserializeError, ser::SerializeError};
2use std::{error::Error as StdError, fmt, io, string};
3
4/// An error produced while parsing fixed width data.
5#[derive(Debug)]
6pub enum Error {
7    /// An IO error occured while reading the data.
8    IOError(io::Error),
9    /// A record could not be converted into valid UTF-8.
10    FormatError(string::FromUtf8Error),
11    /// An error occurred during deserialization.
12    DeserializeError(DeserializeError),
13    /// An error occurred during serialization.
14    SerializeError(SerializeError),
15}
16
17impl fmt::Display for Error {
18    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
19        match self {
20            Error::IOError(ref e) => write!(f, "{}", e),
21            Error::FormatError(ref e) => write!(f, "{}", e),
22            Error::DeserializeError(ref e) => write!(f, "{}", e),
23            Error::SerializeError(ref e) => write!(f, "{}", e),
24        }
25    }
26}
27
28impl From<io::Error> for Error {
29    fn from(e: io::Error) -> Self {
30        Error::IOError(e)
31    }
32}
33
34impl From<DeserializeError> for Error {
35    fn from(e: DeserializeError) -> Self {
36        Error::DeserializeError(e)
37    }
38}
39
40impl From<SerializeError> for Error {
41    fn from(e: SerializeError) -> Self {
42        Error::SerializeError(e)
43    }
44}
45
46impl StdError for Error {
47    fn cause(&self) -> Option<&dyn StdError> {
48        match self {
49            Error::IOError(ref e) => Some(e),
50            Error::FormatError(ref e) => Some(e),
51            Error::DeserializeError(ref e) => Some(e),
52            Error::SerializeError(ref e) => Some(e),
53        }
54    }
55}