diff --git a/src/Data/CSV/Sip.hs b/src/Data/CSV/Sip.hs index 7d1ab07..5cdadf6 100644 --- a/src/Data/CSV/Sip.hs +++ b/src/Data/CSV/Sip.hs @@ -26,11 +26,17 @@ along with this program. If not, see . {-# 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