cellrune 0.1.17

Bounded XLSX/XLSM reading, deterministic calculation, editing, and writing for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
use std::error::Error;
use std::fmt;

use crate::{CellRange, FormulaText, SheetId, WorkbookSnapshot};

use super::{CalculationOptions, CancellationToken};

mod analyzer;
#[cfg(test)]
mod tests;

const MESSAGE_ZERO_LIMIT: &str = "defined-name analysis limit must be greater than zero";
const MESSAGE_UNKNOWN_SHEET: &str = "current sheet does not exist in the workbook";
const MESSAGE_RESOURCE_LIMIT: &str = "defined-name analysis exceeded a configured resource limit";
const MESSAGE_CANCELLED: &str = "defined-name analysis was cancelled";

/// Stable workbook-order identity of a continuous 3-D sheet span.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct DefinedNameSheetSpan {
    start: SheetId,
    end: SheetId,
}

impl DefinedNameSheetSpan {
    pub(super) const fn new(start: SheetId, end: SheetId) -> Self {
        Self { start, end }
    }

    /// Returns the first sheet in workbook order.
    pub const fn start(self) -> SheetId {
        self.start
    }

    /// Returns the last sheet in workbook order.
    pub const fn end(self) -> SheetId {
        self.end
    }
}

/// One area in an ordered non-rectangular defined-name reference.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum DefinedNameReferenceArea {
    /// One rectangle on one worksheet.
    Rectangular {
        /// Stable worksheet identity.
        sheet_id: SheetId,
        /// Resolved A1 rectangle.
        range: CellRange,
    },
    /// One rectangle repeated across a continuous worksheet span.
    ThreeDimensional {
        /// Stable workbook-order sheet span.
        sheet_span: DefinedNameSheetSpan,
        /// Resolved A1 rectangle shared by each sheet.
        range: CellRange,
    },
}

/// Dynamic reference construct that determines a name's reference shape at calculation time.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum DefinedNameDynamicKind {
    /// The terminal reference expression is `OFFSET`.
    Offset,
    /// The terminal reference expression is `INDIRECT`.
    Indirect,
    /// The terminal reference expression is a spill reference.
    Spill,
    /// Multiple dynamic reference constructs contribute to the result.
    Mixed,
}

impl DefinedNameDynamicKind {
    /// Returns the stable transport spelling.
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Offset => "offset",
            Self::Indirect => "indirect",
            Self::Spill => "spill",
            Self::Mixed => "mixed",
        }
    }
}

/// Typed target category of an external-workbook reference.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum DefinedNameExternalTargetKind {
    /// A cell, area, whole-row, or whole-column reference.
    Reference,
    /// An external defined name.
    DefinedName,
    /// An external structured table reference.
    StructuredReference,
}

impl DefinedNameExternalTargetKind {
    /// Returns the stable transport spelling.
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Reference => "reference",
            Self::DefinedName => "defined_name",
            Self::StructuredReference => "structured_reference",
        }
    }
}

/// Typed detail retained from an external-workbook reference node.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DefinedNameExternalReference {
    locator: Option<Box<str>>,
    workbook: Box<str>,
    sheet: Option<Box<str>>,
    sheet_end: Option<Box<str>>,
    target: DefinedNameExternalTargetKind,
    target_text: Box<str>,
}

impl DefinedNameExternalReference {
    pub(super) fn new(
        locator: Option<Box<str>>,
        workbook: Box<str>,
        sheet: Option<Box<str>>,
        sheet_end: Option<Box<str>>,
        target: DefinedNameExternalTargetKind,
        target_text: Box<str>,
    ) -> Self {
        Self {
            locator,
            workbook,
            sheet,
            sheet_end,
            target,
            target_text,
        }
    }

    /// Returns the optional path or URI prefix before the bracketed workbook token.
    pub fn locator(&self) -> Option<&str> {
        self.locator.as_deref()
    }

    /// Returns the external workbook token without its surrounding brackets or locator.
    pub fn workbook(&self) -> &str {
        &self.workbook
    }

