ソースを参照

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

This file is part of the Haskell package cassava-streams. It is
subject to the license terms in the LICENSE file found in the
top-level directory of this distribution and at
git://pmade.com/cassava-streams/LICENSE. No part of cassava-streams
package, including this file, may be copied, modified, propagated, or
distributed except according to the terms contained in the LICENSE
file.

-}

--------------------------------------------------------------------------------
module Main (main) where

--------------------------------------------------------------------------------
import System.Environment
import System.IO
import System.IO.Streams.Csv.Tutorial

--------------------------------------------------------------------------------
main :: IO ()
main = do
args <- getArgs

case args of
["todo"] -> onlyTodo stdin stdout
["done", x] -> markDone x stdin stdout
_ -> fail "give one of todo or done"

+ 21
- 0
cassava-streams.cabal ファイルの表示

@@ -23,6 +23,7 @@ description:
extra-source-files:
README.md
CHANGELOG
test/simple.csv

--------------------------------------------------------------------------------
source-repository head
@@ -34,6 +35,11 @@ flag maintainer
description: Enable settings for the package maintainer.
default: False

--------------------------------------------------------------------------------
flag tutorial
description: Build the tutorial binary (useful for profiling).
default: False

--------------------------------------------------------------------------------
library
exposed-modules:
@@ -56,3 +62,18 @@ library
, cassava >= 0.3 && < 0.4
, io-streams >= 1.1.0 && < 2.0
, vector >= 0.10 && < 1.0

--------------------------------------------------------------------------------
executable tutorial
default-language: Haskell2010
hs-source-dirs: bin
main-is: tutorial.hs
ghc-options: -O2 -Wall -Werror -rtsopts
ghc-prof-options: -fprof-auto-top -fprof-cafs

if !flag(tutorial)
buildable: False
else
build-depends: base
, cassava-streams
, io-streams

+ 12
- 10
src/System/IO/Streams/Csv/Decode.hs ファイルの表示

@@ -50,7 +50,7 @@ decodeStreamWith :: (FromRecord a)
-> IO (InputStream a) -- ^ An @InputStream@ which produces records.
decodeStreamWith ops hdr input = do
queue <- newIORef []
parser <- newIORef (decodeWith ops hdr)
parser <- newIORef $ Just (decodeWith ops hdr)
makeInputStream (dispatch queue parser input)

--------------------------------------------------------------------------------
@@ -76,15 +76,15 @@ decodeStreamByNameWith ops input = go (decodeByNameWith ops) where
go (PartialH f) = Streams.read input >>= go . maybe (f BS.empty) f
go (DoneH _ p) = do
queue <- newIORef []
parser <- newIORef p
parser <- newIORef (Just p)
makeInputStream (dispatch queue parser input)

--------------------------------------------------------------------------------
-- | Internal function which feeds data to the CSV parser.
dispatch :: IORef [a] -- ^ List of queued CSV records.
-> IORef (Parser a) -- ^ Current CSV parser state.
-> InputStream ByteString -- ^ Upstream.
-> IO (Maybe a) -- ^ Data feed downstream.
dispatch :: IORef [a] -- ^ List of queued CSV records.
-> IORef (Maybe (Parser a)) -- ^ Current CSV parser state.
-> InputStream ByteString -- ^ Upstream.
-> IO (Maybe a) -- ^ Data feed downstream.
dispatch queueRef parserRef input = do
queue <- readIORef queueRef

@@ -92,9 +92,10 @@ dispatch queueRef parserRef input = do
[] -> do
parser <- readIORef parserRef
case parser of
Fail _ e -> bomb e
Many xs f -> more f >> feed xs
Done xs -> feed xs
Nothing -> return Nothing
Just (Fail _ e) -> bomb e
Just (Many xs f) -> more f >> feed xs
Just (Done xs ) -> writeIORef parserRef Nothing >> feed xs

(x:xs) -> do
writeIORef queueRef xs
@@ -103,7 +104,8 @@ dispatch queueRef parserRef input = do
where
-- Send more data to the CSV parser. If there is no more data
-- from upstream then send an empty @ByteString@.
more f = Streams.read input >>= writeIORef parserRef . maybe (f BS.empty) f
more f = Streams.read input >>=
writeIORef parserRef . Just . maybe (f BS.empty) f

-- Feed more data downstream or fail if some records didn't parse
-- 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
-- A stream which produces items which are not 'Done'.
input <- Streams.handleToInputStream inH >>=
decodeStreamByName >>=
Streams.filter (\item -> state item == Done)
Streams.filter (\item -> state item /= Done)

-- A stream to write items into. They will be converted to CSV.
output <- Streams.handleToOutputStream outH >>=

+ 3
- 0
test/simple.csv ファイルの表示

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

読み込み中…
キャンセル
保存