Skip to main content

formualizer_eval/engine/
named_range.rs

1use formualizer_parse::ASTNode;
2#[cfg(any(test, feature = "legacy_oracle"))]
3use rustc_hash::FxHashSet;
4
5use formualizer_common::LiteralValue;
6
7use crate::{CellRef, RangeRef, SheetId, engine::VertexId, reference::SharedRangeRef};
8
9/// Scope of a named range
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
11pub enum NameScope {
12    /// Available throughout workbook
13    Workbook,
14    /// Only available in specific sheet
15    Sheet(SheetId),
16}
17
18/// Definition of what a name refers to
19#[derive(Debug, Clone, PartialEq)]
20#[allow(clippy::large_enum_variant)]
21pub enum NamedDefinition {
22    /// Direct reference to a single cell
23    Cell(CellRef),
24    /// Reference to a range of cells
25    Range(RangeRef),
26    /// Constant literal value
27    Literal(LiteralValue),
28    /// Named formula (evaluates to value/range)
29    Formula {
30        ast: ASTNode,
31        /// Cached dependencies from last parse
32        dependencies: Vec<VertexId>,
33        /// Cached range dependencies
34        range_deps: Vec<SharedRangeRef<'static>>,
35    },
36}
37
38/// Complete named range entry
39#[derive(Debug, Clone)]
40pub struct NamedRange {
41    pub definition: NamedDefinition,
42    pub scope: NameScope,
43    /// Formulas that reference this name: legacy's name link map, kept
44    /// only by the test oracle (Program 1 M5). Normal builds have no public
45    /// equivalent: inspection (`Engine::dependents`) excludes name-mediated
46    /// readers, as legacy's inspection did.
47    #[cfg(any(test, feature = "legacy_oracle"))]
48    pub dependents: FxHashSet<VertexId>,
49    /// Vertex representing this named range within the dependency graph
50    pub vertex: VertexId,
51}
52
53/// Cloned, binding-friendly snapshot of a named range entry.
54#[derive(Debug, Clone, PartialEq)]
55pub struct NamedRangeSnapshot {
56    pub name: String,
57    pub scope: NameScope,
58    pub definition: NamedDefinition,
59}