success_logo

Your form has successfully submitted

One of our teammates will get back to you soon.

Cassava Megaparsec library released

We have released a new open source library for CSV parsing that provides good error-messages and plays nicely with Cassava.


Many libraries in the Haskell ecosystem are designed for speed at the expense of other merits such as flexibility and quality of error messages. Examples of such libraries include:

There is nothing bad with this approach, but sometimes speed is just not that important and you wish you could provide user-friendly error messages or make your life easier by using a more flexible library instead.

Now CSV parsing in Haskell can be done with good error reporting with help of the cassava-megaparsec library. It replaces some built-in functions in Cassava:

  • decode
  • decodeWith
  • decodeByName
  • decodeByNameWith

The new functions work just the same as the old ones and play with the rest of Cassava library seamlessly, except in case of parser failure they return typed error message generated by Megaparsec. The error message can be inspected and rendered.

As an example of why you would want to use cassava-megaparsec in combination with cassava, here are some malformed CSV data and rendered error messages produced by the built-in decode function and the decode function from the cassava-megaparsec package:

Input (trying to parse (String, Maybe Int, Double)):

"foo

Vanilla Cassava:

parse error (Failed reading: conversion error: cannot unpack array of length 1 into a 3-tuple. Input record: ["fo"]) at ""

Cassava Megaparsec:

my-file.csv:1:5:
unexpected end of input
expecting '"', escaped double-quote, or unescaped character

Input:

foo,12,boo

Vanilla Cassava:

parse error (Failed reading: conversion error: expected Double, got "boo" (Failed reading: takeWhile1)) at ""

Cassava Megaparsec:

my-file.csv:1:11:
conversion error: expected Double, got "boo" (Failed reading: takeWhile1)

The quotes speak for themselves. Note how Cassava's type conversion is integrated right into Megaparsec parser.

Here are some guides on how to use the Cassava library:

To use cassava-megaparsec just import decode (and its friends if needed) Data.Csv.Parser.Megaparsec and add hiding clause to import Data.Csv to avoid name conflicts:

import Data.Csv hiding (decode, decodeWith, decodeByName, decodeByNameWith)
import Data.Csv.Parser.Megaparsec (decode, decodeWith, decodeByName, decodeByNameWith)

To use cassava-megaparsec with Stack, add it to extra-deps or use a recent nightly resolver.

Published on: Jun. 29, 2016

Written by:


Mark Karpov

Mark Karpov