diff --git a/README.md b/README.md index 45f6b85..9ac8657 100644 --- a/README.md +++ b/README.md @@ -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)): diff --git a/cassava-megaparsec.cabal b/cassava-megaparsec.cabal index 4716215..fb56eb4 100644 --- a/cassava-megaparsec.cabal +++ b/cassava-megaparsec.cabal @@ -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 diff --git a/example/Main.hs b/example/Main.hs new file mode 100644 index 0000000..7d69504 --- /dev/null +++ b/example/Main.hs @@ -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