use std::error::Error as StdError;
use std::fmt;
use std::result;
pub type Result<T> = result::Result<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)]
#[non_exhaustive]
pub enum ErrorKind {
UnequalLengths {
expected_len: u64,
len: u64,
},
Serialize(String),
}
impl StdError for Error {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match *self.0 {
ErrorKind::UnequalLengths { .. } => None,
ErrorKind::Serialize(_) => None,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self.0 {
ErrorKind::UnequalLengths {
expected_len,
len,
} => {
write!(
f,
"CSV error: \
found record with {} fields, but the previous record \
has {} fields",
len, expected_len
)
}
ErrorKind::Serialize(ref err) => {
write!(f, "CSV write error: {}", err)
}
}
}
}