Skip to main content

truecalc_workbook/
error.rs

1use std::fmt;
2
3/// Errors from workbook value-type constructors and from
4/// [`Workbook::from_json`](crate::Workbook::from_json) /
5/// [`Workbook::to_json`](crate::Workbook::to_json).
6#[derive(Debug, Clone, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum WorkbookError {
9    /// A formula-less cell whose value is `empty` is invalid: it would be
10    /// byte-distinguishable from the absent cell it denotes, breaking
11    /// canonical uniqueness (schema spec §4). Clear a cell by removing its
12    /// entry from the sheet's cell map instead.
13    EmptyLiteral,
14    /// A document-level rule was violated while parsing or serializing — a
15    /// rule serde cannot express (schema spec §1, §2, §3, §5, §7, §8) or a
16    /// resource-limit breach (scope ADR Decision 5). Carries a human-readable
17    /// description of the first violation found.
18    Validation(String),
19    /// A sheet-management operation
20    /// ([`add_sheet`](crate::Workbook::add_sheet),
21    /// [`insert_sheet`](crate::Workbook::insert_sheet),
22    /// [`rename_sheet`](crate::Workbook::rename_sheet),
23    /// [`move_sheet`](crate::Workbook::move_sheet)) violated an invariant —
24    /// a duplicate sheet name under simple case folding (schema spec §2), a
25    /// name that is empty or exceeds the length limit (schema spec §3), the
26    /// per-workbook sheet cap (scope ADR Decision 5), an out-of-range tab
27    /// position, or an unknown sheet name. Carries a human-readable
28    /// description.
29    SheetManagement(String),
30    /// A workbook-level mutation
31    /// ([`set`](crate::Workbook::set), [`define_name`](crate::Workbook::define_name),
32    /// [`redefine_name`](crate::Workbook::redefine_name)) violated an invariant —
33    /// an empty-literal write (use clear), an unknown sheet, a syntactically
34    /// invalid formula for the locked engine, an invalid or non-canonical
35    /// named-range name/`ref`, a `ref` to a missing sheet (schema spec §7), a
36    /// duplicate name under simple case folding, or a per-mutation resource cap
37    /// (cells / text / array / formula / named-range count, scope ADR
38    /// Decision 5). Carries a human-readable description.
39    Mutation(String),
40}
41
42impl fmt::Display for WorkbookError {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        match self {
45            WorkbookError::EmptyLiteral => write!(
46                f,
47                "a literal cell cannot hold an empty value; \
48                 clear a cell by removing its entry instead"
49            ),
50            WorkbookError::Validation(msg) => write!(f, "{msg}"),
51            WorkbookError::SheetManagement(msg) => write!(f, "{msg}"),
52            WorkbookError::Mutation(msg) => write!(f, "{msg}"),
53        }
54    }
55}
56
57impl std::error::Error for WorkbookError {}