|
| 1 | +# Rust Modularization Plan (Top 3 Largest Files) |
| 2 | + |
| 3 | +This document proposes a safe, incremental modularization plan for the three largest Rust source files in this repository. |
| 4 | + |
| 5 | +## Scope and line counts |
| 6 | + |
| 7 | +Measured with: |
| 8 | + |
| 9 | +```bash |
| 10 | +rg --files -g '*.rs' | xargs wc -l | sort -nr | head |
| 11 | +``` |
| 12 | + |
| 13 | +Largest files: |
| 14 | + |
| 15 | +1. `packages/cli-rust/src/commands/update.rs` (~2162 lines) |
| 16 | +2. `packages/core/src/docker/image.rs` (~1845 lines) |
| 17 | +3. `packages/cli-rust/src/commands/start.rs` (~1791 lines) |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +## 1) `packages/cli-rust/src/commands/update.rs` |
| 22 | + |
| 23 | +### Problem shape |
| 24 | + |
| 25 | +`update.rs` currently mixes: |
| 26 | + |
| 27 | +- argument orchestration and command flow |
| 28 | +- image/version decision logic |
| 29 | +- container update/rollback logic |
| 30 | +- runtime status and messaging |
| 31 | +- helper structs/utilities and tests |
| 32 | + |
| 33 | +This makes review/debugging harder and increases coupling between unrelated concerns. |
| 34 | + |
| 35 | +### Proposed module split |
| 36 | + |
| 37 | +Keep `update.rs` as the command entry/root and move logic into: |
| 38 | + |
| 39 | +- `commands/update/args.rs` |
| 40 | + - CLI option parsing helpers and argument validation |
| 41 | +- `commands/update/image_resolution.rs` |
| 42 | + - image/tag selection, version candidate resolution, compatibility checks |
| 43 | +- `commands/update/container_update.rs` |
| 44 | + - update execution flow, candidate container creation, handoff/rollback operations |
| 45 | +- `commands/update/rollback.rs` |
| 46 | + - rollback-specific pathways and error recovery policy |
| 47 | +- `commands/update/output.rs` |
| 48 | + - user-facing progress messages + summary formatting |
| 49 | +- `commands/update/tests/` (or inline `#[cfg(test)]` submodules) |
| 50 | + - focused test files by domain (resolution/update/rollback) |
| 51 | + |
| 52 | +### Migration order |
| 53 | + |
| 54 | +1. Extract pure helpers first (`image_resolution`), no behavior changes. |
| 55 | +2. Extract container update/rollback paths. |
| 56 | +3. Extract output formatting and final orchestration cleanup. |
| 57 | +4. Split tests last to preserve confidence throughout. |
| 58 | + |
| 59 | +--- |
| 60 | + |
| 61 | +## 2) `packages/core/src/docker/image.rs` |
| 62 | + |
| 63 | +### Problem shape |
| 64 | + |
| 65 | +`image.rs` currently combines: |
| 66 | + |
| 67 | +- pull/build/tag primitives |
| 68 | +- local cache/discovery logic |
| 69 | +- provenance/metadata handling |
| 70 | +- progress and stream parsing |
| 71 | +- fallback behavior and registry differences |
| 72 | + |
| 73 | +This inflates cognitive load and makes subtle behavior regressions easier to introduce. |
| 74 | + |
| 75 | +### Proposed module split |
| 76 | + |
| 77 | +Keep `image.rs` as public façade/re-export module and split internals into: |
| 78 | + |
| 79 | +- `docker/image/pull.rs` |
| 80 | + - pull flows, pull-source fallbacks, post-pull retag policy |
| 81 | +- `docker/image/build.rs` |
| 82 | + - build flows, build args, build output handling |
| 83 | +- `docker/image/tag.rs` |
| 84 | + - retagging, canonical naming, migration helpers |
| 85 | +- `docker/image/inspect.rs` |
| 86 | + - image existence/inspection/local lookup helpers |
| 87 | +- `docker/image/provenance.rs` |
| 88 | + - provenance labels and extraction utilities |
| 89 | +- `docker/image/progress.rs` |
| 90 | + - stream/progress translation utilities (if not already covered in `docker/progress.rs`) |
| 91 | + |
| 92 | +### Migration order |
| 93 | + |
| 94 | +1. Move inspect/tag helpers (lowest risk, often pure). |
| 95 | +2. Move pull path with comprehensive tests. |
| 96 | +3. Move build/provenance paths. |
| 97 | +4. Keep `image.rs` as thin API surface delegating to submodules. |
| 98 | + |
| 99 | +--- |
| 100 | + |
| 101 | +## 3) `packages/cli-rust/src/commands/start.rs` |
| 102 | + |
| 103 | +### Problem shape |
| 104 | + |
| 105 | +`start.rs` currently blends: |
| 106 | + |
| 107 | +- preflight checks and environment detection |
| 108 | +- image selection/pull/build decisions |
| 109 | +- mount and runtime composition |
| 110 | +- prompt/interaction UX branches |
| 111 | +- startup orchestration and post-start reporting |
| 112 | + |
| 113 | +Large control-flow depth makes changes risky and slow to reason about. |
| 114 | + |
| 115 | +### Proposed module split |
| 116 | + |
| 117 | +Keep `start.rs` as orchestration root and split into: |
| 118 | + |
| 119 | +- `commands/start/preflight.rs` |
| 120 | + - host checks, dependency checks, config validation |
| 121 | +- `commands/start/image_strategy.rs` |
| 122 | + - local-vs-remote image decision logic, fallback rules |
| 123 | +- `commands/start/mounts.rs` |
| 124 | + - mount derivation, validation, normalization |
| 125 | +- `commands/start/interaction.rs` |
| 126 | + - prompts/confirmations and non-interactive defaults |
| 127 | +- `commands/start/runtime.rs` |
| 128 | + - container launch/runtime start sequence |
| 129 | +- `commands/start/output.rs` |
| 130 | + - user messaging/status summary |
| 131 | + |
| 132 | +### Migration order |
| 133 | + |
| 134 | +1. Extract preflight + mount normalization. |
| 135 | +2. Extract image strategy. |
| 136 | +3. Extract interaction prompts. |
| 137 | +4. Extract runtime launch flow and reduce `start.rs` to top-level pipeline. |
| 138 | + |
| 139 | +--- |
| 140 | + |
| 141 | +## Cross-cutting standards for all three refactors |
| 142 | + |
| 143 | +- Keep behavior stable: **refactor-only** PRs per module extraction. |
| 144 | +- Prefer early returns/guard clauses to reduce nesting. |
| 145 | +- Avoid `unwrap()`; propagate errors with context. |
| 146 | +- Keep each commit small and reversible. |
| 147 | +- Maintain tests for each extracted concern before moving to next. |
| 148 | + |
| 149 | +## Suggested PR sequence |
| 150 | + |
| 151 | +1. `update.rs` extraction (resolution + rollback first) |
| 152 | +2. `image.rs` extraction (inspect/tag first) |
| 153 | +3. `start.rs` extraction (preflight/image strategy first) |
| 154 | + |
| 155 | +This order starts with highest line-count and highest complexity hotspots while keeping runtime risk manageable. |
0 commit comments