    /// Returns the optional first external sheet token.
    pub fn sheet(&self) -> Option<&str> {
        self.sheet.as_deref()
    }

    /// Returns the optional final external sheet token of a 3-D prefix.
    pub fn sheet_end(&self) -> Option<&str> {
        self.sheet_end.as_deref()
    }

    /// Returns the typed external target category.
    pub const fn target(&self) -> DefinedNameExternalTargetKind {
        self.target
    }

    /// Returns the canonical external target text without its workbook or sheet prefix.
    pub fn target_text(&self) -> &str {
        &self.target_text
    }
}

/// Reason a reachable defined-name formula is invalid against the immutable workbook snapshot.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum DefinedNameInvalidReason {
    /// The selected or reachable formula does not parse.
    ParseError,
    /// A non-callable value-name chain contains a cycle.
    CircularReference,
    /// A reachable name is absent from its applicable scope chain.
    UnresolvedName,
    /// A static reference names an absent sheet, table, column, or invalid range.
    InvalidReference,
}

impl DefinedNameInvalidReason {
    /// Returns the stable transport spelling.
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::ParseError => "parse_error",
            Self::CircularReference => "circular_reference",
            Self::UnresolvedName => "unresolved_name",
            Self::InvalidReference => "invalid_reference",
        }
    }
}

/// Reason a valid formula cannot be represented as static reference geometry.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum DefinedNameUnsupportedReason {
    /// The formula is a callable or general non-reference expression.
    NonReferenceExpression,
    /// The result needs a current cell, calculated value, or other runtime state.
    ContextDependent,
    /// The typed AST is valid but outside the current inspection resolver.
    UnsupportedExpression,
}

impl DefinedNameUnsupportedReason {
    /// Returns the stable transport spelling.
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::NonReferenceExpression => "non_reference_expression",
            Self::ContextDependent => "context_dependent",
            Self::UnsupportedExpression => "unsupported_expression",
        }
    }
}

/// Typed analysis of one workbook or sheet-local defined name.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum DefinedNameAnalysis {
    /// The name resolves to one rectangle on one worksheet.
    Rectangular {
        /// Stable worksheet identity.
        sheet_id: SheetId,
        /// Resolved A1 rectangle.
        range: CellRange,
    },
    /// The name resolves to one rectangle across a continuous worksheet span.
    ThreeDimensional {
        /// Stable workbook-order sheet span.
        sheet_span: DefinedNameSheetSpan,
        /// Resolved A1 rectangle shared by each sheet.
        range: CellRange,
    },
    /// The name resolves to multiple ordered areas.
    NonRectangular {
        /// Ordered areas with source multiplicity preserved.
        areas: Vec<DefinedNameReferenceArea>,
    },
    /// The name resolves to a valid empty reference such as a table without data rows.
    EmptyReference,
    /// The terminal reference shape depends on calculation state.
    DynamicFormula {
        /// Dynamic construct classification.
        kind: DefinedNameDynamicKind,
        /// Formula of the terminal dynamic definition.
        formula: FormulaText,
    },
    /// The terminal definition is dependency-free constant syntax.
    Constant {
        /// Formula of the terminal constant definition.
        formula: FormulaText,
    },
    /// The typed syntax addresses another workbook.
    ExternalReference {
        /// Typed external target detail.
        detail: DefinedNameExternalReference,
    },
    /// The selected name or a reachable value-name definition is invalid.
    Invalid {
        /// Stable invalidity reason.
        reason: DefinedNameInvalidReason,
        /// Optional source-specific context.
        detail: Option<Box<str>>,
    },
    /// The formula is valid but cannot be represented as static reference geometry.
    Unsupported {
        /// Stable unsupported reason.
        reason: DefinedNameUnsupportedReason,
        /// Optional source-specific context.
        detail: Option<Box<str>>,
    },
    /// No root name exists in the selected sheet-local/workbook lookup chain.
    NotFound,
}

