From 8d3a0c6e903e5b844f724df9e55766b184b2a98c Mon Sep 17 00:00:00 2001 From: "William R. Arellano" Date: Tue, 5 Sep 2023 16:36:25 -0500 Subject: [PATCH 1/4] misc: add example file --- cassava-megaparsec.cabal | 10 ++++++++++ example/Main.hs | 4 ++++ 2 files changed, 14 insertions(+) create mode 100644 example/Main.hs diff --git a/cassava-megaparsec.cabal b/cassava-megaparsec.cabal index 4716215..fa7b8f1 100644 --- a/cassava-megaparsec.cabal +++ b/cassava-megaparsec.cabal @@ -55,6 +55,16 @@ 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-megaparsec + , text + 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..2a869f3 --- /dev/null +++ b/example/Main.hs @@ -0,0 +1,4 @@ +module Main where + +main :: IO () +main = putStrLn "This is a new example" From 10ca6dac8b940c572176c21648f3b8cb6ee12f9d Mon Sep 17 00:00:00 2001 From: "William R. Arellano" Date: Fri, 6 Oct 2023 16:26:28 -0500 Subject: [PATCH 2/4] misc: add example --- cassava-megaparsec.cabal | 2 ++ example/Main.hs | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/cassava-megaparsec.cabal b/cassava-megaparsec.cabal index fa7b8f1..b1048f7 100644 --- a/cassava-megaparsec.cabal +++ b/cassava-megaparsec.cabal @@ -61,8 +61,10 @@ executable cassava-megaparsec-example hs-source-dirs: example ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N build-depends: base + , cassava , cassava-megaparsec , text + , vector default-language: Haskell2010 test-suite tests diff --git a/example/Main.hs b/example/Main.hs index 2a869f3..3f5bdd7 100644 --- a/example/Main.hs +++ b/example/Main.hs @@ -1,4 +1,19 @@ +{-# LANGUAGE DeriveGeneric #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE TypeApplications #-} + module Main where +import Data.Csv hiding (decode, decodeByName, decodeByNameWith, decodeWith) +import Data.Csv.Parser.Megaparsec (decode, decodeByName, decodeByNameWith, decodeWith) +import Data.Vector (Vector) +import GHC.Generics (Generic) + +data Test = Test {a :: Char, b :: Char, c :: Char} deriving (Eq, Show, Generic) + +instance FromRecord Test + main :: IO () -main = putStrLn "This is a new example" +main = do + let res = decode @Test NoHeader "example" "a,ba,c\n" + print res From 00692223865c43fd956ccbd472918839ed81ca51 Mon Sep 17 00:00:00 2001 From: William Arellano Date: Wed, 4 Sep 2024 17:58:07 -0500 Subject: [PATCH 3/4] misc: add pretty print to error bundle of example --- cassava-megaparsec.cabal | 1 + example/Main.hs | 12 +++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/cassava-megaparsec.cabal b/cassava-megaparsec.cabal index b1048f7..fb56eb4 100644 --- a/cassava-megaparsec.cabal +++ b/cassava-megaparsec.cabal @@ -65,6 +65,7 @@ executable cassava-megaparsec-example , cassava-megaparsec , text , vector + , megaparsec default-language: Haskell2010 test-suite tests diff --git a/example/Main.hs b/example/Main.hs index 3f5bdd7..7d69504 100644 --- a/example/Main.hs +++ b/example/Main.hs @@ -4,10 +4,10 @@ module Main where -import Data.Csv hiding (decode, decodeByName, decodeByNameWith, decodeWith) -import Data.Csv.Parser.Megaparsec (decode, decodeByName, decodeByNameWith, decodeWith) -import Data.Vector (Vector) +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) @@ -15,5 +15,7 @@ instance FromRecord Test main :: IO () main = do - let res = decode @Test NoHeader "example" "a,ba,c\n" - print res + let res = decode @Test NoHeader "example.csv" "a,ba,c\n" + case res of + Left errorBundle -> putStrLn $ errorBundlePretty errorBundle + Right value -> print value From f1b30a17a9696d3c094c81df75e780cff2e41021 Mon Sep 17 00:00:00 2001 From: William Arellano Date: Tue, 17 Sep 2024 17:30:46 -0500 Subject: [PATCH 4/4] misc: add example explanation to README --- README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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)):