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 validate → 3 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)
CodingErrorfields:line: usizeandsource: serde_json::Error, derived withthiserrorand 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 emptyc2_anchor_quote.- CLI:
--coded <PathBuf>; success printsOK: {n} coded rows valid against the codebookto 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)
- 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. - Predict: the bad-enum test feeds
"c2_logic":"AGGRESSIVE". Where exactly does it fail — inload_coded'sserde_json::from_str, or incheck_anchors? Trace the path before running. - Run, check.
- Implement, then exercise the CLI on a deliberately bad file and confirm a non-zero exit.
- Run green, commit.
Done when
cargo test -p panoptes-coding validate passes and panoptes-code --coded bad.jsonl exits non-zero naming the offending line.