Cassava Megaparsec library released
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:
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
Input (trying to parse
(String, Maybe Int, Double)):
parse error (Failed reading: conversion error: cannot unpack array of length 1 into a 3-tuple. Input record: ["fo"]) at ""
my-file.csv:1:5: unexpected end of input expecting '"', escaped double-quote, or unescaped character
parse error (Failed reading: conversion error: expected Double, got "boo" (Failed reading: takeWhile1)) at ""
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:
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)
cassava-megaparsec with Stack, add it to
extra-deps or use a recent nightly resolver.