basic structure for decodeRawRows

This commit is contained in:
2022-04-19 19:33:35 -04:00
parent eea4710b80
commit 67e85f0a78
4 changed files with 152 additions and 1 deletions
+24 -1
View File
@@ -29,13 +29,16 @@ module Data.CSV.Slurp (
decodeRows,
decodeRawRows,
decodeUTF8,
toBytes,
) where
import Conduit (ConduitT, mapC, (.|))
import Control.Monad.Trans.State (StateT, evalStateT)
import qualified Data.ByteString as BS
import Data.Maybe (fromMaybe)
import qualified Data.Text as T
import Data.Text.Encoding (decodeUtf8')
import Data.Word (Word8)
-- | decode the rows from a stream of ByteStrings
decodeRows :: Monad m => ConduitT BS.ByteString [T.Text] m ()
@@ -43,7 +46,7 @@ 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 ()
decodeRawRows = toBytes .| evalStateT decodeLoop newDecodeState
-- | decode a raw ByteString into Text (if possible)
decodeUTF8 :: BS.ByteString -> Maybe T.Text
@@ -51,4 +54,24 @@ decodeUTF8 bs = case decodeUtf8' bs of
Left _ -> Nothing
Right txt -> Just txt
-- | convert a stream to ByteStrings to a string of bytes
toBytes :: Monad m => ConduitT BS.ByteString Word8 m ()
toBytes = return ()
data DecodeState = DecodeState
{ isQuoted :: Bool
, collected :: BS.ByteString
} deriving (Eq, Show)
newDecodeState :: DecodeState
newDecodeState = DecodeState
{ isQuoted = False
, collected = ""
}
decodeLoop
:: Monad m
=> StateT DecodeState (ConduitT Word8 [BS.ByteString] m) ()
decodeLoop = return ()
--jl