Skip to main content

formualizer_sheetport/
error.rs

1use crate::validation::ConstraintViolation;
2use formualizer_common::ExcelError;
3use formualizer_workbook::error::IoError;
4use sheetport_spec::{ManifestIssue, ValidationError};
5use thiserror::Error;
6
7/// Errors produced when constructing or operating a SheetPort runtime.
8#[derive(Debug, Error)]
9#[non_exhaustive]
10pub enum SheetPortError {
11    /// Manifest failed canonical validation.
12    #[error("manifest validation failed")]
13    InvalidManifest { issues: Vec<ManifestIssue> },
14    /// Selector combination is not yet supported for the given port.
15    #[error("unsupported selector for port `{port}`: {reason}")]
16    UnsupportedSelector { port: String, reason: String },
17    /// Reference string could not be parsed.
18    #[error("invalid reference `{reference}` in port `{port}`: {details}")]
19    InvalidReference {
20        port: String,
21        reference: String,
22        details: String,
23    },
24    /// Referenced sheet was not present in the workbook.
25    #[error("sheet `{sheet}` referenced by port `{port}` was not found in the workbook")]
26    MissingSheet { port: String, sheet: String },
27    /// Structural invariant could not be satisfied.
28    #[error("invariant violation for port `{port}`: {message}")]
29    InvariantViolation { port: String, message: String },
30    /// A bounded layout selector did not observe its configured terminator.
31    #[error(
32        "layout selector for port `{port}` on sheet `{sheet}` exhausted {limit} rows from {scan_start} using `{termination}`"
33    )]
34    LayoutExhausted {
35        port: String,
36        sheet: String,
37        termination: String,
38        scan_start: u32,
39        limit: u32,
40        observed: u32,
41    },
42    /// A selector could not be proven safe inside its evaluation envelope.
43    #[error("selector safety error for port `{port}`: {reason}")]
44    SelectorSafety { port: String, reason: String },
45    /// Input or resolved data violated manifest constraints.
46    #[error("value did not satisfy manifest constraints")]
47    ConstraintViolation {
48        violations: Vec<ConstraintViolation>,
49    },
50    /// Baseline restoration failed after a scenario failure.
51    #[error("batch failed ({primary}); baseline restoration also failed ({restoration})")]
52    BatchRestoration {
53        primary: Box<SheetPortError>,
54        restoration: Box<SheetPortError>,
55    },
56    /// Underlying engine reported an evaluation error.
57    #[error("engine error: {source}")]
58    Engine {
59        #[from]
60        source: ExcelError,
61    },
62    /// Failure when interacting with the underlying workbook backend.
63    #[error("workbook error: {source}")]
64    Workbook {
65        #[from]
66        source: IoError,
67    },
68}
69
70impl From<ValidationError> for SheetPortError {
71    fn from(err: ValidationError) -> Self {
72        SheetPortError::InvalidManifest {
73            issues: err.issues().to_vec(),
74        }
75    }
76}