spreadsheet_to_json/
error.rs

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