← LeaderboardThe Skill Leaderboard
rust-csv-logical-records
Fix Rust CSV imports that split quoted records across physical lines. Parse the original stream with the csv crate and report record positions on validation errors.
Try your first task
A source-grounded starter task has not been verified for this skill yet.
SKILL.md
name: rust-csv-logical-recordsdescription: Fix Rust CSV imports that split quoted records across physical lines. Parse the original stream with the csv crate and report record positions on validation errors.category: developmentrisk: criticalsource: selfsource_type: selfdate_added: "2026-12-02"author: ShianMiketags: [rust, csv, parsing, data-import]Rust CSV Logical Records
When to Use
- A Rust import silently drops part of a quoted field containing a line break.
- Commas or doubled quotes inside fields appear as separators or literal quote characters.
- A malformed record diagnostic uses a data-row counter where the user needs a physical line number.
Prerequisites
- A representative input in its original encoding and line ending format.
- The producer's delimiter, quote, header, and field-count policy.
- A fixture with quoted multiline data and a deliberate later field-count error.
1. Give the CSV reader the original stream
- Find uses of
BufRead::lines,str::lines, and manual separator splitting before CSV decoding. - Pass the original reader into
csv::ReaderBuilder::from_readerso the parser can carry quoting state across line breaks. - Configure delimiter, header handling, quote behavior, and record terminators from the agreed dialect. Preserve line breaks that belong inside quoted fields.
- Use
StringRecordwhen input must be valid UTF-8. ChooseByteRecordwhen the application must validate or convert the source encoding separately.
2. Validate the decoded records
- Check the expected header and field lengths where the import contract defines them. The reader's default length check can be kept for fixed-width rows.
- Use
flexible(true)only when variable field counts belong to the dialect. Apply any required downstream validation explicitly. - Preserve the reader's byte, line, and record coordinates in raw diagnostics. The record index is zero-based and counts the header. Verify where the raw offset lands before showing an editor location. A CSV position can point to the LF of the preceding CRLF.
- Propagate parsing and validation errors to the import boundary. For a whole-file guarantee, stage records and publish only after validation succeeds.
3. Test the original input shape
- Assert the exact values of fields containing a line break, delimiter, and doubled quote.
- Assert a record after a multiline field retains its position and value.
- Add a record with too few fields. Assert the error variant, expected and actual field lengths, and raw position against the source bytes.
- When the dialect requires stricter grammar checks, test the required rejections separately. The csv crate accepts a range of nonstandard quoting.
Example
Run Read quoted multiline CSV. It reproduces data loss in physical-line splitting, verifies the decoded fields, and locates a later field-count error.
Limitations
- The fixture uses a small UTF-8 stream with a fixed two-column schema. Encoding detection, very large records, and alternate dialects require separate validation.
- A successful parse is one step in validation. Application-level type checks and import commit behavior belong to the caller.
Safety and Side Effects
- Run the example in a scratch Cargo project. It reads synthetic in-memory data and writes local build artifacts.
- Verify with disposable inputs and retain source coordinates when changing an importer that writes stored data.