@@ -50,7 +50,7 @@ decodeStreamWith :: (FromRecord a)
5050 -> IO (InputStream a) -- ^ An @InputStream@ which produces records.
5151decodeStreamWith ops hdr input = do
5252 queue <- newIORef []
53− parser <- newIORef (decodeWith ops hdr)
53+ parser <- newIORef $ Just (decodeWith ops hdr)
5454 makeInputStream (dispatch queue parser input)
5555
5656--------------------------------------------------------------------------------
@@ -76,15 +76,15 @@ decodeStreamByNameWith ops input = go (decodeByNameWith ops) where
7676 go (PartialH f) = Streams.read input >>= go . maybe (f BS.empty) f
7777 go (DoneH _ p) = do
7878 queue <- newIORef []
79− parser <- newIORef p
79+ parser <- newIORef (Just p)
8080 makeInputStream (dispatch queue parser input)
8181
8282--------------------------------------------------------------------------------
8383-- | Internal function which feeds data to the CSV parser.
84−dispatch :: IORef [a] -- ^ List of queued CSV records.
85− -> IORef (Parser a) -- ^ Current CSV parser state.
86− -> InputStream ByteString -- ^ Upstream.
87− -> IO (Maybe a) -- ^ Data feed downstream.
84+dispatch :: IORef [a] -- ^ List of queued CSV records.
85+ -> IORef (Maybe (Parser a)) -- ^ Current CSV parser state.
86+ -> InputStream ByteString -- ^ Upstream.
87+ -> IO (Maybe a) -- ^ Data feed downstream.
8888dispatch queueRef parserRef input = do
8989 queue <- readIORef queueRef
9090
@@ -92,9 +92,10 @@ dispatch queueRef parserRef input = do
9292 [] -> do
9393 parser <- readIORef parserRef
9494 case parser of
95− Fail _ e -> bomb e
96− Many xs f -> more f >> feed xs
97− Done xs -> feed xs
95+ Nothing -> return Nothing
96+ Just (Fail _ e) -> bomb e
97+ Just (Many xs f) -> more f >> feed xs
98+ Just (Done xs ) -> writeIORef parserRef Nothing >> feed xs
9899
99100 (x:xs) -> do
100101 writeIORef queueRef xs
@@ -103,7 +104,8 @@ dispatch queueRef parserRef input = do
103104 where
104105 -- Send more data to the CSV parser. If there is no more data
105106 -- from upstream then send an empty @ByteString@.
106− more f = Streams.read input >>= writeIORef parserRef . maybe (f BS.empty) f
107+ more f = Streams.read input >>=
108+ writeIORef parserRef . Just . maybe (f BS.empty) f
107109
108110 -- Feed more data downstream or fail if some records didn't parse
109111 -- correctly. The elements are wrapped in an @Either@ which