csv_lib/models/
csv_error.rs

1use std::fmt::{Display, Formatter};
2use std::io;
3use std::io::Error;
4
5#[derive(Debug)]
6#[derive(Clone)]
7#[allow(dead_code)]
8/// ## CsvError
9/// - An enum, that handle the different types of error, that the library can produce.
10pub enum CsvError {
11    IO(String),
12    Parse(i32,i32,String),
13    Decode(String),
14    FileError(String),
15    Unknow
16
17}
18
19/// ## Display implementation
20/// - Implement the fmt function for the trait.
21impl Display for CsvError {
22    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
23        match self {
24            CsvError::IO(e) => {
25                write!(f, "IO Error: {}", e)
26            }
27            CsvError::Parse(row,col , v) => {
28                write!(f, "Row {}, Column {}: Extracted value {}, failed", row, col, v)
29            }
30            CsvError::FileError(e) => {
31                write!(f, "File Error: {}", e)
32            }
33            CsvError::Decode(e) => {
34                write!(f, "Error decoding: {}", e)
35            }
36            CsvError::Unknow => {
37                write!(f, "Unknown error")
38            }
39        }
40    }
41}
42
43/// Default implent of std:err::Error for CsvError.
44impl std::error::Error for CsvError {}
45
46/// ## Implements io::Error for CsvError.
47/// - Allows to cast an io::Error into a CsvError
48impl From<io::Error> for CsvError {
49    fn from(value: Error) -> Self {
50        CsvError::IO(value.to_string())
51    }
52}