tech-debt: main.rs is 17 300 lines and App has 119 fields #13

Open
opened 2026-11-16 14:26:50 +00:00 by ben · 0 comments
Owner

Version: 0.1.2 (main @ 41c7fc5)

This is a structural issue, not a defect: nothing behaves wrongly today.

The measurement

  • app/src/main.rs: 17 304 lines; the next largest file is data.rs with 5 913.
  • struct App (main.rs:223) has 119 fields and lives in that same file; its impl blocks span ~13 000 of those lines.
  • AGENTS.md already says "main.rs — the whole interface: panels, menus, shortcuts, drag and drop, and the glue between them", so this is known and deliberate. It has reached the size where it costs something.

What it costs today

  • Every change touches the same file. A patch that touches three panels conflicts with any other patch that touches a fourth; with branch protection and one-topic PRs that is a merge conflict per PR.
  • The seams that do exist are informal. App is document state, UI state, test hooks, export plumbing and pipeline state in one bag, so a new panel can read and write anything, and nothing says which fields belong to the document (and therefore to a snapshot, a save, or an undo step).
  • Tests pin the internals. uitest.rs drives real widgets (good) but also sets app.params.x and reads app.layer_pixels directly, because there is no document type to talk to.
  • The god object is why the undo bug exists. Snapshot has to name every field that is document state by hand (main.rs:3182), so layer_pixels was simply missed. A Document type with a snapshot() next to its fields makes that omission impossible to forget.

Suggested fix, in an order that keeps main.rs working throughout

  1. Move document state into a doc.rs (params, canvas, layer_pixels, shapes, ascii, effects, anim_layers, export_w/h) with impl Document { fn snapshot(&self) -> Snapshot }. That alone fixes the class of bug that layer_pixels fell into.
  2. Pull the panels into ui/ modules, one per tab (ui/dither.rs, ui/layers.rs, …), each an fn panel(&mut self, ui: &mut Ui) -> Changed style signature over Document + a small UiState.
  3. Leave main.rs with the frame loop, the menu bar, shortcuts and drag-and-drop — the parts that really are about the window.

No behaviour change anywhere; the --uitest rows keep clicking the same widgets.

A check that would catch a regression here

A cargo test row that walks the Document fields and asserts the snapshot round-trips them (the same trick this repo already uses for project save/load: build a document with everything non-default, snapshot, mutate, restore, compare). That turns "someone added a field and forgot the snapshot" from a data-loss bug into a red test.

Version: 0.1.2 (`main` @ 41c7fc5) This is a structural issue, not a defect: nothing behaves wrongly today. ## The measurement - `app/src/main.rs`: **17 304 lines**; the next largest file is `data.rs` with 5 913. - `struct App` (`main.rs:223`) has **119 fields** and lives in that same file; its `impl` blocks span ~13 000 of those lines. - `AGENTS.md` already says "`main.rs` — the whole interface: panels, menus, shortcuts, drag and drop, and the glue between them", so this is known and deliberate. It has reached the size where it costs something. ## What it costs today - **Every change touches the same file.** A patch that touches three panels conflicts with any other patch that touches a fourth; with branch protection and one-topic PRs that is a merge conflict per PR. - **The seams that do exist are informal.** `App` is document state, UI state, test hooks, export plumbing and pipeline state in one bag, so a new panel can read and write anything, and nothing says which fields belong to the document (and therefore to a snapshot, a save, or an undo step). - **Tests pin the internals.** `uitest.rs` drives real widgets (good) but also sets `app.params.x` and reads `app.layer_pixels` directly, because there is no document type to talk to. - **The god object is why the undo bug exists.** `Snapshot` has to name every field that is document state by hand (`main.rs:3182`), so `layer_pixels` was simply missed. A `Document` type with a `snapshot()` next to its fields makes that omission impossible to forget. ## Suggested fix, in an order that keeps `main.rs` working throughout 1. Move document state into a `doc.rs` (`params`, `canvas`, `layer_pixels`, `shapes`, `ascii`, `effects`, `anim_layers`, `export_w/h`) with `impl Document { fn snapshot(&self) -> Snapshot }`. That alone fixes the class of bug that `layer_pixels` fell into. 2. Pull the panels into `ui/` modules, one per tab (`ui/dither.rs`, `ui/layers.rs`, …), each an `fn panel(&mut self, ui: &mut Ui) -> Changed` style signature over `Document` + a small `UiState`. 3. Leave `main.rs` with the frame loop, the menu bar, shortcuts and drag-and-drop — the parts that really are about the window. No behaviour change anywhere; the `--uitest` rows keep clicking the same widgets. ## A check that would catch a regression here A `cargo test` row that walks the `Document` fields and asserts the snapshot round-trips them (the same trick this repo already uses for project save/load: build a document with everything non-default, snapshot, mutate, restore, compare). That turns "someone added a field and forgot the snapshot" from a data-loss bug into a red test.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
ben/openDither#13
No description provided.