1use std::fmt;
7use std::io;
8
9#[derive(Debug)]
11pub enum Error {
12 Decode(String),
14 Parse(String),
16 Io(io::Error),
18 EOL,
20 UnescapedQuote,
22 UnexpextedQuote,
24 ColumnMismatch(usize, usize),
26}
27
28pub type Result<T> = ::std::result::Result<T, Error>;
30
31impl fmt::Display for Error {
32 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
33 match *self {
34 Error::Decode(ref msg) => write!(f, "CSV decode error: {}", msg),
35 Error::Parse(ref err) => write!(f, "{}", err),
36 Error::Io(ref err) => write!(f, "{}", err),
37 Error::EOL => write!(f, "Trying to access column but found End Of Line"),
38 Error::UnescapedQuote => write!(f, "A CSV column has an unescaped quote"),
39 Error::UnexpextedQuote => write!(f, "A CSV column has a quote but the entire column value is not quoted"),
40 Error::ColumnMismatch(exp, cur) => write!(f, "Expectiong {} columns, found {}", exp, cur),
41 }
42 }
43}
44
45impl ::std::error::Error for Error {
46 fn description(&self) -> &str {
47 match *self {
48 Error::Decode(..) => "CSV decoding error",
49 Error::Parse(..) => "CSV parse error",
50 Error::Io(..) => "CSV IO error",
51 Error::EOL => "Trying to access column but found End Of Line",
52 Error::UnescapedQuote => "A CSV column has an unescaped quote",
53 Error::UnexpextedQuote => "A CSV column has a quote but the entire column value is not quoted",
54 Error::ColumnMismatch(..) => "Current column count mismatch with previous rows",
55 }
56 }
57
58 fn cause(&self) -> Option<&::std::error::Error> {
59 match *self {
60 Error::Io(ref err) => Some(err),
61 _ => None,
62 }
63 }
64}
65
66impl From<::std::io::Error> for Error {
67 fn from(err: ::std::io::Error) -> Error { Error::Io(err) }
68}
69