Csv

Config & IO

Fast CSV parsing for the BEAM (NimbleCSV-shaped).

Hot path is Erlang binary matching. Parsed fields may be binary references into the current row buffer — copy with care if you retain fields across rows or send them to another process.

Prefer large byte chunks from the source (Server.bodyStream 65536, Multipart.fileStream, or File.streamBytes).

Server.bodyStream 65536 req
    |> Csv.decode Csv.rfc4180
    |> Stream.stoppableFoldl validate 0

Consume HTTP body streams inside handle with tryFoldl / stoppableFoldl, not attemptFoldl.

Types

Options

type Options = Options

Parser options (separator, escape, header skipping).

Examples

Csv.rfc4180

Row

type alias Row =
List String

One CSV record as field strings.

Examples

[ "Ada", "Lovelace" ]

Error

type Error
= ParseFailed Int String
| InvalidRow Int String

Parse or validation failure with a 1-based row number.

Examples

Csv.invalidRow 2 "bad email"

Values

rfc4180

rfc4180 : Options

RFC 4180 defaults: comma separator, double-quote escape, skip header row.

Examples

Csv.rfc4180

tsv

tsv : Options

Tab-separated values with the same escape / header defaults as rfc4180.

Examples

Csv.tsv

withSkipHeaders

withSkipHeaders : Bool -> Options -> Options

Set whether the first row is treated as headers and dropped.

Examples

Csv.withSkipHeaders False Csv.rfc4180

errorToString

errorToString : Error -> String

Human-readable Error.

Examples

Csv.errorToString err

invalidRow

invalidRow : Int -> String -> Error

Build an InvalidRow for application validation failures.

Examples

Csv.invalidRow 2 "bad email"

decode

decode : Options -> Stream Bytes -> Stream (Result Error Row)

Lazily decode CSV records from byte chunks (quoted multiline fields OK).

Examples

Csv.decode Csv.rfc4180 (Server.bodyStream 65536 req)

decodeLines

decodeLines : Options -> Stream String -> Stream (Result Error Row)

Lazily decode from a line-oriented stream (NimbleCSV parse_stream shape). Lines that open a quote continue until the escape closes.

Examples

Csv.decodeLines Csv.rfc4180 (File.stream path)

decodeString

decodeString : Options -> String -> Result Error (List Row)

Eagerly decode a whole CSV document.

Examples

Csv.decodeString Csv.rfc4180 "a,b\n1,2\n"