dcsv/
error.rs

1//! Error variants
2
3/// Result of dcsv operations
4pub type DcsvResult<T> = Result<T, DcsvError>;
5
6/// Error types for dcsv related operations.
7#[derive(Debug)]
8pub enum DcsvError {
9    InvalidLimiter(String),
10    InvalidValueType(String),
11    IoError(IoErrorWithMeta),
12    OutOfRangeError,
13    InsufficientRowData,
14    InvalidRowData(String),
15    InvalidColumn(String),
16    InvalidCellData(String),
17    CommandError(String),
18}
19
20impl std::fmt::Display for DcsvError {
21    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22        match self {
23            Self::InvalidLimiter(txt) => write!(f, "ERR : Invalid limiter =\n{0}", txt),
24            Self::InvalidValueType(txt) => write!(f, "ERR : Invalid type =\n{0}", txt),
25            Self::IoError(io_error) => write!(f, "ERR : IO Error =\n{0}", io_error),
26            Self::OutOfRangeError => write!(f, "ERR : Index out of range"),
27            Self::InsufficientRowData => write!(f, "ERR : Insufficient row data"),
28            Self::InvalidRowData(txt) => write!(f, "ERR : Invalid row data =\n{0}", txt),
29            Self::InvalidColumn(txt) => write!(f, "ERR : Invalid column =\n{0}", txt),
30            Self::InvalidCellData(txt) => write!(f, "ERR : Invalid cell data =\n{0}", txt),
31            Self::CommandError(txt) => write!(f, "ERR : Invalid command call =\n{0}", txt),
32        }
33    }
34}
35
36impl DcsvError {
37    pub fn io_error(err: std::io::Error, meta: &str) -> Self {
38        Self::IoError(IoErrorWithMeta::new(err, meta))
39    }
40}
41
42/// Specific error struct with meta information
43pub struct IoErrorWithMeta {
44    error: std::io::Error,
45    meta: String,
46}
47
48impl IoErrorWithMeta {
49    /// Create a new instance
50    pub fn new(error: std::io::Error, meta: &str) -> Self {
51        Self {
52            error,
53            meta: meta.to_owned(),
54        }
55    }
56}
57
58impl std::fmt::Debug for IoErrorWithMeta {
59    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60        write!(f, "{} :: {}", self.error, self.meta)
61    }
62}
63
64impl std::fmt::Display for IoErrorWithMeta {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        write!(f, "{} :: {}", self.error, self.meta)
67    }
68}