Skip to main content

formualizer_eval/engine/
named_range.rs

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