https://github.com/stackbuilders/cassava-megaparsec
Megaparsec parser of CSV files that plays nicely with Cassava
https://github.com/stackbuilders/cassava-megaparsec
Last synced: 6 months ago
JSON representation
Megaparsec parser of CSV files that plays nicely with Cassava
- Host: GitHub
- URL: https://github.com/stackbuilders/cassava-megaparsec
- Owner: stackbuilders
- License: mit
- Created: 2016-06-20T13:51:55.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2024-11-22T21:59:14.000Z (about 1 year ago)
- Last Synced: 2025-06-14T07:05:45.953Z (7 months ago)
- Language: Haskell
- Homepage:
- Size: 64.5 KB
- Stars: 9
- Watchers: 16
- Forks: 5
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: docs/CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: docs/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Cassava Megaparsec
[](#contributors-)
[](http://opensource.org/licenses/MIT)
[](https://hackage.haskell.org/package/cassava-megaparsec)
[](http://stackage.org/nightly/package/cassava-megaparsec)
[](http://stackage.org/lts/package/cassava-megaparsec)

The package provides alternative parser for the
[Cassava](https://hackage.haskell.org/package/cassava) package written with
[Megaparsec](https://hackage.haskell.org/package/megaparsec) so you can get
better error messages at expense of some speed.
## Quick start
The package works seamlessly with Cassava by replacing the following
functions:
* `decode`
* `decodeWith`
* `decodeByName`
* `decodeByNameWith`
The functions work just the same as Cassava's equivalents, but also take
name of file they parse (to include into error messages) and return typed
high-quality error messages produced by
[Megaparsec](https://hackage.haskell.org/package/megaparsec).
The import section typically looks like this:
```haskell
import Data.Csv hiding (decode, decodeWith, decodeByName, decodeByNameWith)
import Data.Csv.Parser.Megaparsec (decode, decodeWith, decodeByName, decodeByNameWith)
```
Next you call appropriate function and get either result of parsing
identical to that of Cassava or error message. The error message is
well-typed so you can examine it in Haskell code easily. Conversion error
are considered parsing errors by the `cassava-megaparsec` package and are
reported via custom error message component `Cec` supported by Megaparsec 5.
Since Cassava's conversion errors are plain strings, we have no choice but
to represent them as strings too:
```haskell
-- | Custom error component for CSV parsing. It allows typed reporting of
-- conversion errors.
data Cec
= CecFail String
| CecIndentation Ordering Pos Pos
| CecConversionError String
deriving (Eq, Data, Typeable, Ord, Read, Show)
```
To pretty print a error message use the `parseErrorPretty` function from
`Text.Megaparsec`.
This should be enough to start using the package, please consult Haddocks
for detailed per-function documentation.
## Example
To check an example using `cassava-megaparsec` check the code in the `example` directory.
Given the following type:
```haskell
data Test = Test {a :: Char, b :: Char, c :: Char} deriving (Eq, Show, Generic)
```
And, the following csv file:
```text
a,ba,c
```
We get the following error:
```
example.csv:1:7:
|
1 | a,ba,c
| ^
conversion error: expected Char, got "ba"
```
## Contributors ✨
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):

Mark Karpov
💻

William R. Arellano
💻

Cristhian Motoche
💻

Andrés Martínez
💻

ˌbodʲɪˈɡrʲim
💻

Felix Yan
💻

Jorge Guerra Landázuri
💻

Justin S. Leitgeb
💻
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
## License
MIT, see [the LICENSE file](LICENSE).
## Contributing
Do you want to contribute to this project? Please take a look at our [contributing guideline](/docs/CONTRIBUTING.md) to know how you can help us build it.
---
[Check out our libraries](https://github.com/stackbuilders/) | [Join our team](https://www.stackbuilders.com/join-us/)