git.iko.soy/typeable/cassava-streams
on trunk help sign in
New tutorial binary, fix a few bugs
commit7c4a17fb0c92a12e8ee21a19830abc15ef8ddfd7 authorPeter Jones <pjones@pmade.com> dateTue 22 Apr 2014, 14:07 (12 years ago) parent02d0de7
5 files changed+66−11
bin/tutorial.hs+29 cassava-streams.cabal+21 src/System/IO/Streams/Csv/Decode.hs+12−10 src/System/IO/Streams/Csv/Tutorial.hs+1−1 test/simple.csv+3
bin/tutorial.hsnew file+29
@@ -0,0 +1,29 @@
1+{-
2+
3+This file is part of the Haskell package cassava-streams. It is
4+subject to the license terms in the LICENSE file found in the
5+top-level directory of this distribution and at
6+git://pmade.com/cassava-streams/LICENSE. No part of cassava-streams
7+package, including this file, may be copied, modified, propagated, or
8+distributed except according to the terms contained in the LICENSE
9+file.
10+
11+-}
12+
13+--------------------------------------------------------------------------------
14+module Main (main) where
15+
16+--------------------------------------------------------------------------------
17+import System.Environment
18+import System.IO
19+import System.IO.Streams.Csv.Tutorial
20+
21+--------------------------------------------------------------------------------
22+main :: IO ()
23+main = do
24+ args <- getArgs
25+
26+ case args of
27+ ["todo"] -> onlyTodo stdin stdout
28+ ["done", x] -> markDone x stdin stdout
29+ _ -> fail "give one of todo or done"
cassava-streams.cabal+21
@@ -23,6 +23,7 @@ description:
2323extra-source-files:
2424 README.md
2525 CHANGELOG
26+ test/simple.csv
2627
2728--------------------------------------------------------------------------------
2829source-repository head
@@ -34,6 +35,11 @@ flag maintainer
3435 description: Enable settings for the package maintainer.
3536 default: False
3637
38+--------------------------------------------------------------------------------
39+flag tutorial
40+ description: Build the tutorial binary (useful for profiling).
41+ default: False
42+
3743--------------------------------------------------------------------------------
3844library
3945 exposed-modules:
@@ -56,3 +62,18 @@ library
5662 , cassava >= 0.3 && < 0.4
5763 , io-streams >= 1.1.0 && < 2.0
5864 , vector >= 0.10 && < 1.0
65+
66+--------------------------------------------------------------------------------
67+executable tutorial
68+ default-language: Haskell2010
69+ hs-source-dirs: bin
70+ main-is: tutorial.hs
71+ ghc-options: -O2 -Wall -Werror -rtsopts
72+ ghc-prof-options: -fprof-auto-top -fprof-cafs
73+
74+ if !flag(tutorial)
75+ buildable: False
76+ else
77+ build-depends: base
78+ , cassava-streams
79+ , io-streams
src/System/IO/Streams/Csv/Decode.hs+12−10
@@ -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
src/System/IO/Streams/Csv/Tutorial.hs+1−1
@@ -95,7 +95,7 @@ onlyTodo inH outH = do
9595 -- A stream which produces items which are not 'Done'.
9696 input <- Streams.handleToInputStream inH >>=
9797 decodeStreamByName >>=
98− Streams.filter (\item -> state item == Done)
98+ Streams.filter (\item -> state item /= Done)
9999
100100 -- A stream to write items into. They will be converted to CSV.
101101 output <- Streams.handleToOutputStream outH >>=
test/simple.csvnew file+3
@@ -0,0 +1,3 @@
1+State,Title
2+TODO,Make a bigger file
3+TODO,Release this package