Skip to main content

sheets_diff/core/
error.rs

1use std::fmt;
2use std::path::PathBuf;
3
4#[cfg(feature = "serde_derive")]
5use serde::{Deserialize, Serialize};
6
7/// Identifies which workbook (old or new) an error refers to.
8#[derive(Clone, Copy, Debug, PartialEq, Eq)]
9#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
10pub enum WorkbookSide {
11    Old,
12    New,
13}
14
15impl fmt::Display for WorkbookSide {
16    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17        match self {
18            WorkbookSide::Old => write!(f, "old"),
19            WorkbookSide::New => write!(f, "new"),
20        }
21    }
22}
23
24/// Structured error type for all fallible `Diff` constructors.
25///
26/// Each variant carries enough context for a GUI application to display
27/// a precise error message identifying which side and which path or sheet
28/// caused the failure.
29#[derive(Debug)]
30pub enum SheetsDiffError {
31    /// The workbook file could not be opened or parsed.
32    OpenWorkbook {
33        side: WorkbookSide,
34        path: PathBuf,
35        source: calamine::XlsxError,
36    },
37    /// A workbook supplied as a reader could not be parsed.
38    OpenReader {
39        side: WorkbookSide,
40        source: calamine::XlsxError,
41    },
42    /// Reading cell values from a named worksheet failed.
43    ReadSheetValues {
44        side: WorkbookSide,
45        sheet: String,
46        source: calamine::XlsxError,
47    },
48    /// Reading cell formulas from a named worksheet failed.
49    ReadSheetFormulas {
50        side: WorkbookSide,
51        sheet: String,
52        source: calamine::XlsxError,
53    },
54}
55
56impl fmt::Display for SheetsDiffError {
57    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58        match self {
59            SheetsDiffError::OpenWorkbook { side, path, source } => write!(
60                f,
61                "cannot open {} workbook '{}': {}",
62                side,
63                path.display(),
64                source,
65            ),
66            SheetsDiffError::OpenReader { side, source } => {
67                write!(f, "cannot parse {} workbook from reader: {}", side, source)
68            }
69            SheetsDiffError::ReadSheetValues { side, sheet, source } => write!(
70                f,
71                "cannot read values from {} workbook sheet '{}': {}",
72                side, sheet, source,
73            ),
74            SheetsDiffError::ReadSheetFormulas { side, sheet, source } => write!(
75                f,
76                "cannot read formulas from {} workbook sheet '{}': {}",
77                side, sheet, source,
78            ),
79        }
80    }
81}
82
83impl std::error::Error for SheetsDiffError {
84    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
85        match self {
86            SheetsDiffError::OpenWorkbook { source, .. } => Some(source),
87            SheetsDiffError::OpenReader { source, .. } => Some(source),
88            SheetsDiffError::ReadSheetValues { source, .. } => Some(source),
89            SheetsDiffError::ReadSheetFormulas { source, .. } => Some(source),
90        }
91    }
92}