1use std::io;
2use std::num::{ParseFloatError, ParseIntError};
3use thiserror::Error;
4use zip::result::ZipError;
5
6#[derive(Error, Debug, PartialEq, Eq)]
7pub enum XlsxError {
8 #[error("I/O Error: {0}")]
9 IO(String),
10 #[error("Zip Error: {0}")]
11 Zip(String),
12 #[error("XML Error: {0}")]
13 Xml(String),
14 #[error("{0}")]
15 Workbook(String),
16 #[error("Evaluation Error: {}", .0.join("; "))]
17 Evaluation(Vec<String>),
18 #[error("Comparison Error: {0}")]
19 Comparison(String),
20 #[error("Not Implemented Error: {0}")]
21 NotImplemented(String),
22}
23
24impl From<io::Error> for XlsxError {
25 fn from(error: io::Error) -> Self {
26 XlsxError::IO(error.to_string())
27 }
28}
29
30impl From<ZipError> for XlsxError {
31 fn from(error: ZipError) -> Self {
32 XlsxError::Zip(error.to_string())
33 }
34}
35
36impl From<ParseIntError> for XlsxError {
37 fn from(error: ParseIntError) -> Self {
38 XlsxError::Xml(error.to_string())
39 }
40}
41
42impl From<ParseFloatError> for XlsxError {
43 fn from(error: ParseFloatError) -> Self {
44 XlsxError::Xml(error.to_string())
45 }
46}
47
48impl From<roxmltree::Error> for XlsxError {
49 fn from(error: roxmltree::Error) -> Self {
50 XlsxError::Xml(error.to_string())
51 }
52}
53
54impl XlsxError {
55 pub fn user_message(&self) -> String {
56 match &self {
57 XlsxError::IO(_) | XlsxError::Workbook(_) => self.to_string(),
58 XlsxError::Zip(_) | XlsxError::Xml(_) => {
59 "IronCalc can only open workbooks created by Microsoft Excel. \
60 Can you open this file with Excel, save it to a new file, \
61 and then open that new file with IronCalc? If you've already tried this, \
62 then send this workbook to support@ironcalc.com and our engineering team \
63 will work with you to fix the issue."
64 .to_string()
65 }
66 XlsxError::NotImplemented(error) => format!(
67 "IronCalc cannot open this workbook due to the following unsupported features: \
68 {error}. You can either re-implement these parts of your workbook using features \
69 supported by IronCalc, or you can send this workbook to support@ironcalc.com \
70 and our engineering team will work with you to fix the issue.",
71 ),
72 XlsxError::Evaluation(errors) => format!(
73 "IronCalc could not evaluate this workbook without errors. This may indicate a bug or missing feature \
74 in the IronCalc spreadsheet calculation engine. Please contact support@ironcalc.com, share the entirety \
75 of this error message and the relevant workbook, and we will work with you to resolve the issue. \
76 Detailed error message:\n{}",
77 errors.join("\n")
78 ),
79 XlsxError::Comparison(error) => format!(
80 "IronCalc produces different results when evaluating the workbook \
81 than those already present in the workbook. This may indicate a bug or missing \
82 feature in the IronCalc spreadsheet calculation engine. Please contact \
83 support@ironcalc.com, share the entirety of this error message and the relevant \
84 workbook, and we will work with you to resolve the issue. \
85 Detailed error message:\n{error}"
86 ),
87 }
88 }
89}