/usr/share/doc/libghc-cassava-doc/html/cassava.txt is in libghc-cassava-doc 0.4.4.0-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 | -- Hoogle documentation, generated by Haddock
-- See Hoogle, http://www.haskell.org/hoogle/
-- | A CSV parsing and encoding library
--
-- A CSV parsing and encoding library optimized for ease of use and high
-- performance.
@package cassava
@version 0.4.4.0
-- | A CSV parser. The parser defined here is RFC 4180 compliant, with the
-- following extensions:
--
-- <ul>
-- <li>Empty lines are ignored.</li>
-- <li>Non-escaped fields may contain any characters except
-- double-quotes, commas, carriage returns, and newlines.</li>
-- <li>Escaped fields may contain any characters (but double-quotes need
-- to be escaped).</li>
-- </ul>
--
-- The functions in this module can be used to implement e.g. a resumable
-- parser that is fed input incrementally.
module Data.Csv.Parser
-- | Options that controls how data is decoded. These options can be used
-- to e.g. decode tab-separated data instead of comma-separated data.
--
-- To avoid having your program stop compiling when new fields are added
-- to <a>DecodeOptions</a>, create option records by overriding values in
-- <a>defaultDecodeOptions</a>. Example:
--
-- <pre>
-- myOptions = defaultDecodeOptions {
-- decDelimiter = fromIntegral (ord '\t')
-- }
-- </pre>
data DecodeOptions
DecodeOptions :: {-# UNPACK #-} !Word8 -> DecodeOptions
-- | Field delimiter.
[decDelimiter] :: DecodeOptions -> {-# UNPACK #-} !Word8
-- | Decoding options for parsing CSV files.
defaultDecodeOptions :: DecodeOptions
-- | Parse a CSV file that does not include a header.
csv :: DecodeOptions -> Parser Csv
-- | Parse a CSV file that includes a header.
csvWithHeader :: DecodeOptions -> Parser (Header, Vector NamedRecord)
-- | Parse a header, including the terminating line separator.
header :: Word8 -> Parser Header
-- | Parse a record, not including the terminating line separator. The
-- terminating line separate is not included as the last record in a CSV
-- file is allowed to not have a terminating line separator. You most
-- likely want to use the <a>endOfLine</a> parser in combination with
-- this parser.
record :: Word8 -> Parser Record
-- | Parse a header name. Header names have the same format as regular
-- <a>field</a>s.
name :: Word8 -> Parser Name
-- | Parse a field. The field may be in either the escaped or non-escaped
-- format. The return value is unescaped.
field :: Word8 -> Parser Field
instance GHC.Show.Show Data.Csv.Parser.DecodeOptions
instance GHC.Classes.Eq Data.Csv.Parser.DecodeOptions
-- | Low-level bytestring builders. Most users want to use the more
-- type-safe <a>Incremental</a> module instead.
module Data.Csv.Builder
-- | Encode a header.
encodeHeader :: Header -> Builder
-- | Encode a single record.
encodeRecord :: ToRecord a => a -> Builder
-- | Encode a single named record, given the field order.
encodeNamedRecord :: ToNamedRecord a => Header -> a -> Builder
-- | Encode a single named record, using the default field order.
encodeDefaultOrderedNamedRecord :: (DefaultOrdered a, ToNamedRecord a) => a -> Builder
-- | Like <a>encodeHeader</a>, but lets you customize how the CSV data is
-- encoded.
encodeHeaderWith :: EncodeOptions -> Header -> Builder
-- | Like <a>encodeRecord</a>, but lets you customize how the CSV data is
-- encoded.
encodeRecordWith :: ToRecord a => EncodeOptions -> a -> Builder
-- | Like <a>encodeNamedRecord</a>, but lets you customize how the CSV data
-- is encoded.
encodeNamedRecordWith :: ToNamedRecord a => EncodeOptions -> Header -> a -> Builder
-- | Like <a>encodeDefaultOrderedNamedRecord</a>, but lets you customize
-- how the CSV data is encoded.
encodeDefaultOrderedNamedRecordWith :: (DefaultOrdered a, ToNamedRecord a) => EncodeOptions -> a -> Builder
-- | This module allows for incremental decoding and encoding of CSV data.
-- This is useful if you e.g. want to interleave I/O with parsing or if
-- you want finer grained control over how you deal with type conversion
-- errors.
--
-- Decoding example:
--
-- <pre>
-- main :: IO ()
-- main = withFile "salaries.csv" ReadMode $ \ csvFile -> do
-- let loop !_ (Fail _ errMsg) = putStrLn errMsg >> exitFailure
-- loop acc (Many rs k) = loop (acc + sumSalaries rs) =<< feed k
-- loop acc (Done rs) = putStrLn $ "Total salaries: " ++
-- show (sumSalaries rs + acc)
--
-- feed k = do
-- isEof <- hIsEOF csvFile
-- if isEof
-- then return $ k B.empty
-- else k `fmap` B.hGetSome csvFile 4096
-- loop 0 (decode NoHeader)
-- where
-- sumSalaries rs = sum [salary | Right (_ :: String, salary :: Int) <- rs]
-- </pre>
--
-- Encoding example:
--
-- <pre>
-- data Person = Person { name :: !String, salary :: !Int }
-- deriving Generic
--
-- instance FromNamedRecord Person
-- instance ToNamedRecord Person
-- instance DefaultOrdered Person
--
-- persons :: [Person]
-- persons = [Person "John" 50000, Person "Jane" 60000]
--
-- main :: IO ()
-- main = putStrLn $ encodeDefaultOrderedByName (go persons)
-- where
-- go (x:xs) = encodeNamedRecord x <> go xs
-- </pre>
module Data.Csv.Incremental
-- | An incremental parser that when fed data eventually returns a parsed
-- <a>Header</a>, or an error.
data HeaderParser a
-- | The input data was malformed. The first field contains any unconsumed
-- input and second field contains information about the parse error.
FailH :: !ByteString -> String -> HeaderParser a
-- | The parser needs more input data before it can produce a result. Use
-- an <a>empty</a> string to indicate that no more input data is
-- available. If fed an 'B.empty string', the continuation is guaranteed
-- to return either <a>FailH</a> or <a>DoneH</a>.
PartialH :: (ByteString -> HeaderParser a) -> HeaderParser a
-- | The parse succeeded and produced the given <a>Header</a>.
DoneH :: !Header -> a -> HeaderParser a
-- | Parse a CSV header in an incremental fashion. When done, the
-- <a>HeaderParser</a> returns any unconsumed input in the second field
-- of the <a>DoneH</a> constructor.
decodeHeader :: HeaderParser ByteString
-- | Like <a>decodeHeader</a>, but lets you customize how the CSV data is
-- parsed.
decodeHeaderWith :: DecodeOptions -> HeaderParser ByteString
-- | An incremental parser that when fed data eventually produces some
-- parsed records, converted to the desired type, or an error in case of
-- malformed input data.
data Parser a
-- | The input data was malformed. The first field contains any unconsumed
-- input and second field contains information about the parse error.
Fail :: !ByteString -> String -> Parser a
-- | The parser parsed and converted zero or more records. Any records that
-- failed type conversion are returned as <tt><a>Left</a> errMsg</tt> and
-- the rest as <tt><a>Right</a> val</tt>. Feed a <a>ByteString</a> to the
-- continuation to continue parsing. Use an <a>empty</a> string to
-- indicate that no more input data is available. If fed an <a>empty</a>
-- string, the continuation is guaranteed to return either <a>Fail</a> or
-- <a>Done</a>.
Many :: [Either String a] -> (ByteString -> Parser a) -> Parser a
-- | The parser parsed and converted some records. Any records that failed
-- type conversion are returned as <tt><a>Left</a> errMsg</tt> and the
-- rest as <tt><a>Right</a> val</tt>.
Done :: [Either String a] -> Parser a
-- | Is the CSV data preceded by a header?
data HasHeader
-- | The CSV data is preceded by a header
HasHeader :: HasHeader
-- | The CSV data is not preceded by a header
NoHeader :: HasHeader
-- | Efficiently deserialize CSV in an incremental fashion. Equivalent to
-- <tt><a>decodeWith</a> <a>defaultDecodeOptions</a></tt>.
decode :: FromRecord a => HasHeader -> Parser a
-- | Like <a>decode</a>, but lets you customize how the CSV data is parsed.
decodeWith :: FromRecord a => DecodeOptions -> HasHeader -> Parser a
-- | Efficiently deserialize CSV in an incremental fashion. The data is
-- assumed to be preceeded by a header. Returns a <a>HeaderParser</a>
-- that when done produces a <a>Parser</a> for parsing the actual
-- records. Equivalent to <tt><a>decodeByNameWith</a>
-- <a>defaultDecodeOptions</a></tt>.
decodeByName :: FromNamedRecord a => HeaderParser (Parser a)
-- | Like <a>decodeByName</a>, but lets you customize how the CSV data is
-- parsed.
decodeByNameWith :: FromNamedRecord a => DecodeOptions -> HeaderParser (Parser a)
-- | Efficiently serialize records in an incremental fashion. Equivalent to
-- <tt><a>encodeWith</a> <tt>defaultEncodeOptions</tt></tt>.
encode :: ToRecord a => Builder a -> ByteString
-- | Like <a>encode</a>, but lets you customize how the CSV data is
-- encoded.
encodeWith :: ToRecord a => EncodeOptions -> Builder a -> ByteString
-- | Encode a single record.
encodeRecord :: ToRecord a => a -> Builder a
-- | A builder for building the CSV data incrementally. Just like the
-- <tt>ByteString</tt> builder, this builder should be used in a
-- right-associative, <a>foldr</a> style. Using <a><></a> to
-- compose builders in a left-associative, <tt>foldl'</tt> style makes
-- the building not be incremental.
data Builder a
-- | Efficiently serialize named records in an incremental fashion,
-- including the leading header. Equivalent to <tt><a>encodeWith</a>
-- <tt>defaultEncodeOptions</tt></tt>. The header is written before any
-- records and dictates the field order.
encodeByName :: ToNamedRecord a => Header -> NamedBuilder a -> ByteString
-- | Like <a>encodeByName</a>, but header and field order is dictated by
-- the <a>headerOrder</a> method.
encodeDefaultOrderedByName :: (DefaultOrdered a, ToNamedRecord a) => NamedBuilder a -> ByteString
-- | Like <a>encodeByName</a>, but lets you customize how the CSV data is
-- encoded.
encodeByNameWith :: ToNamedRecord a => EncodeOptions -> Header -> NamedBuilder a -> ByteString
-- | Like <a>encodeDefaultOrderedByName</a>, but lets you customize how the
-- CSV data is encoded.
encodeDefaultOrderedByNameWith :: (DefaultOrdered a, ToNamedRecord a) => EncodeOptions -> NamedBuilder a -> ByteString
-- | Encode a single named record.
encodeNamedRecord :: ToNamedRecord a => a -> NamedBuilder a
-- | A builder for building the CSV data incrementally. Just like the
-- <tt>ByteString</tt> builder, this builder should be used in a
-- right-associative, <a>foldr</a> style. Using <a><></a> to
-- compose builders in a left-associative, <tt>foldl'</tt> style makes
-- the building not be incremental.
data NamedBuilder a
instance GHC.Show.Show Data.Csv.Incremental.More
instance GHC.Classes.Eq Data.Csv.Incremental.More
instance GHC.Base.Functor Data.Csv.Incremental.Parser
instance GHC.Base.Functor Data.Csv.Incremental.HeaderParser
instance GHC.Show.Show a => GHC.Show.Show (Data.Csv.Incremental.HeaderParser a)
instance GHC.Show.Show a => GHC.Show.Show (Data.Csv.Incremental.Parser a)
instance GHC.Base.Monoid (Data.Csv.Incremental.Builder a)
instance GHC.Base.Monoid (Data.Csv.Incremental.NamedBuilder a)
-- | This module allows for streaming decoding of CSV data. This is useful
-- if you need to parse large amounts of input in constant space. The API
-- also allows you to ignore type conversion errors on a per-record
-- basis.
module Data.Csv.Streaming
-- | A stream of parsed records. If type conversion failed for the record,
-- the error is returned as <tt><a>Left</a> errMsg</tt>.
data Records a
-- | A record or an error message, followed by more records.
Cons :: (Either String a) -> (Records a) -> Records a
-- | End of stream, potentially due to a parse error. If a parse error
-- occured, the first field contains the error message. The second field
-- contains any unconsumed input.
Nil :: (Maybe String) -> ByteString -> Records a
-- | Is the CSV data preceded by a header?
data HasHeader
-- | The CSV data is preceded by a header
HasHeader :: HasHeader
-- | The CSV data is not preceded by a header
NoHeader :: HasHeader
-- | Efficiently deserialize CSV records in a streaming fashion. Equivalent
-- to <tt><a>decodeWith</a> <a>defaultDecodeOptions</a></tt>.
decode :: FromRecord a => HasHeader -> ByteString -> Records a
-- | Like <a>decode</a>, but lets you customize how the CSV data is parsed.
decodeWith :: FromRecord a => DecodeOptions -> HasHeader -> ByteString -> Records a
-- | Efficiently deserialize CSV in a streaming fashion. The data is
-- assumed to be preceeded by a header. Returns <tt><a>Left</a>
-- errMsg</tt> if parsing the header fails. Equivalent to
-- <tt><a>decodeByNameWith</a> <a>defaultDecodeOptions</a></tt>.
decodeByName :: FromNamedRecord a => ByteString -> Either String (Header, Records a)
-- | Like <a>decodeByName</a>, but lets you customize how the CSV data is
-- parsed.
decodeByNameWith :: FromNamedRecord a => DecodeOptions -> ByteString -> Either String (Header, Records a)
instance GHC.Show.Show a => GHC.Show.Show (Data.Csv.Streaming.Records a)
instance GHC.Base.Functor Data.Csv.Streaming.Records
instance GHC.Classes.Eq a => GHC.Classes.Eq (Data.Csv.Streaming.Records a)
instance Data.Foldable.Foldable Data.Csv.Streaming.Records
instance Data.Traversable.Traversable Data.Csv.Streaming.Records
instance Control.DeepSeq.NFData a => Control.DeepSeq.NFData (Data.Csv.Streaming.Records a)
-- | This module implements encoding and decoding of CSV data. The
-- implementation is RFC 4180 compliant, with the following extensions:
--
-- <ul>
-- <li>Empty lines are ignored.</li>
-- <li>Non-escaped fields may contain any characters except
-- double-quotes, commas, carriage returns, and newlines.</li>
-- <li>Escaped fields may contain any characters (but double-quotes need
-- to be escaped).</li>
-- </ul>
module Data.Csv
-- | Is the CSV data preceded by a header?
data HasHeader
-- | The CSV data is preceded by a header
HasHeader :: HasHeader
-- | The CSV data is not preceded by a header
NoHeader :: HasHeader
-- | Efficiently deserialize CSV records from a lazy <a>ByteString</a>. If
-- this fails due to incomplete or invalid input, <tt><a>Left</a>
-- msg</tt> is returned. Equivalent to <tt><a>decodeWith</a>
-- <a>defaultDecodeOptions</a></tt>.
decode :: FromRecord a => HasHeader -> ByteString -> Either String (Vector a)
-- | Efficiently deserialize CSV records from a lazy <a>ByteString</a>. If
-- this fails due to incomplete or invalid input, <tt><a>Left</a>
-- msg</tt> is returned. The data is assumed to be preceeded by a header.
-- Equivalent to <tt><a>decodeByNameWith</a>
-- <a>defaultDecodeOptions</a></tt>.
decodeByName :: FromNamedRecord a => ByteString -> Either String (Header, Vector a)
-- | Should quoting be applied to fields, and at which level
data Quoting
-- | No quotes
QuoteNone :: Quoting
-- | Quotes according to RFC 4180
QuoteMinimal :: Quoting
-- | Always quote
QuoteAll :: Quoting
-- | Efficiently serialize CSV records as a lazy <a>ByteString</a>.
encode :: ToRecord a => [a] -> ByteString
-- | Efficiently serialize CSV records as a lazy <a>ByteString</a>. The
-- header is written before any records and dictates the field order.
encodeByName :: ToNamedRecord a => Header -> [a] -> ByteString
-- | Like <a>encodeByName</a>, but header and field order is dictated by
-- the <a>header</a> method.
encodeDefaultOrderedByName :: (DefaultOrdered a, ToNamedRecord a) => [a] -> ByteString
-- | A type that has a default field order when converted to CSV. This
-- class lets you specify how to get the headers to use for a record type
-- that's an instance of <a>ToNamedRecord</a>.
--
-- To derive an instance, the type is required to only have one
-- constructor and that constructor must have named fields (also known as
-- selectors) for all fields.
--
-- Right: <tt>data Foo = Foo { foo :: !Int }</tt>
--
-- Wrong: <tt>data Bar = Bar Int</tt>
--
-- If you try to derive an instance using GHC generics and your type
-- doesn't have named fields, you will get an error along the lines of:
--
-- <pre>
-- <interactive>:9:10:
-- No instance for (DefaultOrdered (M1 S NoSelector (K1 R Char) ()))
-- arising from a use of ‘Data.Csv.Conversion.$gdmheader’
-- In the expression: Data.Csv.Conversion.$gdmheader
-- In an equation for ‘header’:
-- header = Data.Csv.Conversion.$gdmheader
-- In the instance declaration for ‘DefaultOrdered Foo’
-- </pre>
class DefaultOrdered a where headerOrder = fromList . gtoNamedRecordHeader . from
-- | The header order for this record. Should include the names used in the
-- <a>NamedRecord</a> returned by <a>toNamedRecord</a>. Pass
-- <a>undefined</a> as the argument, together with a type annotation e.g.
-- <tt><a>headerOrder</a> (<a>undefined</a> :: MyRecord)</tt>.
headerOrder :: DefaultOrdered a => a -> Header
-- | Options that controls how data is decoded. These options can be used
-- to e.g. decode tab-separated data instead of comma-separated data.
--
-- To avoid having your program stop compiling when new fields are added
-- to <a>DecodeOptions</a>, create option records by overriding values in
-- <a>defaultDecodeOptions</a>. Example:
--
-- <pre>
-- myOptions = defaultDecodeOptions {
-- decDelimiter = fromIntegral (ord '\t')
-- }
-- </pre>
data DecodeOptions
DecodeOptions :: {-# UNPACK #-} !Word8 -> DecodeOptions
-- | Field delimiter.
[decDelimiter] :: DecodeOptions -> {-# UNPACK #-} !Word8
-- | Decoding options for parsing CSV files.
defaultDecodeOptions :: DecodeOptions
-- | Like <a>decode</a>, but lets you customize how the CSV data is parsed.
decodeWith :: FromRecord a => DecodeOptions -> HasHeader -> ByteString -> Either String (Vector a)
-- | Like <a>decodeByName</a>, but lets you customize how the CSV data is
-- parsed.
decodeByNameWith :: FromNamedRecord a => DecodeOptions -> ByteString -> Either String (Header, Vector a)
-- | Options that controls how data is encoded. These options can be used
-- to e.g. encode data in a tab-separated format instead of in a
-- comma-separated format.
--
-- To avoid having your program stop compiling when new fields are added
-- to <a>EncodeOptions</a>, create option records by overriding values in
-- <a>defaultEncodeOptions</a>. Example:
--
-- <pre>
-- myOptions = defaultEncodeOptions {
-- encDelimiter = fromIntegral (ord '\t')
-- }
-- </pre>
--
-- <i>N.B.</i> The <a>encDelimiter</a> must <i>not</i> be the quote
-- character (i.e. <tt>"</tt>) or one of the record separator characters
-- (i.e. <tt>\n</tt> or <tt>\r</tt>).
data EncodeOptions
EncodeOptions :: {-# UNPACK #-} !Word8 -> !Bool -> !Bool -> !Quoting -> EncodeOptions
-- | Field delimiter.
[encDelimiter] :: EncodeOptions -> {-# UNPACK #-} !Word8
-- | Record separator selection. <tt>True</tt> for CRLF (<tt>\r\n</tt>) and
-- <tt>False</tt> for LF (<tt>\n</tt>).
[encUseCrLf] :: EncodeOptions -> !Bool
-- | Include a header row when encoding <tt>ToNamedRecord</tt> instances.
[encIncludeHeader] :: EncodeOptions -> !Bool
-- | What kind of quoting should be applied to text fields.
[encQuoting] :: EncodeOptions -> !Quoting
-- | Encoding options for CSV files.
defaultEncodeOptions :: EncodeOptions
-- | Like <a>encode</a>, but lets you customize how the CSV data is
-- encoded.
encodeWith :: ToRecord a => EncodeOptions -> [a] -> ByteString
-- | Like <a>encodeByName</a>, but lets you customize how the CSV data is
-- encoded.
encodeByNameWith :: ToNamedRecord a => EncodeOptions -> Header -> [a] -> ByteString
-- | Like <a>encodeDefaultOrderedByNameWith</a>, but lets you customize how
-- the CSV data is encoded.
encodeDefaultOrderedByNameWith :: (DefaultOrdered a, ToNamedRecord a) => EncodeOptions -> [a] -> ByteString
-- | CSV data represented as a Haskell vector of vector of bytestrings.
type Csv = Vector Record
-- | A record corresponds to a single line in a CSV file.
type Record = Vector Field
-- | A single field within a record.
type Field = ByteString
-- | The header corresponds to the first line a CSV file. Not all CSV files
-- have a header.
type Header = Vector Name
-- | A header has one or more names, describing the data in the column
-- following the name.
type Name = ByteString
-- | A record corresponds to a single line in a CSV file, indexed by the
-- column name rather than the column index.
type NamedRecord = HashMap ByteString ByteString
-- | A type that can be converted from a single CSV record, with the
-- possibility of failure.
--
-- When writing an instance, use <a>empty</a>, <a>mzero</a>, or
-- <a>fail</a> to make a conversion fail, e.g. if a <a>Record</a> has the
-- wrong number of columns.
--
-- Given this example data:
--
-- <pre>
-- John,56
-- Jane,55
-- </pre>
--
-- here's an example type and instance:
--
-- <pre>
-- data Person = Person { name :: !Text, age :: !Int }
--
-- instance FromRecord Person where
-- parseRecord v
-- | length v == 2 = Person <$>
-- v .! 0 <*>
-- v .! 1
-- | otherwise = mzero
-- </pre>
class FromRecord a where parseRecord r = to <$> gparseRecord r
parseRecord :: FromRecord a => Record -> Parser a
-- | Conversion of a field to a value might fail e.g. if the field is
-- malformed. This possibility is captured by the <a>Parser</a> type,
-- which lets you compose several field conversions together in such a
-- way that if any of them fail, the whole record conversion fails.
data Parser a
-- | Run a <a>Parser</a>, returning either <tt><a>Left</a> errMsg</tt> or
-- <tt><a>Right</a> result</tt>. Forces the value in the <a>Left</a> or
-- <a>Right</a> constructors to weak head normal form.
--
-- You most likely won't need to use this function directly, but it's
-- included for completeness.
runParser :: Parser a -> Either String a
-- | Retrieve the <i>n</i>th field in the given record. The result is
-- <a>empty</a> if the value cannot be converted to the desired type.
-- Raises an exception if the index is out of bounds.
--
-- <a>index</a> is a simple convenience function that is equivalent to
-- <tt><a>parseField</a> (v <a>!</a> idx)</tt>. If you're certain that
-- the index is not out of bounds, using <a>unsafeIndex</a> is somewhat
-- faster.
index :: FromField a => Record -> Int -> Parser a
-- | Alias for <a>index</a>.
(.!) :: FromField a => Record -> Int -> Parser a
-- | Like <a>index</a> but without bounds checking.
unsafeIndex :: FromField a => Record -> Int -> Parser a
-- | A type that can be converted to a single CSV record.
--
-- An example type and instance:
--
-- <pre>
-- data Person = Person { name :: !Text, age :: !Int }
--
-- instance ToRecord Person where
-- toRecord (Person name age) = record [
-- toField name, toField age]
-- </pre>
--
-- Outputs data on this form:
--
-- <pre>
-- John,56
-- Jane,55
-- </pre>
class ToRecord a where toRecord = fromList . gtoRecord . from
-- | Convert a value to a record.
toRecord :: ToRecord a => a -> Record
-- | Construct a record from a list of <a>ByteString</a>s. Use
-- <a>toField</a> to convert values to <a>ByteString</a>s for use with
-- <a>record</a>.
record :: [ByteString] -> Record
-- | Haskell lacks a single-element tuple type, so if you CSV data with
-- just one column you can use the <a>Only</a> type to represent a
-- single-column result.
newtype Only a
Only :: a -> Only a
[fromOnly] :: Only a -> a
-- | A type that can be converted from a single CSV record, with the
-- possibility of failure.
--
-- When writing an instance, use <a>empty</a>, <a>mzero</a>, or
-- <a>fail</a> to make a conversion fail, e.g. if a <a>Record</a> has the
-- wrong number of columns.
--
-- Given this example data:
--
-- <pre>
-- name,age
-- John,56
-- Jane,55
-- </pre>
--
-- here's an example type and instance:
--
-- <pre>
-- {-# LANGUAGE OverloadedStrings #-}
--
-- data Person = Person { name :: !Text, age :: !Int }
--
-- instance FromNamedRecord Person where
-- parseNamedRecord m = Person <$>
-- m .: "name" <*>
-- m .: "age"
-- </pre>
--
-- Note the use of the <tt>OverloadedStrings</tt> language extension
-- which enables <a>ByteString</a> values to be written as string
-- literals.
class FromNamedRecord a where parseNamedRecord r = to <$> gparseNamedRecord r
parseNamedRecord :: FromNamedRecord a => NamedRecord -> Parser a
-- | Retrieve a field in the given record by name. The result is
-- <a>empty</a> if the field is missing or if the value cannot be
-- converted to the desired type.
lookup :: FromField a => NamedRecord -> ByteString -> Parser a
-- | Alias for <a>lookup</a>.
(.:) :: FromField a => NamedRecord -> ByteString -> Parser a
-- | A type that can be converted to a single CSV record.
--
-- An example type and instance:
--
-- <pre>
-- data Person = Person { name :: !Text, age :: !Int }
--
-- instance ToNamedRecord Person where
-- toNamedRecord (Person name age) = namedRecord [
-- "name" .= name, "age" .= age]
-- </pre>
class ToNamedRecord a where toNamedRecord = namedRecord . gtoRecord . from
-- | Convert a value to a named record.
toNamedRecord :: ToNamedRecord a => a -> NamedRecord
-- | Construct a named record from a list of name-value <a>ByteString</a>
-- pairs. Use <a>.=</a> to construct such a pair from a name and a value.
namedRecord :: [(ByteString, ByteString)] -> NamedRecord
-- | Construct a pair from a name and a value. For use with
-- <a>namedRecord</a>.
namedField :: ToField a => ByteString -> a -> (ByteString, ByteString)
-- | Alias for <a>namedField</a>.
(.=) :: ToField a => ByteString -> a -> (ByteString, ByteString)
-- | Construct a header from a list of <a>ByteString</a>s.
header :: [ByteString] -> Header
-- | A type that can be converted from a single CSV field, with the
-- possibility of failure.
--
-- When writing an instance, use <a>empty</a>, <a>mzero</a>, or
-- <a>fail</a> to make a conversion fail, e.g. if a <a>Field</a> can't be
-- converted to the given type.
--
-- Example type and instance:
--
-- <pre>
-- {-# LANGUAGE OverloadedStrings #-}
--
-- data Color = Red | Green | Blue
--
-- instance FromField Color where
-- parseField s
-- | s == "R" = pure Red
-- | s == "G" = pure Green
-- | s == "B" = pure Blue
-- | otherwise = mzero
-- </pre>
class FromField a
parseField :: FromField a => Field -> Parser a
-- | A type that can be converted to a single CSV field.
--
-- Example type and instance:
--
-- <pre>
-- {-# LANGUAGE OverloadedStrings #-}
--
-- data Color = Red | Green | Blue
--
-- instance ToField Color where
-- toField Red = "R"
-- toField Green = "G"
-- toField Blue = "B"
-- </pre>
class ToField a
toField :: ToField a => a -> Field
|