fix: formula correctness and command validation bugs

- model.rs: division by zero now returns Empty instead of 0 so the cell
  visually signals the error rather than silently showing a wrong value.
  Updated the test to assert Empty.
- parser.rs: missing closing ')' in aggregate functions (SUM, AVG, etc.)
  and IF(...) now returns a parse error instead of silently succeeding,
  preventing malformed formulas from evaluating with unexpected results.
- dispatch.rs: SetCell validates that every category in the coords
  exists before mutating anything; previously a typo in a category name
  silently wrote an orphaned cell (stored but never visible in any grid)
  and returned ok. Now returns an error immediately.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Ed L
2026-03-23 23:32:14 -07:00
parent 6ba7245338
commit 0cb491901d
3 changed files with 31 additions and 9 deletions

View File

@ -36,12 +36,16 @@ pub fn dispatch(model: &mut Model, cmd: &Command) -> CommandResult {
let kv: Vec<(String, String)> = coords.iter()
.map(|pair| (pair[0].clone(), pair[1].clone()))
.collect();
// Ensure items exist
for (cat_name, item_name) in &kv {
if let Some(cat) = model.category_mut(cat_name) {
cat.add_item(item_name);
// Validate all categories exist before mutating anything
for (cat_name, _) in &kv {
if model.category(cat_name).is_none() {
return CommandResult::err(format!("Category '{cat_name}' not found"));
}
}
// Ensure items exist within their categories
for (cat_name, item_name) in &kv {
model.category_mut(cat_name).unwrap().add_item(item_name);
}
let key = CellKey::new(kv);
let cell_value = match value {
CellValueArg::Number { number } => CellValue::Number(*number),
@ -136,7 +140,8 @@ pub fn dispatch(model: &mut Model, cmd: &Command) -> CommandResult {
Command::Load { path } => {
match persistence::load(std::path::Path::new(path)) {
Ok(loaded) => {
Ok(mut loaded) => {
loaded.normalize_view_state();
*model = loaded;
CommandResult::ok_msg(format!("Loaded from {path}"))
}