quiver-binary (empty) → 0.1.0.0
raw patch · 7 files changed
+256/−0 lines, 7 filesdep +QuickCheckdep +basedep +binarysetup-changed
Dependencies added: QuickCheck, base, binary, bytestring, hspec, quiver, quiver-binary, quiver-bytestring, transformers
Files
- LICENSE +20/−0
- README.md +17/−0
- Setup.hs +2/−0
- quiver-binary.cabal +50/−0
- src/Control/Quiver/Binary.hs +91/−0
- stack.yaml +37/−0
- test/PropTests.hs +39/−0
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2016 Ivan Lazar Miljenovic++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ README.md view
@@ -0,0 +1,17 @@+quiver-binary+=============++[](https://hackage.haskell.org/package/quiver-binary) [](https://travis-ci.org/ivan-m/quiver-binary)++This package provides inter-operability between the _[quiver]_+stream-processing library and the _[binary]_ serialisation library.++Using this you can efficiently encode/decode native Haskell values in+a streaming fashion.++This is especially useful when combined with the _[quiver-bytestring]_+library for I/O.++[quiver]: http://hackage.haskell.org/package/quiver+[binary]: https://hackage.haskell.org/package/binary+[quiver-bytestring]: http://hackage.haskell.org/package/quiver-bytestring
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ quiver-binary.cabal view
@@ -0,0 +1,50 @@+name: quiver-binary+version: 0.1.0.0+synopsis: Binary serialisation support for Quivers+description: Handling for the binary library within Quivers+license: MIT+license-file: LICENSE+author: Ivan Lazar Miljenovic+maintainer: Ivan.Miljenovic@gmail.com+-- copyright:+category: Control+build-type: Simple+extra-source-files: README.md+ , stack.yaml+cabal-version: >=1.10++tested-with: GHC == 7.10.2, GHC == 7.11.*++source-repository head+ type: git+ location: git://github.com/ivan-m/quiver-binary.git++library+ exposed-modules: Control.Quiver.Binary+ -- other-modules:+ build-depends: base >=4 && <5+ , binary >= 0.6 && < 0.9+ , bytestring+ , quiver == 1.1.*+ , quiver-bytestring+ hs-source-dirs: src+ default-language: Haskell2010++ ghc-options: -Wall++test-suite properties+ type: exitcode-stdio-1.0+ main-is: PropTests.hs+ build-depends: quiver-binary+ , base+ , quiver+ , transformers >= 0.4 && < 0.6++ , QuickCheck >= 2.5 && < 2.9+ -- Just to make it nicer to write+ , hspec >= 2.1 && < 2.3+ hs-source-dirs: test+ default-language: Haskell2010++ ghc-options: -Wall+ ghc-prof-options: -prof -auto
+ src/Control/Quiver/Binary.hs view
@@ -0,0 +1,91 @@+{-# LANGUAGE RankNTypes #-}+{- |+ Module : Control.Quiver.Binary+ Description : Support Binary inside Quiver+ Copyright : (c) Ivan Lazar Miljenovic+ License : MIT+ Maintainer : Ivan.Miljenovic@gmail.com++This module provides functions for encoding\/decoding values within a+Quiver.++For any I\/O operations, use the functions provided by the+<http://hackage.haskell.org/package/quiver-bytestring quiver-bytestring>+package.++ -}+module Control.Quiver.Binary (+ -- * Simple encoding\/decoding+ spput+ , spget+ -- * Conversions to\/from ByteStrings+ , spdecode+ , spdecodeL+ , spencode+ , spencodeL+ ) where++import Control.Quiver.ByteString+import Control.Quiver.SP++import Data.Binary+import Data.Binary.Get+import Data.Binary.Put++import Data.ByteString (ByteString)+import qualified Data.ByteString as B+import qualified Data.ByteString.Lazy as L++--------------------------------------------------------------------------------++-- | Encode all values.+spput :: (Binary a) => SConsumer a PutM e+spput = sptraverse_ put++-- | Decode all values.+spget :: (Binary a) => SProducer a Get e+spget = spuntilM isEmpty get++spuntilM :: (Monad m) => m Bool -> m a -> SProducer a m e+spuntilM chk prod = go+ where+ go = do done <- qlift chk+ if done+ then spcomplete+ else do a <- qlift prod+ a >:> go++--------------------------------------------------------------------------------++-- | Decode all values from the provided stream of strict ByteStrings. Note+-- that the error message does not return the 'ByteOffset' from the+-- 'Decoder' as it will probably not match the actual location of the+-- source ByteString.+spdecode :: (Binary a, Monad m) => SP ByteString a m String+spdecode = runDecode nextD+ where+ nextD = runGetIncremental get++ runDecode d = case d of+ Fail b' _ err -> if B.null b'+ then spcomplete -- No more input! (should probably also check the offset)+ else spfailed err+ Partial p -> spfetch >>= runDecode . p+ Done b' _ a -> a >:> runDecode (nextD `pushChunk` b')++-- TODO: consider using state internally to keep track of the offset throughout the entire thing++-- | Decode all values from the provided stream of lazy ByteStrings. Note+-- that the error message does not return the 'ByteOffset' from the+-- 'Decoder' as it will probably not match the actual location of the+-- source ByteString.+spdecodeL :: forall a m. (Binary a, Monad m) => SP L.ByteString a m String+spdecodeL = toChunks >->> spdecode >&> snd++-- | Encode all values to a stream of strict ByteStrings.+spencode :: (Binary a, Functor m) => SP a ByteString m ()+spencode = spencodeL >->> toChunks >&> fst++-- | Encode all values to a stream of lazy ByteStrings.+spencodeL :: (Binary a) => SP a L.ByteString m ()+spencodeL = sppure encode
+ stack.yaml view
@@ -0,0 +1,37 @@+# For more information, see: http://docs.haskellstack.org/en/stable/yaml_configuration.html++# Specifies the GHC version and set of packages available (e.g., lts-3.5, nightly-2015-09-21, ghc-7.10.2)+resolver: lts-5.10++# Local packages, usually specified by relative directory name+packages:+- '.'++# Packages to be pulled from upstream that are not in the resolver (e.g., acme-missiles-0.3)+extra-deps:+- quiver-1.1.3+- quiver-bytestring-1.0.0++# 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: >= 1.0.0++# 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
+ test/PropTests.hs view
@@ -0,0 +1,39 @@+{-# LANGUAGE RankNTypes #-}+{- |+ Module : Main+ Description : Properties for quiver-binary+ Copyright : (c) Ivan Lazar Miljenovic+ License : MIT+ Maintainer : Ivan.Miljenovic@gmail.com++++ -}+module Main (main) where++import Control.Quiver.Binary++import Control.Quiver.SP+import Data.Functor.Identity++import Test.Hspec+import Test.Hspec.QuickCheck (prop)+import Test.QuickCheck++--------------------------------------------------------------------------------++main :: IO ()+main = hspec $+ describe "decode . encode = id" $ do+ prop "strict ByteStrings" $+ forAllShrink (arbitrary :: Gen [Int]) shrink $ idProp spencode spdecode+ prop "lazy ByteStrings" $+ forAllShrink (arbitrary :: Gen [Int]) shrink $ idProp spencodeL spdecodeL++idProp :: (Eq a) => SP a b Identity () -> SP b a Identity String -> [a] -> Bool+idProp enc dec as = runIdentity (sprun pipeline) == as+ where+ pipeline = spevery as >->> enc >->> dec >->> spToList >&> snd++spToList :: SQ a x f [a]+spToList = spfoldr (:) []