Skip to content

Add example to showcase #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading