gridline_engine/engine/
cell.rs1use dashmap::DashMap;
10use serde::{Deserialize, Serialize};
11
12use super::cell_ref::CellRef;
13use super::deps::extract_dependencies;
14
15#[derive(Clone, Debug, Serialize, Deserialize)]
17pub enum CellType {
18 Empty,
19 Text(String),
20 Number(f64),
21 Script(String),
22}
23
24#[derive(Clone, Debug, Serialize, Deserialize)]
26pub struct Cell {
27 pub contents: CellType,
28 pub depends_on: Vec<CellRef>,
29 pub dirty: bool,
30 #[serde(skip)]
32 pub cached_value: Option<String>,
33}
34
35impl Cell {
36 pub fn new_empty() -> Cell {
37 Cell {
38 contents: CellType::Empty,
39 depends_on: vec![],
40 dirty: false,
41 cached_value: None,
42 }
43 }
44
45 pub fn new_text(text: &str) -> Cell {
46 Cell {
47 contents: CellType::Text(text.to_string()),
48 depends_on: vec![],
49 dirty: false,
50 cached_value: None,
51 }
52 }
53
54 pub fn new_number(n: f64) -> Cell {
55 Cell {
56 contents: CellType::Number(n),
57 depends_on: vec![],
58 dirty: false,
59 cached_value: None,
60 }
61 }
62
63 pub fn new_script(script: &str) -> Cell {
66 Cell {
67 depends_on: extract_dependencies(script),
68 contents: CellType::Script(script.to_string()),
69 dirty: true,
70 cached_value: None,
71 }
72 }
73
74 pub fn from_input(input: &str) -> Cell {
81 let trimmed = input.trim();
82 if trimmed.is_empty() {
83 return Cell::new_empty();
84 }
85
86 if let Some(formula) = trimmed.strip_prefix('=') {
87 return Cell::new_script(formula);
88 }
89
90 if trimmed.starts_with('"') && trimmed.ends_with('"') && trimmed.len() >= 2 {
91 let text = &trimmed[1..trimmed.len() - 1];
92 return Cell::new_text(text);
93 }
94
95 if let Ok(n) = trimmed.parse::<f64>() {
96 return Cell::new_number(n);
97 }
98
99 Cell::new_text(trimmed)
100 }
101
102 pub fn to_input_string(&self) -> String {
104 match &self.contents {
105 CellType::Empty => String::new(),
106 CellType::Text(s) => s.clone(),
107 CellType::Number(n) => n.to_string(),
108 CellType::Script(s) => format!("={}", s),
109 }
110 }
111}
112
113pub type Grid = DashMap<CellRef, Cell>;
115
116pub type SpillMap = DashMap<CellRef, rhai::Dynamic>;
119
120pub type ComputedMap = DashMap<CellRef, rhai::Dynamic>;