/// Resource unit enforced by defined-name analysis.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum DefinedNameAnalysisLimitKind {
    /// Per-formula token count.
    FormulaTokens,
    /// Per-formula UTF-8 source bytes.
    FormulaSourceBytes,
    /// Per-formula AST nodes.
    FormulaAstNodes,
    /// Per-formula AST nesting depth.
    FormulaNestingDepth,
    /// Simultaneously active value-name definitions.
    NameChainDepth,
    /// Cumulative AST nodes scanned across reachable definitions.
    ScanNodes,
    /// Ordered areas retained by the analysis result.
    ReferenceAreas,
    /// Pairwise area operations used by intersection.
    FunctionIterations,
}

impl DefinedNameAnalysisLimitKind {
    /// Returns the stable transport spelling.
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::FormulaTokens => "formula_tokens",
            Self::FormulaSourceBytes => "formula_source_bytes",
            Self::FormulaAstNodes => "formula_ast_nodes",
            Self::FormulaNestingDepth => "formula_nesting_depth",
            Self::NameChainDepth => "name_chain_depth",
            Self::ScanNodes => "scan_nodes",
            Self::ReferenceAreas => "reference_areas",
            Self::FunctionIterations => "function_iterations",
        }
    }
}

/// Stable execution-failure category for a defined-name query.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum DefinedNameAnalysisErrorKind {
    /// The requested current sheet is absent.
    UnknownCurrentSheet,
    /// Analysis exceeded a configured resource limit.
    ResourceLimit,
    /// Cooperative cancellation was requested.
    Cancelled,
}

impl DefinedNameAnalysisErrorKind {
    /// Returns the stable dotted error code.
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::UnknownCurrentSheet => "defined_name.unknown_current_sheet",
            Self::ResourceLimit => "defined_name.resource_limit",
            Self::Cancelled => "defined_name.cancelled",
        }
    }
}

/// Execution failure returned separately from a semantic analysis result.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DefinedNameAnalysisError {
    kind: DefinedNameAnalysisErrorKind,
    limit: Option<DefinedNameAnalysisLimitKind>,
    detail: Option<Box<str>>,
}

impl DefinedNameAnalysisError {
    pub(super) fn unknown_sheet(sheet_id: SheetId) -> Self {
        Self {
            kind: DefinedNameAnalysisErrorKind::UnknownCurrentSheet,
            limit: None,
            detail: Some(sheet_id.get().to_string().into_boxed_str()),
        }
    }

    pub(super) fn resource(limit: DefinedNameAnalysisLimitKind) -> Self {
        Self {
            kind: DefinedNameAnalysisErrorKind::ResourceLimit,
            limit: Some(limit),
            detail: Some(limit.as_str().into()),
        }
    }

    pub(super) fn cancelled() -> Self {
        Self {
            kind: DefinedNameAnalysisErrorKind::Cancelled,
            limit: None,
            detail: None,
        }
    }

    /// Returns the stable execution-failure category.
    pub const fn kind(&self) -> DefinedNameAnalysisErrorKind {
        self.kind
    }

    /// Returns the exhausted resource unit, when applicable.
    pub const fn limit(&self) -> Option<DefinedNameAnalysisLimitKind> {
        self.limit
    }

    /// Returns source-specific context such as the unknown sheet ID.
    pub fn detail(&self) -> Option<&str> {
        self.detail.as_deref()
    }

    /// Returns the shared human-readable message.
    pub const fn message(&self) -> &'static str {
        match self.kind {
            DefinedNameAnalysisErrorKind::UnknownCurrentSheet => MESSAGE_UNKNOWN_SHEET,
            DefinedNameAnalysisErrorKind::ResourceLimit => MESSAGE_RESOURCE_LIMIT,
            DefinedNameAnalysisErrorKind::Cancelled => MESSAGE_CANCELLED,
        }
    }
}

impl fmt::Display for DefinedNameAnalysisError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.message())
    }
}

impl Error for DefinedNameAnalysisError {}

