refactor(all): use let chains to flatten nested if statements

Replace nested if and if let blocks with combined if statements using let
chains. This reduces indentation and improves readability across the
project.

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-13 21:58:14 -07:00
parent 6370f8b19f
commit cb35e38df9
8 changed files with 32 additions and 48 deletions

View File

@ -167,11 +167,10 @@ fn draw(f: &mut Frame, app: &App) {
if matches!(app.mode, AppMode::Help) { if matches!(app.mode, AppMode::Help) {
f.render_widget(HelpWidget::new(app.help_page), size); f.render_widget(HelpWidget::new(app.help_page), size);
} }
if matches!(app.mode, AppMode::ImportWizard) { if matches!(app.mode, AppMode::ImportWizard)
if let Some(wizard) = &app.wizard { && let Some(wizard) = &app.wizard {
f.render_widget(ImportWizardWidget::new(wizard), size); f.render_widget(ImportWizardWidget::new(wizard), size);
} }
}
// ExportPrompt now uses the minibuffer at the bottom bar. // ExportPrompt now uses the minibuffer at the bottom bar.
if app.is_empty_model() && matches!(app.mode, AppMode::Normal | AppMode::CommandMode { .. }) { if app.is_empty_model() && matches!(app.mode, AppMode::Normal | AppMode::CommandMode { .. }) {
draw_welcome(f, main_chunks[1]); draw_welcome(f, main_chunks[1]);

View File

@ -149,13 +149,12 @@ impl ImportPipeline {
model.add_label_category(&lab.field)?; model.add_label_category(&lab.field)?;
} }
if !measures.is_empty() { if !measures.is_empty()
if let Some(cat) = model.category_mut("_Measure") { && let Some(cat) = model.category_mut("_Measure") {
for m in &measures { for m in &measures {
cat.add_item(&m.field); cat.add_item(&m.field);
} }
} }
}
for record in &self.records { for record in &self.records {
if let Value::Object(map) = record { if let Value::Object(map) = record {
@ -177,14 +176,13 @@ impl ImportPipeline {
// Extract date components from this field's value // Extract date components from this field's value
for (field, fmt, comp, derived_name) in &date_extractions { for (field, fmt, comp, derived_name) in &date_extractions {
if *field == cat_proposal.field { if *field == cat_proposal.field
if let Some(derived_val) = extract_date_component(&v, fmt, *comp) { && let Some(derived_val) = extract_date_component(&v, fmt, *comp) {
if let Some(cat) = model.category_mut(derived_name) { if let Some(cat) = model.category_mut(derived_name) {
cat.add_item(&derived_val); cat.add_item(&derived_val);
} }
coords.push((derived_name.clone(), derived_val)); coords.push((derived_name.clone(), derived_val));
} }
}
} }
} else { } else {
valid = false; valid = false;

View File

@ -184,11 +184,10 @@ impl Model {
// For non-_Measure target categories, add the target as a category item // For non-_Measure target categories, add the target as a category item
// so it appears in the grid. _Measure targets are dynamically included // so it appears in the grid. _Measure targets are dynamically included
// via measure_item_names(). // via measure_item_names().
if formula.target_category != "_Measure" { if formula.target_category != "_Measure"
if let Some(cat) = self.categories.get_mut(&formula.target_category) { && let Some(cat) = self.categories.get_mut(&formula.target_category) {
cat.add_item(&formula.target); cat.add_item(&formula.target);
} }
}
// Replace if same target within the same category // Replace if same target within the same category
if let Some(pos) = self.formulas.iter().position(|f| { if let Some(pos) = self.formulas.iter().position(|f| {
f.target == formula.target && f.target_category == formula.target_category f.target == formula.target && f.target_category == formula.target_category
@ -331,11 +330,10 @@ impl Model {
return Some(CellValue::Error("circular".into())); return Some(CellValue::Error("circular".into()));
} }
for formula in &self.formulas { for formula in &self.formulas {
if let Some(item_val) = key.get(&formula.target_category) { if let Some(item_val) = key.get(&formula.target_category)
if item_val == formula.target { && item_val == formula.target {
return self.eval_formula_depth(formula, key, depth - 1); return self.eval_formula_depth(formula, key, depth - 1);
} }
}
} }
self.data.get(key).cloned() self.data.get(key).cloned()
} }
@ -457,11 +455,10 @@ impl Model {
/// Uses raw data aggregation for non-formula refs and the cache for formula refs. /// Uses raw data aggregation for non-formula refs and the cache for formula refs.
fn evaluate_formula_cell(&self, key: &CellKey, none_cats: &[String]) -> Option<CellValue> { fn evaluate_formula_cell(&self, key: &CellKey, none_cats: &[String]) -> Option<CellValue> {
for formula in &self.formulas { for formula in &self.formulas {
if let Some(item_val) = key.get(&formula.target_category) { if let Some(item_val) = key.get(&formula.target_category)
if item_val == formula.target { && item_val == formula.target {
return self.eval_formula_with_cache(formula, key, none_cats); return self.eval_formula_with_cache(formula, key, none_cats);
} }
}
} }
None None
} }
@ -562,11 +559,10 @@ impl Model {
Expr::Agg(func, inner, agg_filter) => { Expr::Agg(func, inner, agg_filter) => {
use crate::formula::AggFunc; use crate::formula::AggFunc;
let mut partial = context.without(target_category); let mut partial = context.without(target_category);
if let Expr::Ref(item_name) = inner.as_ref() { if let Expr::Ref(item_name) = inner.as_ref()
if let Some(cat) = find_item_category(model, item_name) { && let Some(cat) = find_item_category(model, item_name) {
partial = partial.with(cat, item_name.as_str()); partial = partial.with(cat, item_name.as_str());
} }
}
if let Some(f) = agg_filter { if let Some(f) = agg_filter {
partial = partial.with(&f.category, &f.item); partial = partial.with(&f.category, &f.item);
} }
@ -756,11 +752,10 @@ impl Model {
Expr::UnaryMinus(e) => Ok(-eval_expr(e, context, model, target_category, depth)?), Expr::UnaryMinus(e) => Ok(-eval_expr(e, context, model, target_category, depth)?),
Expr::Agg(func, inner, agg_filter) => { Expr::Agg(func, inner, agg_filter) => {
let mut partial = context.without(target_category); let mut partial = context.without(target_category);
if let Expr::Ref(item_name) = inner.as_ref() { if let Expr::Ref(item_name) = inner.as_ref()
if let Some(cat) = find_item_category(model, item_name) { && let Some(cat) = find_item_category(model, item_name) {
partial = partial.with(cat, item_name.as_str()); partial = partial.with(cat, item_name.as_str());
} }
}
if let Some(f) = agg_filter { if let Some(f) = agg_filter {
partial = partial.with(&f.category, &f.item); partial = partial.with(&f.category, &f.item);
} }

View File

@ -170,12 +170,11 @@ pub fn format_md(model: &Model) -> String {
w!(out, "\n## View: {}", view.name); w!(out, "\n## View: {}", view.name);
for (cat, axis) in &view.category_axes { for (cat, axis) in &view.category_axes {
let qcat = quote_name(cat); let qcat = quote_name(cat);
if *axis == Axis::Page { if *axis == Axis::Page
if let Some(sel) = view.page_selections.get(cat) { && let Some(sel) = view.page_selections.get(cat) {
w!(out, "{qcat}: page, {}", quote_name(sel)); w!(out, "{qcat}: page, {}", quote_name(sel));
continue; continue;
} }
}
let axis_str = match axis { let axis_str = match axis {
Axis::Row => "row", Axis::Row => "row",
Axis::Column => "column", Axis::Column => "column",
@ -398,15 +397,14 @@ pub fn parse_md(text: &str) -> Result<Model> {
for fl in pair.into_inner() { for fl in pair.into_inner() {
if fl.as_rule() == Rule::formula_line { if fl.as_rule() == Rule::formula_line {
let raw = first_str(fl)?; let raw = first_str(fl)?;
if let Some(i) = raw.rfind(" [") { if let Some(i) = raw.rfind(" [")
if raw.ends_with(']') { && raw.ends_with(']') {
formulas.push(( formulas.push((
raw[..i].to_string(), raw[..i].to_string(),
raw[i + 2..raw.len() - 1].to_string(), raw[i + 2..raw.len() - 1].to_string(),
)); ));
continue; continue;
} }
}
// No [Category] suffix — default to _Measure // No [Category] suffix — default to _Measure
if !raw.is_empty() && raw.contains('=') { if !raw.is_empty() && raw.contains('=') {
formulas.push((raw, "_Measure".to_string())); formulas.push((raw, "_Measure".to_string()));
@ -476,11 +474,10 @@ pub fn parse_md(text: &str) -> Result<Model> {
_ => bail!("Unknown axis kind: {kind_str}"), _ => bail!("Unknown axis kind: {kind_str}"),
}; };
pv.axes.push((cat.clone(), axis)); pv.axes.push((cat.clone(), axis));
if axis == Axis::Page { if axis == Axis::Page
if let Some(sel_pair) = parts.next() { && let Some(sel_pair) = parts.next() {
pv.page_selections.push((cat, extract_name(sel_pair)?)); pv.page_selections.push((cat, extract_name(sel_pair)?));
} }
}
} }
Rule::format_line => pv.format = first_str(entry)?, Rule::format_line => pv.format = first_str(entry)?,
Rule::hidden_line => pv.hidden.push(extract_name_pair(entry)?), Rule::hidden_line => pv.hidden.push(extract_name_pair(entry)?),
@ -550,11 +547,10 @@ pub fn parse_md(text: &str) -> Result<Model> {
} }
} }
if let Some(iv) = &initial_view { if let Some(iv) = &initial_view
if m.views.contains_key(iv) { && m.views.contains_key(iv) {
m.active_view = iv.clone(); m.active_view = iv.clone();
} }
}
for (raw, cat_name) in &formulas { for (raw, cat_name) in &formulas {
m.add_formula(parse_formula(raw, cat_name).with_context(|| format!("Formula: {raw}"))?); m.add_formula(parse_formula(raw, cat_name).with_context(|| format!("Formula: {raw}"))?);

View File

@ -374,13 +374,12 @@ impl App {
} }
pub fn autosave_if_needed(&mut self) { pub fn autosave_if_needed(&mut self) {
if self.dirty && self.last_autosave.elapsed() > Duration::from_secs(30) { if self.dirty && self.last_autosave.elapsed() > Duration::from_secs(30)
if let Some(path) = &self.file_path.clone() { && let Some(path) = &self.file_path.clone() {
let ap = persistence::autosave_path(path); let ap = persistence::autosave_path(path);
let _ = persistence::save(&self.model, &ap); let _ = persistence::save(&self.model, &ap);
self.last_autosave = Instant::now(); self.last_autosave = Instant::now();
} }
}
} }
pub fn start_import_wizard(&mut self, json: serde_json::Value) { pub fn start_import_wizard(&mut self, json: serde_json::Value) {

View File

@ -36,8 +36,8 @@ pub fn build_cat_tree(model: &Model, expanded: &HashSet<String>) -> Vec<CatTreeE
item_count, item_count,
expanded: is_expanded, expanded: is_expanded,
}); });
if is_expanded { if is_expanded
if let Some(cat) = cat { && let Some(cat) = cat {
for item_name in cat.ordered_item_names() { for item_name in cat.ordered_item_names() {
entries.push(CatTreeEntry::Item { entries.push(CatTreeEntry::Item {
cat_name: cat_name.to_string(), cat_name: cat_name.to_string(),
@ -45,7 +45,6 @@ pub fn build_cat_tree(model: &Model, expanded: &HashSet<String>) -> Vec<CatTreeE
}); });
} }
} }
}
} }
entries entries
} }

View File

@ -85,11 +85,10 @@ impl Effect for AddFormula {
// For non-_Measure targets, add the item to the category so it // For non-_Measure targets, add the item to the category so it
// appears in the grid. _Measure targets are dynamically included // appears in the grid. _Measure targets are dynamically included
// via Model::measure_item_names(). // via Model::measure_item_names().
if formula.target_category != "_Measure" { if formula.target_category != "_Measure"
if let Some(cat) = app.model.category_mut(&formula.target_category) { && let Some(cat) = app.model.category_mut(&formula.target_category) {
cat.add_item(&formula.target); cat.add_item(&formula.target);
} }
}
app.model.add_formula(formula); app.model.add_formula(formula);
} }
Err(e) => { Err(e) => {

View File

@ -53,15 +53,14 @@ impl GridLayout {
frozen_records: Option<Rc<Vec<(CellKey, CellValue)>>>, frozen_records: Option<Rc<Vec<(CellKey, CellValue)>>>,
) -> Self { ) -> Self {
let mut layout = Self::new(model, view); let mut layout = Self::new(model, view);
if layout.is_records_mode() { if layout.is_records_mode()
if let Some(records) = frozen_records { && let Some(records) = frozen_records {
let row_items: Vec<AxisEntry> = (0..records.len()) let row_items: Vec<AxisEntry> = (0..records.len())
.map(|i| AxisEntry::DataItem(vec![i.to_string()])) .map(|i| AxisEntry::DataItem(vec![i.to_string()]))
.collect(); .collect();
layout.row_items = row_items; layout.row_items = row_items;
layout.records = Some(records); layout.records = Some(records);
} }
}
if view.prune_empty { if view.prune_empty {
layout.prune_empty(model); layout.prune_empty(model);
} }