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}
8
9#[derive(Debug)]
10pub struct Error(ErrorKind);
11
12impl Error {
13 pub(crate) fn unequal_lengths(expected_len: usize, len: usize) -> Self {
14 Self(ErrorKind::UnequalLengths { expected_len, len })
15 }
16
17 pub fn is_io_error(&self) -> bool {
18 matches!(self.0, ErrorKind::Io(_))
19 }
20
21 pub fn kind(&self) -> &ErrorKind {
22 &self.0
23 }
24
25 pub fn into_kind(self) -> ErrorKind {
26 self.0
27 }
28}
29
30impl From<io::Error> for Error {
31 fn from(err: io::Error) -> Self {
32 Self(ErrorKind::Io(err))
33 }
34}
35
36impl From<Error> for io::Error {
37 fn from(err: Error) -> Self {
38 Self::new(io::ErrorKind::Other, err)
39 }
40}
41
42impl error::Error for Error {}
43
44impl fmt::Display for Error {
45 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
46 match self.0 {
47 ErrorKind::Io(ref err) => err.fmt(f),
48 ErrorKind::UnequalLengths { expected_len, len } => write!(
49 f,
50 "CSV error: found record with {} fields, but the previous record has {} fields",
51 len, expected_len
52 ),
53 }
54 }
55}
56
57pub type Result<T> = result::Result<T, Error>;