This commit is contained in:
2025-09-10 15:54:52 +10:00
parent c41d5487a9
commit e17d66fed1
10 changed files with 349 additions and 244 deletions

View File

@@ -23,13 +23,8 @@ impl Grid {
&mut self,
cell_ref: CellRef,
raw_val: String,
do_propagation: bool,
force_propagation: bool,
) -> Result<Vec<CellRef>, String> {
if self.cells.contains_key(&cell_ref)
&& self.cells[&cell_ref].raw() == raw_val
&& !force_propagation
{
if self.cells.contains_key(&cell_ref) && self.cells[&cell_ref].raw() == raw_val {
return Ok(Vec::new());
}
@@ -48,14 +43,7 @@ impl Grid {
if self.cells.contains_key(&cell_ref) {
updated_cells = self
.update_exisiting_cell(
raw_val,
eval,
precs,
cell_ref,
do_propagation,
force_propagation,
)?
.update_exisiting_cell(raw_val, eval, precs, cell_ref)?
.into_iter()
.chain(updated_cells)
.collect();
@@ -66,6 +54,15 @@ impl Grid {
Ok(updated_cells)
}
pub fn quick_eval(&mut self, raw_val: String) -> Eval {
if raw_val.chars().nth(0) != Some('=') {
Eval::Literal(Literal::String(raw_val.to_owned()))
} else {
let (res_eval, ..) = evaluate(raw_val[1..].to_owned(), Some(&self));
res_eval
}
}
pub fn get_cell(&self, cell_ref: CellRef) -> Result<Cell, String> {
if !self.cells.contains_key(&cell_ref) {
return Err(format!("Cell at {:?} not found.", cell_ref));
@@ -168,8 +165,6 @@ impl Grid {
new_eval: Eval,
new_precs: HashSet<CellRef>,
cell_ref: CellRef,
do_propagation: bool,
force_propagation: bool,
) -> Result<Vec<CellRef>, String> {
let (old_precs, old_eval) = match self.cells.get_mut(&cell_ref) {
Some(cell) => {
@@ -211,7 +206,7 @@ impl Grid {
cell.set_precs(new_precs);
cell.set_eval(new_eval);
if (eval_changed && do_propagation) || force_propagation {
if eval_changed {
self.propagate(cell_ref)
} else {
Ok(Vec::new())

View File

@@ -62,16 +62,9 @@ async fn accept_connection(stream: TcpStream) {
MsgType::Set => {
let Some(cell_ref) = req.cell else { continue };
let Some(raw) = req.raw else { continue };
let Some(config) = req.eval_config else {
continue;
};
// let config = req.eval_config.unwrap_or_default();
match grid.update_cell(
cell_ref.clone(),
raw.to_owned(),
config.do_propagation,
config.force_propagation,
) {
match grid.update_cell(cell_ref.clone(), raw.to_owned()) {
Ok(updates) => {
let mut msgs = Vec::new();
@@ -122,6 +115,25 @@ async fn accept_connection(stream: TcpStream) {
}
}
}
MsgType::Eval => {
let Some(cell_ref) = req.cell else { continue };
let Some(raw) = req.raw else { continue };
let eval = grid.quick_eval(raw.to_owned());
let msg = LeadMsg {
msg_type: MsgType::Eval,
cell: Some(cell_ref),
raw: Some(raw),
eval: Some(eval),
bulk_msgs: None,
eval_config: None,
};
let _ = write
.send(serde_json::to_string(&msg).unwrap().into())
.await;
}
_ => {
continue; // handle other cases
}

View File

@@ -6,6 +6,7 @@ use crate::{cell::CellRef, evaluator::Eval};
#[serde(rename_all = "lowercase")]
pub enum MsgType {
Set,
Eval,
Get,
Error,
Bulk,
@@ -17,6 +18,15 @@ pub struct EvalConfig {
pub force_propagation: bool,
}
impl Default for EvalConfig {
fn default() -> Self {
EvalConfig {
do_propagation: true,
force_propagation: false,
}
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct LeadMsg {
pub msg_type: MsgType,