@@ -1,4 +1,5 @@
11{-# LANGUAGE DeriveDataTypeable #-}
2+{-# LANGUAGE ScopedTypeVariables #-}
23
34{-
45
@@ -116,12 +117,12 @@ onlyValidRecords input = makeInputStream $ do
116117
117118 case upstream of
118119 Nothing -> return Nothing
119− Just (Left err) -> bomb err
120+ Just (Left err) -> bomb ("invalid CSV row: " ++ err)
120121 Just (Right x) -> return (Just x)
121122
122123--------------------------------------------------------------------------------
123124-- | Internal function which feeds data to the CSV parser.
124−dispatch :: IORef [Either String a]
125+dispatch :: forall a. IORef [Either String a]
125126 -- ^ List of queued CSV records.
126127 -> IORef (Maybe (Parser a))
127128 -- ^ Current CSV parser state.
@@ -137,9 +138,10 @@ dispatch queueRef parserRef input = do
137138 parser <- readIORef parserRef
138139 case parser of
139140 Nothing -> return Nothing
140− Just (Fail _ e) -> bomb e
141− Just (Many xs f) -> more f >> feed xs
142− Just (Done xs ) -> writeIORef parserRef Nothing >> feed xs
141+ Just (Fail _ e) -> bomb ("input data malformed: " ++ e)
142+ Just (Many [] f) -> more f
143+ Just (Many xs f) -> writeIORef parserRef (Just $ Many [] f) >> feed xs
144+ Just (Done xs ) -> writeIORef parserRef Nothing >> feed xs
143145
144146 (x:xs) -> do
145147 writeIORef queueRef xs
@@ -148,12 +150,15 @@ dispatch queueRef parserRef input = do
148150 where
149151 -- Send more data to the CSV parser. If there is no more data
150152 -- from upstream then send an empty @ByteString@.
151− more f = Streams.read input >>=
152− writeIORef parserRef . Just . maybe (f BS.empty) f
153+ more :: (ByteString -> Parser a) -> IO (Maybe (Either String a))
154+ more f = do bs <- Streams.read input
155+ writeIORef parserRef (Just $ maybe (f BS.empty) f bs)
156+ dispatch queueRef parserRef input
153157
154158 -- Feed records downstream.
155− feed xs = writeIORef queueRef xs >>
156− dispatch queueRef parserRef input
159+ feed :: [Either String a] -> IO (Maybe (Either String a))
160+ feed xs = do writeIORef queueRef xs
161+ dispatch queueRef parserRef input
157162
158163--------------------------------------------------------------------------------
159164-- | Throw an exception.