Skip to content

refactor: eliminate code duplication and simplify core patterns#147

Open
vmvarela wants to merge 1 commit intomasterfrom
refactor/clean-code-patterns
Open

refactor: eliminate code duplication and simplify core patterns#147
vmvarela wants to merge 1 commit intomasterfrom
refactor/clean-code-patterns

Conversation

@vmvarela
Copy link
Copy Markdown
Owner

@vmvarela vmvarela commented May 8, 2026

Summary

  • fatal() deduplicated: the 4 identical copies in main.zig, modes/columns.zig, modes/validate.zig, and modes/sample.zig removed — all now import the pub fn from sqlite.zig.
  • Error switch in main() collapsed: from ~100 lines with 19 writeAll → flush → exit blocks down to 22 lines using fatal() directly (it is noreturn).
  • parse() simplified in format.zig: chains of if (mem.eql(...)) replaced by std.meta.stringToEnum — automatically stays in sync with the enum.
  • Transactions in loader.zig: inline sqlite3_exec("BEGIN"/"COMMIT") replaced by the beginTransaction/commitTransaction helpers from sqlite.zig.
  • JSON reading fixed: while (takeByte()) loop in columns.zig and validate.zig replaced by allocRemaining(.unlimited).
  • Tests cleaned up: 24 unnecessary @as(usize, N) casts removed in csv.zig.

Impact

No behaviour change. Pure refactoring.

7 files changed, 66 insertions(+), 245 deletions(-)

Checklist

  • zig build test — all tests pass
  • ziglint src build.zig — clean

- Remove 4 identical copies of fatal() — all files now import the pub
  fn from sqlite.zig
- Collapse 19-case error switch in main() from ~100 lines to 22 using
  fatal() directly (it is noreturn)
- Replace if-chain parse() in InputFormat/OutputFormat with
  std.meta.stringToEnum — stays in sync with enum variants automatically
- Use beginTransaction/commitTransaction helpers in loader.zig instead
  of duplicating the inline sqlite3_exec logic
- Replace byte-by-byte takeByte() JSON reading in columns.zig and
  validate.zig with allocRemaining(.unlimited)
- Remove redundant @as(usize, N) casts in csv.zig unit tests

No behaviour change. All tests pass, ziglint clean.
@vmvarela vmvarela added type:chore Maintenance, refactoring, tooling priority:medium Should be done soon size:s Small — 1 to 4 hours tech-debt Technical debt — address proactively labels May 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority:medium Should be done soon size:s Small — 1 to 4 hours tech-debt Technical debt — address proactively type:chore Maintenance, refactoring, tooling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant