Initial commit
9 files changed, 314 insertions(+), 0 deletions(-)

A => .hgignore
A => CHANGELOG.md
A => LICENSE
A => README.md
A => Setup.hs
A => src/Streamly/Data/Binary.hs
A => stack.yaml
A => streamly-binary.cabal
A => test/Spec.hs
A => .hgignore +6 -0
@@ 0,0 1,6 @@ 
+syntax: glob
+
+.stack-work/
+dist-newstyle
+stack*.yaml.lock
+

          
A => CHANGELOG.md +5 -0
@@ 0,0 1,5 @@ 
+# Revision history for streamly-binary
+
+## 1.0.0.0
+
+* First version

          
A => LICENSE +30 -0
@@ 0,0 1,30 @@ 
+Copyright (c) 2020, G. Eyaeb
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+
+    * Redistributions in binary form must reproduce the above
+      copyright notice, this list of conditions and the following
+      disclaimer in the documentation and/or other materials provided
+      with the distribution.
+
+    * Neither the name of G. Eyaeb nor the names of other
+      contributors may be used to endorse or promote products derived
+      from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

          
A => README.md +3 -0
@@ 0,0 1,3 @@ 
+# streamly-binary
+
+Support for encoding/decoding using @binary@ in @streamly@ streams.

          
A => Setup.hs +2 -0
@@ 0,0 1,2 @@ 
+import Distribution.Simple
+main = defaultMain

          
A => src/Streamly/Data/Binary.hs +68 -0
@@ 0,0 1,68 @@ 
+{- ORMOLU_DISABLE -}
+{-|
+Module      : Streamly.Data.Binary
+Description : Support for encoding/decoding using @binary@ in @streamly@ streams.
+Copyright   : © 2020 G. Eyaeb
+License     : BSD-3-Clause
+Maintainer  : geyaeb@protonmail.com
+Stability   : experimental
+Portability : POSIX
+
+This module contains functions for decoding stream of bytestrings (coming, for example, from TCP connection)
+to your data type using [binary](https://hackage.haskell.org/package/binary) and vice versa.
+-}
+{- ORMOLU_ENABLE -}
+module Streamly.Data.Binary
+  ( decodeStream,
+    decodeStreamGet,
+    encodeStream,
+    encodeStreamPut,
+  )
+where
+
+import Control.Exception (Exception)
+import Data.Binary
+import Data.Binary.Get
+import Data.Binary.Put
+import qualified Data.ByteString as BS
+import qualified Data.ByteString.Lazy as BL
+import Streamly (SerialT)
+import Streamly.Internal.Data.Pipe.Types
+import Streamly.Internal.Prelude (transform)
+import qualified Streamly.Prelude as S
+
+-- | Decode stream of bytestrings given that there exists instance of 'Binary'
+-- for target type. Bytestrings do not have to be aligned in any way.
+decodeStream :: (Binary a, MonadFail m) => SerialT m BS.ByteString -> SerialT m a
+decodeStream = decodeStreamGet get
+
+-- | Decode stream of bytestrings using 'Get' from 'Binary'.
+-- Bytestrings do not have to be aligned in any way.
+decodeStreamGet :: MonadFail m => Get a -> SerialT m BS.ByteString -> SerialT m a
+decodeStreamGet g = transform $ Pipe consume (produce g) (runGetIncremental g)
+
+-- | Encode stream of elements to bytestrings given that there exists instance of 'Binary'
+-- for source type. Resulting bytestrings are not guaranteed to be aligned in any way.
+encodeStream :: (Binary a, MonadFail m) => SerialT m a -> SerialT m BS.ByteString
+encodeStream = encodeStreamPut put
+
+-- | Encode stream of elements using 'Put' from 'Binary'.
+-- Resulting bytestrings are not guaranteed to be aligned in any way.
+encodeStreamPut :: (MonadFail m) => (a -> Put) -> SerialT m a -> SerialT m BS.ByteString
+encodeStreamPut p = S.concatMap (S.fromList . BL.toChunks) . S.map (runPut . p)
+
+consume :: MonadFail m => Decoder a -> BS.ByteString -> m (Step (PipeState (Decoder a) (Decoder a)) a)
+consume d@Done {} input = return $ Continue (Produce $ pushChunk d input)
+consume (Partial f) input =
+  if BS.null input
+    then return (Continue (Consume (f Nothing)))
+    else return (Continue (Produce (f (Just input))))
+consume (Fail _ _ msg) _ = fail msg
+
+produce :: MonadFail m => Get a -> Decoder a -> m (Step (PipeState (Decoder a) (Decoder a)) a)
+produce g (Done unused _ output) =
+  if BS.null unused
+    then return $ Yield output (Consume (runGetIncremental g))
+    else return $ Yield output (Produce (runGetIncremental g `pushChunk` unused))
+produce _ d@(Partial _) = return $ Continue (Consume d)
+produce _ (Fail _ _ msg) = fail msg

          
A => stack.yaml +66 -0
@@ 0,0 1,66 @@ 
+# This file was automatically generated by 'stack init'
+#
+# Some commonly used options have been documented as comments in this file.
+# For advanced use and comprehensive documentation of the format, please see:
+# https://docs.haskellstack.org/en/stable/yaml_configuration/
+
+# Resolver to choose a 'specific' stackage snapshot or a compiler version.
+# A snapshot resolver dictates the compiler version and the set of packages
+# to be used for project dependencies. For example:
+#
+# resolver: lts-3.5
+# resolver: nightly-2015-09-21
+# resolver: ghc-7.10.2
+#
+# The location of a snapshot can be provided as a file or url. Stack assumes
+# a snapshot provided as a file might change, whereas a url resource does not.
+#
+# resolver: ./custom-snapshot.yaml
+# resolver: https://example.com/snapshots/2018-01-01.yaml
+resolver: lts-16.3
+
+# User packages to be built.
+# Various formats can be used as shown in the example below.
+#
+# packages:
+# - some-directory
+# - https://example.com/foo/bar/baz-0.0.2.tar.gz
+#   subdirs:
+#   - auto-update
+#   - wai
+packages:
+- .
+# Dependency packages to be pulled from upstream that are not in the resolver.
+# These entries can reference officially published versions as well as
+# forks / in-progress versions pinned to a git hash. For example:
+#
+# extra-deps:
+# - acme-missiles-0.3
+# - git: https://github.com/commercialhaskell/stack.git
+#   commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
+#
+# extra-deps: []
+
+# Override default flag values for local packages and extra-deps
+# flags: {}
+
+# Extra package databases containing global packages
+# extra-package-dbs: []
+
+# Control whether we use the GHC we find on the path
+# system-ghc: true
+#
+# Require a specific version of stack, using version ranges
+# require-stack-version: -any # Default
+# require-stack-version: ">=2.2"
+#
+# Override the architecture used by stack, especially useful on Windows
+# arch: i386
+# arch: x86_64
+#
+# Extra directories used by stack for building
+# extra-include-dirs: [/path/to/dir]
+# extra-lib-dirs: [/path/to/dir]
+#
+# Allow a newer minor version of GHC than the snapshot specifies
+# compiler-check: newer-minor

          
A => streamly-binary.cabal +43 -0
@@ 0,0 1,43 @@ 
+cabal-version:  >=1.10
+
+name:                streamly-binary
+version:             1.0.0.0
+synopsis:            Integration of streamly and binary
+description:         Support for encoding/decoding using @binary@ in @streamly@ streams.
+homepage:            https://sr.ht/~geyaeb/streamly-binary/
+bug-reports:         https://todo.sr.ht/~geyaeb/streamly-binary
+license:             BSD3
+license-file:        LICENSE
+author:              G. Eyaeb
+maintainer:          geyaeb@protonmail.com
+copyright:           2020 G. Eyaeb
+category:            Streamly, Streaming, Binary
+build-type:          Simple
+extra-source-files:  CHANGELOG.md, README.md
+
+source-repository head
+  type:             mercurial
+  location:         https://hg.sr.ht/~geyaeb/streamly-binary
+
+library
+  exposed-modules:  Streamly.Data.Binary
+  hs-source-dirs:   src
+  build-depends:    base >=4.7 && <5
+                  , binary == 0.8.*
+                  , bytestring == 0.10.*
+                  , streamly == 0.7.*
+  default-language: Haskell2010
+
+test-suite streamly-binary-test
+  type:             exitcode-stdio-1.0
+  main-is:          Spec.hs
+  hs-source-dirs:   test
+  ghc-options:      -threaded -rtsopts -with-rtsopts=-N
+  build-depends:    QuickCheck
+                  , base >=4.7 && <5
+                  , binary == 0.8.*
+                  , bytestring == 0.10.*
+                  , hspec == 2.7.*
+                  , streamly == 0.7.* 
+                  , streamly-binary
+  default-language: Haskell2010

          
A => test/Spec.hs +91 -0
@@ 0,0 1,91 @@ 
+{-# LANGUAGE BlockArguments #-}
+{-# LANGUAGE LambdaCase #-}
+
+import Control.Monad
+import Control.Monad.IO.Class
+import Data.Binary
+import Data.Binary.Get
+import Data.Binary.Put
+import Data.ByteString as BS
+import Data.Word
+import Streamly hiding (parallel)
+import Streamly.Data.Binary
+import qualified Streamly.Data.Fold as FL
+import qualified Streamly.Prelude as S
+import Test.Hspec
+import Test.QuickCheck
+import qualified Test.QuickCheck.Gen as Gen
+import Test.QuickCheck.Monadic
+
+-- One element of stream is one encoded Object
+prop_normal :: [Object] -> Property
+prop_normal objs = monadicIO do
+  rt <-
+    run $
+      S.toList $
+        decodeStream $ encodeStream $ S.fromList objs
+  return $ rt === objs
+
+-- One element of stream is one byte (represented as bytestring)
+prop_single_bytes :: [Object] -> Property
+prop_single_bytes objs = monadicIO do
+  rt <-
+    run $
+      S.toList $
+        decodeStream
+          (encodeStream (S.fromList objs) >>= S.fromList . fmap BS.singleton . BS.unpack)
+  return $ rt === objs
+
+-- One element of stream is all encoded objects concatenated as one big bytestring
+prop_one_bytestring :: [Object] -> Property
+prop_one_bytestring objs = monadicIO do
+  rt <-
+    run $
+      S.toList $
+        decodeStream $
+          S.yieldM (S.fold FL.mconcat $ encodeStream $ S.fromList objs)
+  return $ rt === objs
+
+main :: IO ()
+main =
+  hspec $
+    parallel $
+      describe "Object" do
+        it "decode . encode === id" $ property \obj ->
+          obj === decode (encode (obj :: Object))
+        it "works in stream" $ property prop_normal
+        it "works in stream" $ property prop_single_bytes
+        it "works in stream" $ property prop_one_bytestring
+
+data Object
+  = ObjectNil
+  | ObjectNum Word32
+  | ObjectStr String
+  | ObjectBin ByteString
+  | ObjectMap [(Object, Object)]
+  deriving (Show, Eq)
+
+instance Arbitrary Object where
+  arbitrary = Gen.sized \n ->
+    Gen.oneof
+      [ pure ObjectNil,
+        ObjectNum <$> arbitrary,
+        ObjectStr <$> arbitrary,
+        ObjectBin <$> (pack <$> arbitrary),
+        ObjectMap <$> Gen.resize (n `div` 4) arbitrary
+      ]
+
+instance Binary Object where
+  put ObjectNil = putWord8 0
+  put (ObjectNum w) = putWord8 1 >> putWord32be w
+  put (ObjectStr s) = putWord8 2 >> put s
+  put (ObjectBin b) = putWord8 3 >> put b
+  put (ObjectMap m) = putWord8 4 >> putList m
+  get =
+    getWord8 >>= \case
+      0 -> pure ObjectNil
+      1 -> ObjectNum <$> getWord32be
+      2 -> ObjectStr <$> get
+      3 -> ObjectBin <$> get
+      4 -> ObjectMap <$> get
+      _ -> fail "unknown tag"