/// Bounded options for a defined-name inspection.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DefinedNameAnalysisOptions {
    calculation: CalculationOptions,
    max_name_chain_depth: u64,
    max_scan_nodes: u64,
}

impl DefinedNameAnalysisOptions {
    /// Creates options that reuse the supplied formula and reference limits.
    pub const fn new(calculation: CalculationOptions) -> Self {
        Self {
            calculation,
            max_name_chain_depth: 256,
            max_scan_nodes: 65_536,
        }
    }

    /// Replaces the maximum simultaneously active value-name chain depth.
    ///
    /// # Errors
    ///
    /// Returns [`DefinedNameAnalysisOptionsError`] when `value` is zero.
    pub fn with_max_name_chain_depth(
        mut self,
        value: u64,
    ) -> Result<Self, DefinedNameAnalysisOptionsError> {
        if value == 0 {
            return Err(DefinedNameAnalysisOptionsError);
        }
        self.max_name_chain_depth = value;
        Ok(self)
    }

    /// Replaces the cumulative reachable AST-node scan budget.
    ///
    /// # Errors
    ///
    /// Returns [`DefinedNameAnalysisOptionsError`] when `value` is zero.
    pub fn with_max_scan_nodes(
        mut self,
        value: u64,
    ) -> Result<Self, DefinedNameAnalysisOptionsError> {
        if value == 0 {
            return Err(DefinedNameAnalysisOptionsError);
        }
        self.max_scan_nodes = value;
        Ok(self)
    }

    /// Returns calculation/parser limits reused by the analyzer.
    pub const fn calculation(self) -> CalculationOptions {
        self.calculation
    }

    /// Returns the value-name chain depth limit.
    pub const fn max_name_chain_depth(self) -> u64 {
        self.max_name_chain_depth
    }

    /// Returns the cumulative reachable AST-node scan limit.
    pub const fn max_scan_nodes(self) -> u64 {
        self.max_scan_nodes
    }
}

impl Default for DefinedNameAnalysisOptions {
    fn default() -> Self {
        Self::new(CalculationOptions::default())
    }
}

/// Error returned when a defined-name analysis limit is zero.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DefinedNameAnalysisOptionsError;

impl fmt::Display for DefinedNameAnalysisOptionsError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(MESSAGE_ZERO_LIMIT)
    }
}

impl Error for DefinedNameAnalysisOptionsError {}

/// Analyzes one defined name with default bounded options.
///
/// # Errors
///
/// Returns a typed error for an unknown current sheet, exhausted resource budget, or cancellation.
pub fn analyze_defined_name(
    workbook: &WorkbookSnapshot,
    name: &str,
    current_sheet: Option<SheetId>,
) -> Result<DefinedNameAnalysis, DefinedNameAnalysisError> {
    analyzer::analyze(
        workbook,
        name,
        current_sheet,
        DefinedNameAnalysisOptions::default(),
        &|| false,
    )
}

/// Analyzes one defined name with explicit bounded options.
///
/// # Errors
///
/// Returns a typed error for an unknown current sheet or exhausted resource budget.
pub fn analyze_defined_name_with_options(
    workbook: &WorkbookSnapshot,
    name: &str,
    current_sheet: Option<SheetId>,
    options: DefinedNameAnalysisOptions,
) -> Result<DefinedNameAnalysis, DefinedNameAnalysisError> {
    analyzer::analyze(workbook, name, current_sheet, options, &|| false)
}

/// Analyzes one defined name with explicit options and cooperative cancellation.
///
/// # Errors
///
/// Returns a typed error for an unknown current sheet, exhausted resource budget, or cancellation.
pub fn analyze_defined_name_cancellable(
    workbook: &WorkbookSnapshot,
    name: &str,
    current_sheet: Option<SheetId>,
    options: DefinedNameAnalysisOptions,
    cancellation: &CancellationToken,
) -> Result<DefinedNameAnalysis, DefinedNameAnalysisError> {
    analyzer::analyze(workbook, name, current_sheet, options, &|| {
        cancellation.is_cancelled()
    })
}