Skip to content

Commit

Permalink
chore: add example to README
Browse files Browse the repository at this point in the history
* misc: add example file

* misc: add example

* misc: add pretty print to error bundle of example

* misc: add example explanation to README
  • Loading branch information
cptrodolfox authored Sep 17, 2024
1 parent de949e4 commit d02994e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,27 @@ To pretty print a error message use the `parseErrorPretty` function from
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)):
Expand Down
13 changes: 13 additions & 0 deletions cassava-megaparsec.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ library
-Wnoncanonical-monadfail-instances
default-language: Haskell2010

executable cassava-megaparsec-example
main-is: Main.hs
other-modules: Paths_cassava_megaparsec
hs-source-dirs: example
ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N
build-depends: base
, cassava
, cassava-megaparsec
, text
, vector
, megaparsec
default-language: Haskell2010

test-suite tests
main-is: Spec.hs
hs-source-dirs: tests
Expand Down
21 changes: 21 additions & 0 deletions example/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}

module Main where

import Data.Csv (FromRecord, HasHeader (NoHeader))
import Data.Csv.Parser.Megaparsec (decode)
import GHC.Generics (Generic)
import Text.Megaparsec.Error (errorBundlePretty)

data Test = Test {a :: Char, b :: Char, c :: Char} deriving (Eq, Show, Generic)

instance FromRecord Test

main :: IO ()
main = do
let res = decode @Test NoHeader "example.csv" "a,ba,c\n"
case res of
Left errorBundle -> putStrLn $ errorBundlePretty errorBundle
Right value -> print value

0 comments on commit d02994e

Please sign in to comment.