csvsc/
error.rs

1//! All the things that can go wrong.
2use std::path::PathBuf;
3use std::result;
4use std::io;
5
6use crate::Row;
7
8/// An error found somewhere in the transformation chain.
9#[derive(Debug, Clone, PartialEq)]
10pub enum Error {
11    // TODO derive partialeq, eq and hash. This will
12    // allow for errors to be grouped and streamed in groups
13    /// An error ocurred in the underlaying csv library, likely a permissions
14    /// issue.
15    Csv(String),
16
17    /// The headers of two of the input csv files do not match each other.
18    InconsistentHeaders,
19
20    /// A stream was found that has a row with different number of fields than
21    /// the rest.
22    InconsistentSizeOfRows(PathBuf),
23
24    /// You tried to add a column to the stream but that name was alredy in
25    /// use.
26    DuplicatedColumn(String),
27
28    /// You tried to use a column as source for something but that column does
29    /// not exist.
30    ColumnNotFound(String),
31
32    /// An error ocurred trying to convert a value from string to a different
33    /// data type.
34    ValueError {
35        data: String,
36        to_type: String,
37    },
38
39    /// String didn't match regular expression. Likely in the .add() method.
40    ReNoMatch(String, String),
41
42    /// Template string for defining a new column had an invalid format.
43    InvalidFormat(String),
44
45    /// An IO error ocurred, and this is its representation.
46    IOError(String),
47
48    /// You atempted to create a row stream without adding any source files.
49    NoSources,
50}
51
52pub type Result<T> = result::Result<T, Error>;
53
54/// The type that actually flows through the row stream. Either a row or an
55/// error.
56pub 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}