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:
@ -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}"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user