Преглед изворни кода

New tutorial binary, fix a few bugs

tags/v0.1.0.0
Peter J. Jones пре 5 година
родитељ
комит
7c4a17fb0c
5 измењених фајлова са 66 додато и 11 уклоњено
  1. 29
    0
      bin/tutorial.hs
  2. 21
    0
      cassava-streams.cabal
  3. 12
    10
      src/System/IO/Streams/Csv/Decode.hs
  4. 1
    1
      src/System/IO/Streams/Csv/Tutorial.hs
  5. 3
    0
      test/simple.csv

+ 29
- 0
bin/tutorial.hs Прегледај датотеку

@@ -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"

+ 21
- 0
cassava-streams.cabal Прегледај датотеку

@@ -23,6 +23,7 @@ description:
23 23
 extra-source-files:
24 24
   README.md
25 25
   CHANGELOG
26
+  test/simple.csv
26 27
 
27 28
 --------------------------------------------------------------------------------
28 29
 source-repository head
@@ -34,6 +35,11 @@ flag maintainer
34 35
   description: Enable settings for the package maintainer.
35 36
   default: False
36 37
 
38
+--------------------------------------------------------------------------------
39
+flag tutorial
40
+  description: Build the tutorial binary (useful for profiling).
41
+  default: False
42
+
37 43
 --------------------------------------------------------------------------------
38 44
 library
39 45
   exposed-modules:
@@ -56,3 +62,18 @@ library
56 62
                , cassava    >= 0.3   && < 0.4
57 63
                , io-streams >= 1.1.0 && < 2.0
58 64
                , 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

+ 12
- 10
src/System/IO/Streams/Csv/Decode.hs Прегледај датотеку

@@ -50,7 +50,7 @@ decodeStreamWith :: (FromRecord a)
50 50
                  -> IO (InputStream a)      -- ^ An @InputStream@ which produces records.
51 51
 decodeStreamWith ops hdr input = do
52 52
   queue  <- newIORef []
53
-  parser <- newIORef (decodeWith ops hdr)
53
+  parser <- newIORef $ Just (decodeWith ops hdr)
54 54
   makeInputStream (dispatch queue parser input)
55 55
 
56 56
 --------------------------------------------------------------------------------
@@ -76,15 +76,15 @@ decodeStreamByNameWith ops input = go (decodeByNameWith ops) where
76 76
   go (PartialH f) = Streams.read input >>= go . maybe (f BS.empty) f
77 77
   go (DoneH _ p)  = do
78 78
     queue  <- newIORef []
79
-    parser <- newIORef p
79
+    parser <- newIORef (Just p)
80 80
     makeInputStream (dispatch queue parser input)
81 81
 
82 82
 --------------------------------------------------------------------------------
83 83
 -- | 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.
88 88
 dispatch queueRef parserRef input = do
89 89
   queue <- readIORef queueRef
90 90
 
@@ -92,9 +92,10 @@ dispatch queueRef parserRef input = do
92 92
     [] -> do
93 93
       parser <- readIORef parserRef
94 94
       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
98 99
 
99 100
     (x:xs) -> do
100 101
       writeIORef queueRef xs
@@ -103,7 +104,8 @@ dispatch queueRef parserRef input = do
103 104
   where
104 105
     -- Send more data to the CSV parser.  If there is no more data
105 106
     -- 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
107 109
 
108 110
     -- Feed more data downstream or fail if some records didn't parse
109 111
     -- correctly.  The elements are wrapped in an @Either@ which

+ 1
- 1
src/System/IO/Streams/Csv/Tutorial.hs Прегледај датотеку

@@ -95,7 +95,7 @@ onlyTodo inH outH = do
95 95
   -- A stream which produces items which are not 'Done'.
96 96
   input  <- Streams.handleToInputStream inH   >>=
97 97
             decodeStreamByName                >>=
98
-            Streams.filter (\item -> state item == Done)
98
+            Streams.filter (\item -> state item /= Done)
99 99
 
100 100
   -- A stream to write items into.  They will be converted to CSV.
101 101
   output <- Streams.handleToOutputStream outH >>=

+ 3
- 0
test/simple.csv Прегледај датотеку

@@ -0,0 +1,3 @@
1
+State,Title
2
+TODO,Make a bigger file
3
+TODO,Release this package

Loading…
Откажи
Сачувај