Something went wrong. Try again.
Declarative CSV codecs
Something went wrong. Try again.
Files
- OCaml 100%
README.md
# nox-csv
Typed CSV codecs for OCaml, with streaming I/O and located errors.
The usual way to read a CSV file into records is to split lines by hand, look
columns up by index and raise an exception on a bad value, with no row or
column to point at; writing the records back means doing it all again the
other way round. `nox-csv` has you describe a row once, as a codec in the
style of [jsont](https://erratique.ch/software/jsont), and uses that
description to decode a file into records and to encode records into a
file. The codec's column names are matched against the header once per file,
so decoding a row needs no further lookup.
The streaming functions use [bytesrw](https://erratique.ch/software/bytesrw):
`of_reader` reads from a `Bytes.Reader.t` and `to_writer` writes to a
`Bytes.Writer.t`, which lets CSV sit in a pipeline with other bytesrw codecs.
`of_file` and `of_string` return the decoded rows, while `fold_file` and
`fold_reader` go through them without building the list.
A decode error carries a source location from
[nox-loc](https://tangled.org/gazagnaire.org/ocaml-loc) and a path to the
value that failed, giving the row by index and the column by name.
## Contents
- [Installation](#installation)
- [Usage](#usage)
- [API](#api)
- [References](#references)
- [License](#license)
## Installation
Install with opam:
<!-- $MDX skip -->
```sh
$ opam install nox-csv
```
If opam cannot find the package, it may not yet be released in the public
`opam-repository`. Add the overlay repository, then install it:
<!-- $MDX skip -->
```sh
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install nox-csv
```
The package installs the library `nox-csv`, whose module is `Csv`. It needs
OCaml 5.1 or later. The package `nox-csv-eio` installs the library of the
same name, whose module `Csv_eio` reads, folds and writes the files an
`Eio.Path.t` names, keeping the core free of Eio.
## Usage
```ocaml
type point = { x : float; y : float; label : string }
let point_codec =
…