refactor(command): simplify commit_cell_value by extracting helper functions

Simplify the commit_cell_value function by extracting its core logic into
specialized helper functions: commit_regular_cell_value, stage_drill_edit,
and commit_plain_records_edit. This improves readability and reduces
nesting.

Co-Authored-By: fiddlerwoaroof/git-smart-commit (gemma-4-31B-it-UD-Q4_K_XL.gguf)
This commit is contained in:
Edward Langley
2026-04-15 03:01:30 -07:00
parent 6f291ccd04
commit d8375ceaa7

View File

@ -139,22 +139,37 @@ mod tests {
/// Commit a cell value: for synthetic records keys, stage in drill pending edits /// Commit a cell value: for synthetic records keys, stage in drill pending edits
/// in drill mode, or apply directly in plain records mode; for real keys, write /// in drill mode, or apply directly in plain records mode; for real keys, write
/// to the model. /// to the model.
fn commit_cell_value( fn commit_regular_cell_value(key: &CellKey, value: &str, effects: &mut Vec<Box<dyn Effect>>) {
ctx: &CmdContext, if value.is_empty() {
key: &CellKey, effects.push(Box::new(effect::ClearCell(key.clone())));
value: &str, } else if let Ok(n) = value.parse::<f64>() {
effects: &mut Vec<Box<dyn Effect>>, effects.push(Box::new(effect::SetCell(key.clone(), CellValue::Number(n))));
) { } else {
if let Some((record_idx, col_name)) = crate::view::synthetic_record_info(key) { effects.push(Box::new(effect::SetCell(
if ctx.has_drill_state { key.clone(),
effects.push(Box::new(effect::SetDrillPendingEdit { CellValue::Text(value.to_string()),
)));
}
effects.push(effect::mark_dirty());
}
/// Stage a synthetic edit in drill state so it can be applied atomically on exit.
fn stage_drill_edit(record_idx: usize, col_name: String, value: &str) -> Box<dyn Effect> {
Box::new(effect::SetDrillPendingEdit {
record_idx, record_idx,
col_name, col_name,
new_value: value.to_string(), new_value: value.to_string(),
})); })
return;
} }
/// Apply a synthetic records-mode edit directly to the underlying model cell.
fn commit_plain_records_edit(
ctx: &CmdContext,
record_idx: usize,
col_name: &str,
value: &str,
effects: &mut Vec<Box<dyn Effect>>,
) {
let Some((orig_key, _)) = ctx let Some((orig_key, _)) = ctx
.layout .layout
.records .records
@ -165,25 +180,12 @@ fn commit_cell_value(
}; };
if col_name == "Value" { if col_name == "Value" {
if value.is_empty() { commit_regular_cell_value(orig_key, value, effects);
effects.push(Box::new(effect::ClearCell(orig_key.clone())));
} else if let Ok(n) = value.parse::<f64>() {
effects.push(Box::new(effect::SetCell(
orig_key.clone(),
CellValue::Number(n),
)));
} else {
effects.push(Box::new(effect::SetCell(
orig_key.clone(),
CellValue::Text(value.to_string()),
)));
}
effects.push(effect::mark_dirty());
return; return;
} }
if value.is_empty() { if value.is_empty() {
effects.push(effect::set_status("Record coordinates cannot be empty")); effects.push(effect::set_status(effect::RECORD_COORDS_CANNOT_BE_EMPTY));
return; return;
} }
@ -192,7 +194,7 @@ fn commit_cell_value(
}; };
effects.push(Box::new(effect::ClearCell(orig_key.clone()))); effects.push(Box::new(effect::ClearCell(orig_key.clone())));
effects.push(Box::new(effect::AddItem { effects.push(Box::new(effect::AddItem {
category: col_name.clone(), category: col_name.to_string(),
item: value.to_string(), item: value.to_string(),
})); }));
effects.push(Box::new(effect::SetCell( effects.push(Box::new(effect::SetCell(
@ -200,20 +202,24 @@ fn commit_cell_value(
existing_value, existing_value,
))); )));
effects.push(effect::mark_dirty()); effects.push(effect::mark_dirty());
return;
} else if value.is_empty() {
effects.push(Box::new(effect::ClearCell(key.clone())));
effects.push(effect::mark_dirty());
} else if let Ok(n) = value.parse::<f64>() {
effects.push(Box::new(effect::SetCell(key.clone(), CellValue::Number(n))));
effects.push(effect::mark_dirty());
} else {
effects.push(Box::new(effect::SetCell(
key.clone(),
CellValue::Text(value.to_string()),
)));
effects.push(effect::mark_dirty());
} }
fn commit_cell_value(
ctx: &CmdContext,
key: &CellKey,
value: &str,
effects: &mut Vec<Box<dyn Effect>>,
) {
if let Some((record_idx, col_name)) = crate::view::synthetic_record_info(key) {
if ctx.has_drill_state {
effects.push(stage_drill_edit(record_idx, col_name, value));
return;
}
commit_plain_records_edit(ctx, record_idx, &col_name, value, effects);
return;
}
commit_regular_cell_value(key, value, effects);
} }
/// Direction to advance after committing a cell edit. /// Direction to advance after committing a cell edit.