Debian Codegraph @

Data/Csv/Parser/Megaparsec.hs

raw symbols SCIP
1 -- |
2 -- Module : Data.Csv.Parser.Megaparsec
3 -- Copyright : © 2016–2021 Stack Builders
4 -- License : MIT
5 --
6 -- Maintainer : Mark Karpov <[email protected]>
7 -- Stability : experimental
8 -- Portability : portable
9 --
10 -- A CSV parser. The parser here is RFC 4180 compliant, with the following
11 -- extensions:
12 --
13 -- * Non-escaped fields may contain any characters except double-quotes,
14 -- commas (or generally delimiter characters), carriage returns, and
15 -- newlines.
16 -- * Escaped fields may contain any characters, but double-quotes need
17 -- to be escaped.
18 --
19 -- The parser provides better error messages than the parser that comes with
20 -- Cassava library, while being compatible with the rest of the library.
21
22 module Data.Csv.Parser.Megaparsec
23 ( ConversionError (..)
24 , decode
25 , decodeWith
26 , decodeByName
27 , decodeByNameWith)
28 where
29
30 import Data.Csv hiding
31 ( decode
32 , decodeWith
33 , decodeByName
34 , decodeByNameWith )
35 import Data.Vector (Vector)
36 import Text.Megaparsec
37 import qualified Data.ByteString.Lazy as BL
38 import Data.Csv.Parser.Megaparsec.Internals
39 ( ConversionError (..)
40 , csv
41 , csvWithHeader
42 , decodeWithC)
43
44 ----------------------------------------------------------------------------
45 -- Top level interface
46
47 -- | Deserialize CSV records form a lazy 'BL.ByteString'. If this fails due
48 -- to incomplete or invalid input, 'Left' is returned. Equivalent to
49 -- 'decodeWith' 'defaultDecodeOptions'.
50
51 decode :: FromRecord a
52 => HasHeader
53 -- ^ Whether the data contains header that should be skipped
54 -> FilePath
55 -- ^ File name (only for displaying in parse error messages, use empty
56 -- string if you have none)
57 -> BL.ByteString
58 -- ^ CSV data
59 -> Either (ParseErrorBundle BL.ByteString ConversionError) (Vector a)
60 decode = decodeWith defaultDecodeOptions
61 {-# INLINE decode #-}
62
63 -- | Like 'decode', but lets you customize how the CSV data is parsed.
64
65 decodeWith :: FromRecord a
66 => DecodeOptions
67 -- ^ Decoding options
68 -> HasHeader
69 -- ^ Whether the data contains header that should be skipped
70 -> FilePath
71 -- ^ File name (only for displaying in parse error messages, use empty
72 -- string if you have none)
73 -> BL.ByteString
74 -- ^ CSV data
75 -> Either (ParseErrorBundle BL.ByteString ConversionError) (Vector a)
76 decodeWith = decodeWithC csv
77 {-# INLINE decodeWith #-}
78
79 -- | Deserialize CSV records from a lazy 'BL.ByteString'. If this fails due
80 -- to incomplete or invalid input, 'Left' is returned. The data is assumed
81 -- to be preceded by a header. Equivalent to 'decodeByNameWith'
82 -- 'defaultDecodeOptions'.
83
84 decodeByName :: FromNamedRecord a
85 => FilePath
86 -- ^ File name (only for displaying in parse error messages, use empty
87 -- string if you have none)
88 -> BL.ByteString
89 -- ^ CSV data
90 -> Either (ParseErrorBundle BL.ByteString ConversionError) (Header, Vector a)
91 decodeByName = decodeByNameWith defaultDecodeOptions
92 {-# INLINE decodeByName #-}
93
94 -- | Like 'decodeByName', but lets you customize how the CSV data is parsed.
95
96 decodeByNameWith :: FromNamedRecord a
97 => DecodeOptions
98 -- ^ Decoding options
99 -> FilePath
100 -- ^ File name (only for displaying in parse error messages, use empty
101 -- string if you have none)
102 -> BL.ByteString
103 -- ^ CSV data
104 -> Either (ParseErrorBundle BL.ByteString ConversionError) (Header, Vector a)
105 decodeByNameWith opts = parse (csvWithHeader opts)
106 {-# INLINE decodeByNameWith #-}