-- | Like 'decode', but lets you customize how the CSV data is parsed.
decodeWith :: FromRecord a
           => DecodeOptions  -- ^ Decoding options
           -> Bool           -- ^ Data contains header that should be
                             -- skipped
           -> Parser a
decodeWith !opts skipHeader
    | skipHeader = Partial $ \ s -> go (decodeHeaderWith opts `feedChunkH` s)
    | otherwise  = Partial (decodeWithP parseRecord opts)
  where go (FailH rest msg) = Fail rest msg
        go (PartialH k)     = Partial $ \ s' -> go (k s')
        go (DoneH _ rest)   = decodeWithP parseRecord opts rest

------------------------------------------------------------------------

-- | Efficiently deserialize CSV in an incremental fashion. The data
-- is assumed to be preceeded by a header. Returns a 'HeaderParser'
-- that when done produces a 'Parser' for parsing the actual records.
-- Equivalent to @'decodeByNameWith' 'defaultDecodeOptions'@.
decodeWith :: FromRecord a
           => DecodeOptions  -- ^ Decoding options
           -> Bool           -- ^ Data contains header that should be
                             -- skipped
           -> Parser a
decodeWith !opts skipHeader
    | skipHeader = Partial $ \ s -> go (decodeHeaderWith opts `feedChunkH` s)
    | otherwise  = Partial (decodeWithP parseRecord opts)
  where go (FailH rest msg) = Fail rest msg
        go (PartialH k)     = Partial $ \ s' -> go (k s')
        go (DoneH _ rest)   = decodeWithP parseRecord opts rest

------------------------------------------------------------------------

-- | Efficiently deserialize CSV in an incremental fashion. The data
-- is assumed to be preceeded by a header. Returns a 'HeaderParser'
-- that when done produces a 'Parser' for parsing the actual records.
-- Equivalent to @'decodeByNameWith' 'defaultDecodeOptions'@.
decodeWith !opts skipHeader
    | skipHeader = Partial $ \ s -> go (decodeHeaderWith opts `feedChunkH` s)
    | otherwise  = Partial (decodeWithP parseRecord opts)
  where go (FailH rest msg) = Fail rest msg
        go (PartialH k)     = Partial $ \ s' -> go (k s')
        go (DoneH _ rest)   = decodeWithP parseRecord opts rest

------------------------------------------------------------------------

-- | Efficiently deserialize CSV in an incremental fashion. The data
-- is assumed to be preceeded by a header. Returns a 'HeaderParser'
-- that when done produces a 'Parser' for parsing the actual records.
-- Equivalent to @'decodeByNameWith' 'defaultDecodeOptions'@.