1use std::{error, fmt, io, result};
2
3#[derive(Debug)]
4enum ErrorKind {
5 Io(io::Error),
6 UnequalLengths { expected_len: usize, len: usize },
7 InvalidHeaders,
8}
9
10#[derive(Debug)]
11pub struct Error(ErrorKind);
12
13impl Error {
14 pub(crate) fn unequal_lengths(expected_len: usize, len: usize) -> Self {
15 Self(ErrorKind::UnequalLengths { expected_len, len })
16 }
17
18 pub(crate) fn invalid_headers() -> Self {
19 Self(ErrorKind::InvalidHeaders)
20 }
21}
22
23impl From<io::Error> for Error {
24 fn from(err: io::Error) -> Self {
25 Self(ErrorKind::Io(err))
26 }
27}
28
29impl From<Error> for io::Error {
30 fn from(err: Error) -> Self {
31 Self::new(io::ErrorKind::Other, err)
32 }
33}
34
35impl error::Error for Error {}
36
37impl fmt::Display for Error {
38 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39 match self.0 {
40 ErrorKind::Io(ref err) => err.fmt(f),
41 ErrorKind::UnequalLengths { expected_len, len } => write!(
42 f,
43 "CSV error: found record with {} fields, but the previous record has {} fields",
44 len, expected_len
45 ),
46 ErrorKind::InvalidHeaders => {
47 write!(f, "invalid headers or headers too long for buffer")
48 }
49 }
50 }
51}
52
53pub type Result<T> = result::Result<T, Error>;