git.iko.soy/typeable/cassava-streams
on trunk help sign in
Add the encoding piece
commit5fbfc323c630783d4584a98a1cacc32f692fc623 authorPeter Jones <pjones@pmade.com> dateTue 22 Apr 2014, 09:56 (13 years ago) parent611b73a
3 files changed+77−6
cassava-streams.cabal+13−4 src/System/IO/Streams/Csv.hs+18−2 src/System/IO/Streams/Csv/Encode.hs+46
cassava-streams.cabal+13−4
@@ -6,12 +6,19 @@ license: BSD3
66license-file: LICENSE
77author: Peter Jones <pjones@devalot.com>
88maintainer: Peter Jones <pjones@devalot.com>
9−copyright: Copyright (c) 2013,2014 Peter Jones
9+copyright: Copyright (c) 2014 Peter Jones
1010category: Data, Text, CSV, IO-Streams
1111build-type: Simple
1212cabal-version: >=1.10
13−-- description:
14−-- extra-source-files:
13+tested-with: GHC==7.8.2
14+bug-reports: https://github.com/pjones/cassava-streams/issues
15+description:
16+ The cassava-streams library glues togeter the cassava CSV library
17+ and the io-streams streaming library.
18+
19+--------------------------------------------------------------------------------
20+extra-source-files:
21+ README.md
1522
1623--------------------------------------------------------------------------------
1724source-repository head
@@ -28,11 +35,13 @@ library
2835 exposed-modules:
2936 System.IO.Streams.Csv
3037 other-modules:
38+ System.IO.Streams.Csv.Encode
3139 System.IO.Streams.Csv.Decode
3240
3341 hs-source-dirs: src
3442 default-language: Haskell2010
35− ghc-options: -Wall
43+ ghc-options: -O2 -Wall
44+ ghc-prof-options: -prof -auto-all
3645
3746 if flag(maintainer)
3847 ghc-options: -Werror
src/System/IO/Streams/Csv.hs+18−2
@@ -11,7 +11,23 @@ file.
1111-}
1212
1313--------------------------------------------------------------------------------
14−module System.IO.Streams.Csv (module Export) where
14+-- | This module exports functions which can be used to read instances
15+-- of the cassava classes @FromRecord@ and @FromNamedRecord@ from an
16+-- io-streams @InputStream ByteString@. It also exports functions
17+-- which can write instances of @ToRecord@ and @ToNamedRecord@ to an
18+-- io-streams @OutputStream ByteString@.
19+module System.IO.Streams.Csv
20+ ( -- * Decoding
21+ decodeStream
22+ , decodeStreamWith
23+ , decodeStreamByName
24+ , decodeStreamByNameWith
25+
26+ -- * Encoding
27+ , encodeStream
28+ , encodeStreamByName
29+ ) where
1530
1631--------------------------------------------------------------------------------
17−import System.IO.Streams.Csv.Decode as Export
32+import System.IO.Streams.Csv.Decode
33+import System.IO.Streams.Csv.Encode
src/System/IO/Streams/Csv/Encode.hsnew file+46
@@ -0,0 +1,46 @@
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 System.IO.Streams.Csv.Encode
15+ ( encodeStream
16+ , encodeStreamByName
17+ ) where
18+
19+--------------------------------------------------------------------------------
20+import Data.ByteString (ByteString)
21+import Data.Csv
22+import System.IO.Streams (OutputStream, makeOutputStream)
23+import qualified System.IO.Streams as Streams
24+
25+--------------------------------------------------------------------------------
26+-- | Create a new @OutputStream@ that can be fed @ToRecord@ values
27+-- which are converted to CSV. The records are encoded into
28+-- @ByteString@s and passed on to the given downstream @OutputStream@.
29+encodeStream :: ToRecord a
30+ => OutputStream ByteString -- ^ Downstream.
31+ -> IO (OutputStream a) -- ^ New @OutputStream@.
32+encodeStream output = makeOutputStream stream where
33+ stream Nothing = Streams.write Nothing output
34+ stream (Just x) = Streams.writeLazyByteString (encode [x]) output
35+
36+--------------------------------------------------------------------------------
37+-- | Create a new @OutputStream@ that can be fed @ToNamedRecord@
38+-- values which are converted to CSV. The records are encoded into
39+-- @ByteString@s and passed on to the given downstream @OutputStream@.
40+encodeStreamByName :: ToNamedRecord a
41+ => Header -- ^ CSV Header.
42+ -> OutputStream ByteString -- ^ Downstream.
43+ -> IO (OutputStream a) -- ^ New @OutputStream@.
44+encodeStreamByName hdr output = makeOutputStream stream where
45+ stream Nothing = Streams.write Nothing output
46+ stream (Just x) = Streams.writeLazyByteString (encodeByName hdr [x]) output