Skip to content

refactor: reduce complexity of main in src/main.rs#382

Merged
jamesadevine merged 1 commit into
mainfrom
refactor/reduce-main-complexity-8c98c27e1bb010df
May 1, 2026
Merged

refactor: reduce complexity of main in src/main.rs#382
jamesadevine merged 1 commit into
mainfrom
refactor/reduce-main-complexity-8c98c27e1bb010df

Conversation

@github-actions

@github-actions github-actions Bot commented May 1, 2026

Copy link
Copy Markdown
Contributor

What was complex

main() in src/main.rs was 224 lines with a heuristic complexity score of 44 — the highest in the codebase. The root cause was the Execute command arm, which packed ~120 lines of deeply-nested logic directly inside a match arm:

  • reading and parsing the source markdown
  • building the allowed-repositories map (an explicit for + if let loop)
  • constructing ExecutionContext field-by-field with multiple if let branches
  • loading OTel agent stats with nested if otel_path.exists() / match / if let
  • executing safe outputs
  • processing cache-memory with a further if let Some(cm) / if cm.is_enabled() nest
  • computing and printing the summary
  • std::process::exit with two separate exit-code branches

The top-level if let Some(command) = args.command { match command { ... } } else { ... } also added unnecessary nesting depth.

What changed

Four focused helper functions extracted:

Helper Responsibility
run_compile Handles --skip-integrity / --debug-pipeline warnings and dispatch to compile_pipeline / compile_all_pipelines
run_execute Thin orchestrator: parse → context → run → memory → summary → exit
build_execution_context Constructs ExecutionContext from FrontMatter + CLI overrides + OTel stats
process_cache_memory Checks cache-memory tool config and runs memory artifact processing
print_execution_summary Formats and prints the final success/warning/failure counts

The top-level if let ... else was replaced with let Some(command) = args.command else { ... } + an unconditional match, removing one level of nesting.

Before / After

Metric Before After
main lines 224 ~60
Heuristic complexity score 44 ~12
Clippy cognitive-complexity warnings 0 (threshold ≥ 25) 0

Verification

  • cargo build — clean (0 errors, 0 new warnings)
  • cargo test — all tests pass
  • cargo clippy --all-targets --all-features — no new warnings
  • Clippy cognitive-complexity lint — no warnings at default threshold

Generated by Cyclomatic Complexity Reducer · ● 2.6M ·

Extract the Execute command arm (~120 lines of nested logic) into
dedicated helper functions:

- run_compile: handles Compile arm flags/dispatch
- run_execute: orchestrates the Execute pipeline (parse → context → run → memory → summary)
- build_execution_context: reads front matter into ExecutionContext, including OTel stats
- process_cache_memory: checks cache-memory tool config and processes memory artifacts
- print_execution_summary: formats and prints the final results table

Also flatten the top-level if-let/match in main to let-else + match,
reducing nesting depth.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jamesadevine jamesadevine marked this pull request as ready for review May 1, 2026 21:43
@jamesadevine jamesadevine merged commit 1790b9b into main May 1, 2026
@jamesadevine jamesadevine deleted the refactor/reduce-main-complexity-8c98c27e1bb010df branch May 1, 2026 21:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant