ced/
error.rs

1use dcsv::DcsvError;
2
3pub type CedResult<T> = Result<T, CedError>;
4
5#[derive(Debug)]
6pub enum CedError {
7    CommandError(String),
8    CsvDataError(DcsvError),
9    InvalidColumn(String),
10    InvalidPageOperation(String),
11    InvalidRowData(String),
12    IoError(IoErrorWithMeta),
13    OutOfRangeError,
14}
15
16impl std::fmt::Display for CedError {
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        match self {
19            Self::CommandError(txt) => write!(f, "ERR : Invalid command call =\n{0}", txt),
20            Self::CsvDataError(err) => write!(f, "{err}"),
21            Self::InvalidColumn(txt) => write!(f, "ERR : Invalid column =\n{0}", txt),
22            Self::InvalidPageOperation(txt) => {
23                write!(f, "ERR : Invalid page operation =\n{0}", txt)
24            }
25            Self::InvalidRowData(txt) => write!(f, "ERR : Invalid row data =\n{0}", txt),
26            Self::IoError(io_error) => write!(f, "ERR : IO Error =\n{0}", io_error),
27            Self::OutOfRangeError => write!(f, "ERR : Index out of range"),
28        }
29    }
30}
31
32impl From<DcsvError> for CedError {
33    fn from(err: DcsvError) -> Self {
34        Self::CsvDataError(err)
35    }
36}
37
38impl CedError {
39    pub fn io_error(err: std::io::Error, meta: &str) -> Self {
40        Self::IoError(IoErrorWithMeta::new(err, meta))
41    }
42}
43
44pub struct IoErrorWithMeta {
45    error: std::io::Error,
46    meta: String,
47}
48
49impl IoErrorWithMeta {
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}