1use std::path::PathBuf;
3use std::result;
4use std::io;
5
6use crate::Row;
7
8#[derive(Debug, Clone, PartialEq)]
10pub enum Error {
11    Csv(String),
16
17    InconsistentHeaders,
19
20    InconsistentSizeOfRows(PathBuf),
23
24    DuplicatedColumn(String),
27
28    ColumnNotFound(String),
31
32    ValueError {
35        data: String,
36        to_type: String,
37    },
38
39    ReNoMatch(String, String),
41
42    InvalidFormat(String),
44
45    IOError(String),
47
48    NoSources,
50}
51
52pub type Result<T> = result::Result<T, Error>;
53
54pub type RowResult = result::Result<Row, Error>;
57
58impl From<csv::Error> for Error {
59    fn from(error: csv::Error) -> Error {
60        Error::Csv(format!("{:?}", error))
61    }
62}
63
64impl From<io::Error> for Error {
65    fn from(error: io::Error) -> Error {
66        Error::IOError(format!("{:?}", error))
67    }
68}
69
70impl std::error::Error for Error { }
71
72impl std::fmt::Display for Error {
73    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
74        match self {
75            Error::Csv(e) => write!(f, "Error in underlaying csv library: {}", e),
76            Error::InconsistentSizeOfRows(path) => write!(f, "inconsistent size of rows for path {:?}", path),
77            Error::InconsistentHeaders => write!(f, "inconsistent headers among input files"),
78            Error::DuplicatedColumn(name) => write!(f, "Column {} already exists and cannot be added again", name),
79            Error::ColumnNotFound(name) => write!(f, "Requested unexistent column '{}'", name),
80            Error::ValueError { data, to_type } => write!(f, "Could not convert value '{}' to type {}", data, to_type),
81            Error::ReNoMatch(re, string) => write!(f, "String '{}' doesn't match regular expression {}", string, re),
82            Error::InvalidFormat(format) => write!(f, "Template string '{}' is not valid", format),
83            Error::IOError(msg) => write!(f, "IOError: {}", msg),
84            Error::NoSources => write!(f, "Attempted to build a row stream without input files"),
85        }
86    }
87}