implemented file writing

This commit is contained in:
Jonathan Lamothe 2022-04-24 16:47:24 -04:00
parent 5c74085ada
commit d27eb91952

View File

@ -26,11 +26,17 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
{-# LANGUAGE LambdaCase, OverloadedStrings #-}
module Data.CSV.Sip (
-- * Read an entire CSV file
-- * Working with Files
-- ** Read an entire CSV file
slurpCSV,
slurpRawCSV,
slurpLabelledCSV,
slurpRawLabelledCSV,
-- ** Write an entire CSV file
writeCSV,
writeRawCSV,
writeCSVFromStream,
writeRawCSVFromStream,
-- * Conduits
-- ** Encoding
encodeCSV,
@ -51,6 +57,7 @@ import Conduit
, await
, mapC
, runConduit
, sinkFile
, sourceFile
, yield
, (.|)
@ -100,6 +107,48 @@ slurpRawLabelledCSV
slurpRawLabelledCSV file = runConduit $
sourceFile file .| decodeRawRows .| labelFields .|consume
-- | write a CSV file from Text-based rows
writeCSV
:: MonadResource m
=> FilePath
-- ^ the path to the file to write to
-> [[T.Text]]
-- ^ the fields/rows being written
-> m ()
writeCSV file csv = runConduit $ encodeCSV csv .| sinkFile file
-- | write a CSV file from raw ByteString-based rows
writeRawCSV
:: MonadResource m
=> FilePath
-- ^ the path to the file to write to
-> [[BS.ByteString]]
-- ^ the fields/rows being written
-> m ()
writeRawCSV file csv = runConduit $ encodeRawCSV csv .| sinkFile file
-- | Write a CSV file from a stream of Text-based rows
writeCSVFromStream
:: MonadResource m
=> FilePath
-- ^ the path to the file to write to
-> ConduitT () [T.Text] m ()
-- ^ the source conduit
-> m ()
writeCSVFromStream file src = runConduit $
src .| encodeRows .| sinkFile file
-- | Write a CSV file from a stream of ByteString-based rows
writeRawCSVFromStream
:: MonadResource m
=> FilePath
-- ^ the path to the file to write to
-> ConduitT () [BS.ByteString] m ()
-- ^ the source conduit
-> m ()
writeRawCSVFromStream file src = runConduit $
src .| encodeRawRows .| sinkFile file
-- | encode an entire CSV file
encodeCSV
:: Monad m