table_extractor/
error.rs

1use std::fmt;
2
3/// Error types for table parsing and writing operations.
4///
5/// This enum represents all possible errors that can occur during
6/// table operations, including parsing, validation, and I/O.
7#[derive(Debug)]
8pub enum Error {
9    /// Error during table parsing.
10    ///
11    /// This variant is used when input data cannot be parsed into a valid table.
12    /// The string contains details about what went wrong.
13    ParseError(String),
14
15    /// I/O error during reading or writing.
16    ///
17    /// This wraps standard I/O errors that occur when reading input or
18    /// writing output.
19    IoError(std::io::Error),
20
21    /// Invalid format or format constraint violation.
22    ///
23    /// Used when the input or output violates format constraints,
24    /// such as exceeding the maximum column count or containing
25    /// invalid delimiter characters.
26    InvalidFormat(String),
27
28    /// Inconsistent column count in table data.
29    ///
30    /// This error occurs when a table row has a different number of
31    /// columns than the header row.
32    ///
33    /// # Fields
34    ///
35    /// - `row`: The row number (1-indexed) that has inconsistent columns
36    /// - `expected`: The expected number of columns (from header)
37    /// - `found`: The actual number of columns found in the row
38    InconsistentColumns {
39        /// The row number (1-indexed) with inconsistent columns
40        row: usize,
41        /// Expected number of columns
42        expected: usize,
43        /// Actual number of columns found
44        found: usize,
45    },
46}
47
48impl fmt::Display for Error {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        match self {
51            Error::ParseError(msg) => write!(f, "{}", msg),
52            Error::IoError(err) => write!(f, "{}", err),
53            Error::InvalidFormat(msg) => write!(f, "{}", msg),
54            Error::InconsistentColumns {
55                row,
56                expected,
57                found,
58            } => {
59                write!(
60                    f,
61                    "Inconsistent column count at row {}: expected {}, found {}",
62                    row, expected, found
63                )
64            }
65        }
66    }
67}
68
69impl std::error::Error for Error {
70    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
71        match self {
72            Error::IoError(err) => Some(err),
73            _ => None,
74        }
75    }
76}
77
78impl From<std::io::Error> for Error {
79    fn from(err: std::io::Error) -> Self {
80        Error::IoError(err)
81    }
82}
83
84impl From<csv::Error> for Error {
85    fn from(err: csv::Error) -> Self {
86        Error::ParseError(err.to_string())
87    }
88}
89
90/// Type alias for `Result<T, Error>`.
91///
92/// This is a convenience type that uses the library's [`Error`] type
93/// as the error variant.
94///
95/// # Examples
96///
97/// ```
98/// use table_extractor::error::Result;
99/// use table_extractor::Table;
100///
101/// fn create_table() -> Result<Table> {
102///     Table::new_validated(
103///         vec!["id".to_string(), "name".to_string()],
104///         vec![vec!["1".to_string(), "Alice".to_string()]],
105///     )
106/// }
107/// ```
108pub type Result<T> = std::result::Result<T, Error>;