Stack Builders logo
User Icon
Mark Karpov
Jun. 29, 2016
Dec. 20, 2024
2 mins read
Subscribe to blog
Email
Check More Blogs
software development industry

hapistrano's new features

Hapistrano's new features
Open Source Parsing Libraries
User Icon
Mark Karpov
4 mins read
software development vs programming

ghc generics explained

GHC Generics Explained
Haskell Tutorial Functional Programming GHC
User Icon
Mark Karpov
19 mins read
software development fundamentals

ghc optimization and fusion

GHC optimization and fusion
Haskell Tutorial GHC
User Icon
Mark Karpov
62 mins read
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.

Check More Blogs
software development industry

hapistrano's new features

Hapistrano's new features
Open Source Parsing Libraries
User Icon
Mark Karpov
4 mins read
software development vs programming

ghc generics explained

GHC Generics Explained
Haskell Tutorial Functional Programming GHC
User Icon
Mark Karpov
19 mins read
software development fundamentals

ghc optimization and fusion

GHC optimization and fusion
Haskell Tutorial GHC
User Icon
Mark Karpov
62 mins read
Subscribe to blog
Email