use std::path::PathBuf;
use std::result;
use std::io;
use crate::Row;
#[derive(Debug, Clone, PartialEq)]
pub enum Error {
Csv(String),
InconsistentHeaders,
InconsistentSizeOfRows(PathBuf),
DuplicatedColumn(String),
ColumnNotFound(String),
ValueError {
data: String,
to_type: String,
},
ReNoMatch(String, String),
InvalidFormat(String),
IOError(String),
NoSources,
}
pub type Result<T> = result::Result<T, Error>;
pub type RowResult = result::Result<Row, Error>;
impl From<csv::Error> for Error {
fn from(error: csv::Error) -> Error {
Error::Csv(format!("{:?}", error))
}
}
impl From<io::Error> for Error {
fn from(error: io::Error) -> Error {
Error::IOError(format!("{:?}", error))
}
}
impl std::error::Error for Error { }
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::Csv(e) => write!(f, "Error in underlaying csv library: {}", e),
Error::InconsistentSizeOfRows(path) => write!(f, "inconsistent size of rows for path {:?}", path),
Error::InconsistentHeaders => write!(f, "inconsistent headers among input files"),
Error::DuplicatedColumn(name) => write!(f, "Column {} already exists and cannot be added again", name),
Error::ColumnNotFound(name) => write!(f, "Requested unexistent column '{}'", name),
Error::ValueError { data, to_type } => write!(f, "Could not convert value '{}' to type {}", data, to_type),
Error::ReNoMatch(re, string) => write!(f, "String '{}' doesn't match regular expression {}", string, re),
Error::InvalidFormat(format) => write!(f, "Template string '{}' is not valid", format),
Error::IOError(msg) => write!(f, "IOError: {}", msg),
Error::NoSources => write!(f, "Attempted to build a row stream without input files"),
}
}
}