use csv;
use std::convert;
use std::fmt;
use std::io;
pub enum LoadErrorKind {
IOError,
CSVError,
Empty,
InvalidElement,
}
pub struct LoadError {
kind: LoadErrorKind,
message: String,
}
impl LoadError {
pub fn new(kind: LoadErrorKind, message: String) -> LoadError {
LoadError { kind, message }
}
pub fn kind(&self) -> &LoadErrorKind {
&self.kind
}
fn description(&self) -> String {
match self.kind {
LoadErrorKind::IOError => format!(
"Cannot load Matrix from file due to: {}",
self.message
),
LoadErrorKind::CSVError => {
format!("Cannot load Matrix, {}", self.message)
}
LoadErrorKind::Empty => {
format!("Cannot load Matrix from empty file")
}
LoadErrorKind::InvalidElement => format!(
"Cannot load Matrix, invalid element: {}",
self.message
),
}
}
}
impl convert::From<io::Error> for LoadError {
fn from(error: io::Error) -> Self {
LoadError {
kind: LoadErrorKind::IOError,
message: format!("{}", error),
}
}
}
impl convert::From<csv::Error> for LoadError {
fn from(error: csv::Error) -> Self {
LoadError {
kind: LoadErrorKind::CSVError,
message: format!("{}", error),
}
}
}
impl fmt::Debug for LoadError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.description())
}
}
impl fmt::Display for LoadError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.description())
}
}