packages feed

zlib-conduit 0.0.1 → 0.2.0

raw patch · 3 files changed

+76/−13 lines, 3 filesdep ~conduitdep ~zlib-bindingsPVP ok

version bump matches the API change (PVP)

Dependency ranges changed: conduit, zlib-bindings

API changes (from Hackage documentation)

+ Data.Conduit.Zlib: compressFlush :: ResourceUnsafeIO m => Int -> WindowBits -> Conduit (Flush ByteString) m (Flush ByteString)
+ Data.Conduit.Zlib: decompressFlush :: ResourceUnsafeIO m => WindowBits -> Conduit (Flush ByteString) m (Flush ByteString)

Files

Data/Conduit/Zlib.hs view
@@ -5,6 +5,8 @@ module Data.Conduit.Zlib (     -- * Conduits     compress, decompress, gzip, ungzip,+    -- * Flushing+    compressFlush, decompressFlush,     -- * Re-exported from zlib-bindings     WindowBits (..), defaultWindowBits ) where@@ -33,17 +35,45 @@     :: ResourceUnsafeIO m     => WindowBits -- ^ Zlib parameter (see the zlib-bindings package as well as the zlib C library)     -> Conduit ByteString m ByteString-decompress config = Conduit $ do-    inf <- lift $ unsafeFromIO $ initInflate config-    return $ PreparedConduit (push inf) (close inf)+decompress config = Conduit+    { conduitPush = \input -> do+        inf <- lift $ unsafeFromIO $ initInflate config+        push inf input+    , conduitClose = return []+    }   where+    mkCon inf = Conduit (push inf) (close inf)     push inf x = do         chunks <- lift $ unsafeFromIO $ withInflateInput inf x callback-        return $ Producing chunks+        return $ Producing (mkCon inf) chunks     close inf = do         chunk <- lift $ unsafeFromIO $ finishInflate inf         return $ if S.null chunk then [] else [chunk] +-- | Same as 'decompress', but allows you to explicitly flush the stream.+decompressFlush+    :: ResourceUnsafeIO m+    => WindowBits -- ^ Zlib parameter (see the zlib-bindings package as well as the zlib C library)+    -> Conduit (Flush ByteString) m (Flush ByteString)+decompressFlush config = Conduit+    { conduitPush = \input -> do+        inf <- lift $ unsafeFromIO $ initInflate config+        push inf input+    , conduitClose = return []+    }+  where+    mkCon inf = Conduit (push inf) (close inf)+    push inf (Chunk x) = do+        chunks <- lift $ unsafeFromIO $ withInflateInput inf x callback+        return $ Producing (mkCon inf) $ map Chunk chunks+    push inf Flush = do+        chunk <- lift $ unsafeFromIO $ flushInflate inf+        let chunk' = if S.null chunk then id else (Chunk chunk:)+        return $ Producing (mkCon inf) $ chunk' [Flush]+    close inf = do+        chunk <- lift $ unsafeFromIO $ finishInflate inf+        return $ if S.null chunk then [] else [Chunk chunk]+ -- | -- Compress (deflate) a stream of 'ByteString's. The 'WindowBits' also control -- the format (zlib vs. gzip).@@ -53,16 +83,43 @@     => Int         -- ^ Compression level     -> WindowBits  -- ^ Zlib parameter (see the zlib-bindings package as well as the zlib C library)     -> Conduit ByteString m ByteString-compress level config = Conduit $ do-    def <- lift $ unsafeFromIO $ initDeflate level config-    return $ PreparedConduit (push def) (close def)+compress level config = Conduit+    { conduitPush = \input -> do+        def <- lift $ unsafeFromIO $ initDeflate level config+        push def input+    , conduitClose = return []+    }   where     push def x = do         chunks <- lift $ unsafeFromIO $ withDeflateInput def x callback-        return $ Producing chunks+        return $ Producing (Conduit (push def) (close def)) chunks     close def = do         chunks <- lift $ unsafeFromIO $ finishDeflate def callback         return chunks++-- | Same as 'compress', but allows you to explicitly flush the stream.+compressFlush+    :: ResourceUnsafeIO m+    => Int         -- ^ Compression level+    -> WindowBits  -- ^ Zlib parameter (see the zlib-bindings package as well as the zlib C library)+    -> Conduit (Flush ByteString) m (Flush ByteString)+compressFlush level config = Conduit+    { conduitPush = \input -> do+        def <- lift $ unsafeFromIO $ initDeflate level config+        push def input+    , conduitClose = return []+    }+  where+    mkCon def = Conduit (push def) (close def)+    push def (Chunk x) = do+        chunks <- lift $ unsafeFromIO $ withDeflateInput def x callback+        return $ Producing (mkCon def) $ map Chunk chunks+    push def Flush = do+        chunks <- lift $ unsafeFromIO $ flushDeflate def callback+        return $ Producing (mkCon def) $ map Chunk chunks ++ [Flush]+    close def = do+        chunks <- lift $ unsafeFromIO $ finishDeflate def callback+        return $ map Chunk chunks  callback :: Monad m => m (Maybe a) -> m [a] callback pop = go id where
test/main.hs view
@@ -1,5 +1,3 @@-{-# LANGUAGE OverloadedStrings #-}-{-# LANGUAGE CPP #-} import Test.Hspec.Monadic import Test.Hspec.HUnit () import Test.Hspec.QuickCheck (prop)@@ -23,3 +21,11 @@                 src = mconcat $ map (CL.sourceList . return) bss             outBss <- src C.$= CZ.gzip C.$= CZ.ungzip C.$$ CL.consume             return $ lbs == L.fromChunks outBss+        prop "flush" $ \bss' -> runST $ runResourceT $ do+            let bss = map S.pack $ filter (not . null) bss'+                bssC = concatMap (\bs -> [C.Chunk bs, C.Flush]) bss+                src = mconcat $ map (CL.sourceList . return) bssC+            outBssC <- src C.$= CZ.compressFlush 5 (CZ.WindowBits 31)+                           C.$= CZ.decompressFlush (CZ.WindowBits 31)+                           C.$$ CL.consume+            return $ bssC == outBssC
zlib-conduit.cabal view
@@ -1,5 +1,5 @@ Name:                zlib-conduit-Version:             0.0.1+Version:             0.2.0 Synopsis:            Streaming compression/decompression via conduits. Description:         Streaming compression/decompression via conduits. License:             BSD3@@ -20,8 +20,8 @@                      , containers                      , transformers             >= 0.2.2        && < 0.3                      , bytestring               >= 0.9-                     , zlib-bindings            >= 0.0.1        && < 0.1-                     , conduit+                     , zlib-bindings            >= 0.0.3        && < 0.1+                     , conduit                  >= 0.2   ghc-options:     -Wall  test-suite test