Skip to content

Commit d02994e

Browse files
authored
chore: add example to README
* misc: add example file * misc: add example * misc: add pretty print to error bundle of example * misc: add example explanation to README
1 parent de949e4 commit d02994e

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,27 @@ To pretty print a error message use the `parseErrorPretty` function from
6161
This should be enough to start using the package, please consult Haddocks
6262
for detailed per-function documentation.
6363

64+
## Example
65+
66+
To check an example using `cassava-megaparsec` check the code in the `example` directory.
67+
68+
Given the following type:
69+
```haskell
70+
data Test = Test {a :: Char, b :: Char, c :: Char} deriving (Eq, Show, Generic)
71+
```
72+
And, the following csv file:
73+
```text
74+
a,ba,c
75+
```
76+
We get the following error:
77+
```
78+
example.csv:1:7:
79+
|
80+
1 | a,ba,c
81+
| ^
82+
conversion error: expected Char, got "ba"
83+
```
84+
6485
## Contributors ✨
6586

6687
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):

cassava-megaparsec.cabal

+13
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ library
5555
-Wnoncanonical-monadfail-instances
5656
default-language: Haskell2010
5757

58+
executable cassava-megaparsec-example
59+
main-is: Main.hs
60+
other-modules: Paths_cassava_megaparsec
61+
hs-source-dirs: example
62+
ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N
63+
build-depends: base
64+
, cassava
65+
, cassava-megaparsec
66+
, text
67+
, vector
68+
, megaparsec
69+
default-language: Haskell2010
70+
5871
test-suite tests
5972
main-is: Spec.hs
6073
hs-source-dirs: tests

example/Main.hs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{-# LANGUAGE DeriveGeneric #-}
2+
{-# LANGUAGE OverloadedStrings #-}
3+
{-# LANGUAGE TypeApplications #-}
4+
5+
module Main where
6+
7+
import Data.Csv (FromRecord, HasHeader (NoHeader))
8+
import Data.Csv.Parser.Megaparsec (decode)
9+
import GHC.Generics (Generic)
10+
import Text.Megaparsec.Error (errorBundlePretty)
11+
12+
data Test = Test {a :: Char, b :: Char, c :: Char} deriving (Eq, Show, Generic)
13+
14+
instance FromRecord Test
15+
16+
main :: IO ()
17+
main = do
18+
let res = decode @Test NoHeader "example.csv" "a,ba,c\n"
19+
case res of
20+
Left errorBundle -> putStrLn $ errorBundlePretty errorBundle
21+
Right value -> print value

0 commit comments

Comments
 (0)