Skip to main content

spreadsheet_to_json/
error.rs

1use std::error::Error;
2use std::fmt;
3
4/// Simple GenericError type to cover other error type with different implementations
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub struct GenericError(pub &'static str);
7
8impl fmt::Display for GenericError {
9    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10        write!(f, "{}", self.0)
11    }
12}
13
14impl Error for GenericError {}
15
16/// convert IO Error to GenericError
17impl From<calamine::Error> for GenericError {
18    fn from(error: calamine::Error) -> Self {
19        match error {
20            calamine::Error::Io(_) => GenericError("io_error"),
21            calamine::Error::Xlsx(_) => GenericError("xlsx_error"),
22            calamine::Error::Xls(_) => GenericError("xlsx_error"),
23            calamine::Error::Ods(_) => GenericError("ods_error"),
24            // Add more patterns as needed for other calamine::Error variants
25            _ => GenericError("unknown_calamine_error"),
26        }
27    }
28}
29
30/// convert IO Error to GenericError
31impl From<std::io::Error> for GenericError {
32    fn from(error: std::io::Error) -> Self {
33        match error.kind() {
34            std::io::ErrorKind::NotFound => GenericError("file_not_found"),
35            std::io::ErrorKind::PermissionDenied => GenericError("permission_denied"),
36            std::io::ErrorKind::ConnectionRefused => GenericError("connection_refused"),
37            _ => GenericError("io_error"),
38        }
39    }
40}