use std::{
error::Error as StdError,
fmt, io,
num::{ParseFloatError, ParseIntError},
result::Result as StdResult,
};
pub type Result<T> = StdResult<T, Error>;
#[derive(Debug)]
pub struct Error(Box<ErrorKind>);
impl Error {
pub(crate) fn new(kind: ErrorKind) -> Error {
Error(Box::new(kind))
}
pub fn kind(&self) -> &ErrorKind {
&self.0
}
pub fn into_kind(self) -> ErrorKind {
*self.0
}
}
#[derive(Debug)]
pub enum ErrorKind {
Io(io::Error),
Int(ParseIntError),
Float(ParseFloatError),
ReadRecord(String),
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Error {
Error::new(ErrorKind::Io(err))
}
}
impl From<ParseIntError> for Error {
fn from(err: ParseIntError) -> Self {
Error::new(ErrorKind::Int(err))
}
}
impl From<ParseFloatError> for Error {
fn from(err: ParseFloatError) -> Self {
Error::new(ErrorKind::Float(err))
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self.0 {
ErrorKind::Io(ref err) => write!(f, "I/O error - {}", err),
ErrorKind::Int(ref err) => write!(f, "parsing integer error - {}", err),
ErrorKind::Float(ref err) => write!(f, "parsing float error - {}", err),
ErrorKind::ReadRecord(ref err) => write!(f, "reading record - {}", err),
}
}
}
impl StdError for Error {}