quick_csv/
error.rs

1//! Error management module
2//! 
3//! Provides all csv error conversion and description
4//! Also provides `Result` as a alias of `Result<_, Error>
5
6use std::fmt;
7use std::io;
8
9/// An error produced by an operation on CSV data.
10#[derive(Debug)]
11pub enum Error {
12    /// An error reported by the type-based decoder.
13    Decode(String),
14    /// An error reported by the CSV parser.
15    Parse(String),
16    /// An error originating from reading or writing to the underlying buffer.
17    Io(io::Error),
18    /// An error originating from finding end of line instead of a column.
19    EOL,
20    /// Unescaped quote
21    UnescapedQuote,
22    /// Unexpected quote in a column which is non quoted column
23    UnexpextedQuote,
24    /// Column count mismatch
25    ColumnMismatch(usize, usize),
26}
27
28/// Result type
29pub 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