{-| Module : Data.CSV.Slurp Description : works with CSV files Copyright : (C) Jonathan Lamothe License : GPL-3.0-or-later Maintainer : jonathan@jlamothe.net Stability : experimental Portability : POSIX This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . -} {-# LANGUAGE OverloadedStrings #-} module Data.CSV.Slurp ( decodeRows, decodeRawRows, decodeUTF8, ) where import Conduit (ConduitT, mapC, (.|)) import qualified Data.ByteString as BS import Data.Maybe (fromMaybe) import qualified Data.Text as T import Data.Text.Encoding (decodeUtf8') -- | decode the rows from a stream of ByteStrings decodeRows :: Monad m => ConduitT BS.ByteString [T.Text] m () decodeRows = decodeRawRows .| mapC (map $ fromMaybe "" . decodeUTF8) -- | decode the rows returning raw ByteStrings instead of text decodeRawRows :: Monad m => ConduitT BS.ByteString [BS.ByteString] m () decodeRawRows = return () -- | decode a raw ByteString into Text (if possible) decodeUTF8 :: BS.ByteString -> Maybe T.Text decodeUTF8 bs = case decodeUtf8' bs of Left _ -> Nothing Right txt -> Just txt --jl