Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Build: The Coded-CSV Loader

Maps to: Task 11. Kind: Build.

Objective

Write validate.rs: load_coded, which parses each coded line into a CodedRow — so an off-codebook value fails here, at the boundary — and check_anchors, enforcing the codebook rule that latent codes carry an anchor quote. Add the panoptes-code CLI. This is where the enums-as-validation payoff becomes a runnable command.

Scaffold

Create: crates/panoptes-coding/src/validate.rs and src/main.rs. Modify: crates/panoptes-coding/Cargo.toml (add [[bin]] name = "panoptes-code" path = "src/main.rs") and src/lib.rs (re-export load_coded, check_anchors, CodingError).

Dependencies: thiserror (the CodingError derive with line-number context) — already in the manifest if you followed the previous chapter's Scaffold note.

Expected result: cargo test -p panoptes-coding validate3 tests pass; then cargo run -p panoptes-coding -- --coded /tmp/bad.jsonl (a file with "c2_logic":"AGGRESSIVE") exits non-zero, naming line 1.

The spec (givens)

  • CodingError fields: line: usize and source: serde_json::Error, derived with thiserror and the display format "row {line}: {source}".
  • load_coded(contents: &str) -> Result<Vec<CodedRow>, CodingError> — skips blank lines, line numbers are 1-indexed.
  • check_anchors(rows: &[CodedRow]) -> Result<(), String> — errors with "{response_id}: C2 logic coded without an anchor quote" on the first empty c2_anchor_quote.
  • CLI: --coded <PathBuf>; success prints OK: {n} coded rows valid against the codebook to stderr; any failure exits non-zero.

Full code: Answer Key, Task 11.

Concepts exercised

  • Parse-is-validation in practice: deserialization as the gate.
  • Custom error types (thiserror) carrying line-number context.
  • A CLI that exits non-zero on invalid data.

The build loop (you drive)

  1. Write failing tests: a valid row loads; a bad-enum row fails with the right line number; a missing anchor quote is caught by check_anchors.
  2. Predict: the bad-enum test feeds "c2_logic":"AGGRESSIVE". Where exactly does it fail — in load_coded's serde_json::from_str, or in check_anchors? Trace the path before running.
  3. Run, check.
  4. Implement, then exercise the CLI on a deliberately bad file and confirm a non-zero exit.
  5. Run green, commit.
The payoff, realized This is the chapter the whole course pointed at. The codebook constraint you learned about abstractly in Part II is now a command that rejects invalid coded data at the boundary, with a line number, before it can pollute analysis. Parse is validation, running in a terminal.

Done when

cargo test -p panoptes-coding validate passes and panoptes-code --coded bad.jsonl exits non-zero naming the offending line.