simd_csv/
error.rs

1use std::{error, fmt, io, result};
2
3#[derive(Debug)]
4pub enum 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    pub fn is_io_error(&self) -> bool {
23        matches!(self.0, ErrorKind::Io(_))
24    }
25
26    pub fn kind(&self) -> &ErrorKind {
27        &self.0
28    }
29
30    pub fn into_kind(self) -> ErrorKind {
31        self.0
32    }
33}
34
35impl From<io::Error> for Error {
36    fn from(err: io::Error) -> Self {
37        Self(ErrorKind::Io(err))
38    }
39}
40
41impl From<Error> for io::Error {
42    fn from(err: Error) -> Self {
43        Self::new(io::ErrorKind::Other, err)
44    }
45}
46
47impl error::Error for Error {}
48
49impl fmt::Display for Error {
50    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
51        match self.0 {
52            ErrorKind::Io(ref err) => err.fmt(f),
53            ErrorKind::UnequalLengths { expected_len, len } => write!(
54                f,
55                "CSV error: found record with {} fields, but the previous record has {} fields",
56                len, expected_len
57            ),
58            ErrorKind::InvalidHeaders => {
59                write!(f, "invalid headers or headers too long for buffer")
60            }
61        }
62    }
63}
64
65pub type Result<T> = result::Result<T, Error>;