3.0 KiB
3.0 KiB
Context
The rusty-claude-cli/src/main.rs file is currently a monolithic file containing over 9,200 lines of code. It handles everything from CLI argument parsing and session management to output rendering and error formatting. While we have successfully extracted the ~4,300 lines of test code into main_tests.rs, the remaining codebase is still too large for a single file, making maintenance, navigation, and parallel development difficult.
Goals / Non-Goals
Goals:
- Decompose the
main.rsfile into a modular structure aligned with Rust community best practices. - Improve code readability, maintainability, and compilation times.
- Ensure 100% of the existing test suite continues to pass without modification to the core logic.
Non-Goals:
- Do not add any new features or change existing business logic.
- Do not refactor external dependencies or change the CLI's public-facing behavior.
- Do not rewrite the logic inside functions—this is strictly a structural reorganization.
Decisions
- Incremental "Strangler Fig" Extraction Strategy:
- Rationale: Moving 9,200 lines at once is highly risky and guarantees import nightmares. Extracting one logical domain at a time (e.g., pure data structures first, then independent utility functions) minimizes blast radius and makes resolving
usestatements manageable.
- Rationale: Moving 9,200 lines at once is highly risky and guarantees import nightmares. Extracting one logical domain at a time (e.g., pure data structures first, then independent utility functions) minimizes blast radius and makes resolving
- Module Taxonomy:
models.rs/types.rs: Will house pure data structures likeModelSource,ModelProvenance,SessionLifecycleSummary, and API request/response schemas. These are the leaves of the dependency tree.error.rs: Will contain the global error enums,classify_error_kind, andsplit_error_hint.render.rs/ui.rs: Will handle output formatting, markdown rendering, and CLI visual components.repl.rs: Will encapsulate theLiveClistruct and therun_replinteractive loop.cli/: (Optional) Further split command handlers (e.g.,cli/resume.rs,cli/config.rs) if the command parsing logic remains too large.
- Keep
main.rsas a Thin Entrypoint:- Rationale:
main.rsshould only define the top-levelmoddeclarations, parse CLI arguments, invoke the appropriate runner from the submodules, and handle the finalstd::process::exitcodes.
- Rationale:
Risks / Trade-offs
- Risk: Import Resolution Chaos
- Trade-off: Moving structs to new files means
use crate::models::*will need to be added across many places. - Mitigation: Rely heavily on
cargo checkand the rust-analyzer LSP. We will extract the "leaves" of the dependency graph (pure models/errors) first before moving the "trunks" (the REPL loop).
- Trade-off: Moving structs to new files means
- Risk: Test Suite Breakage
- Trade-off:
main_tests.rscurrently relies heavily onuse crate::*to import private functions frommain.rs. Moving them to submodules might require making some previously private functionspub(crate). - Mitigation: Expose extracted functions as
pub(crate)within their new modules so thatmain.rsandmain_tests.rscan still access them.
- Trade-off: