potoki-cereal (empty) → 0.1
raw patch · 5 files changed
+167/−0 lines, 5 filesdep +basedep +base-preludedep +bytestringsetup-changed
Dependencies added: base, base-prelude, bytestring, cereal, potoki, potoki-core, text
Files
- LICENSE +22/−0
- Setup.hs +2/−0
- library/Potoki/Cereal/Prelude.hs +17/−0
- library/Potoki/Cereal/Transform.hs +74/−0
- potoki-cereal.cabal +52/−0
+ LICENSE view
@@ -0,0 +1,22 @@+Copyright (c) 2017, Nikita Volkov++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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ library/Potoki/Cereal/Prelude.hs view
@@ -0,0 +1,17 @@+module Potoki.Cereal.Prelude+( + module Exports,+)+where++-- base-prelude+-------------------------+import BasePrelude as Exports hiding (first, second)++-- bytestring+-------------------------+import Data.ByteString as Exports (ByteString)++-- text+-------------------------+import Data.Text as Exports (Text)
+ library/Potoki/Cereal/Transform.hs view
@@ -0,0 +1,74 @@+module Potoki.Cereal.Transform+(+ encode,+ decode,+)+where++import Potoki.Cereal.Prelude+import Potoki.Core.Transform+import qualified Potoki.Core.Fetch as A+import qualified Data.Serialize as C+++encode :: C.Serialize element => Transform element ByteString+encode =+ arr C.encode++decode :: C.Serialize element => Transform ByteString (Either Text element)+decode =+ runPartialDecoder (C.runGetPartial C.get)++{-# INLINE runPartialDecoder #-}+runPartialDecoder :: forall decoded. (ByteString -> C.Result decoded) -> Transform ByteString (Either Text decoded)+runPartialDecoder inputToResult =+ Transform $ \ inputFetch ->+ do+ unconsumedRef <- newIORef mempty+ finishedRef <- newIORef False+ return (A.Fetch (fetchParsed inputFetch finishedRef unconsumedRef))+ where+ fetchParsed :: A.Fetch ByteString -> IORef Bool -> IORef ByteString -> forall x. x -> (Either Text decoded -> x) -> IO x+ fetchParsed (A.Fetch inputFetchIO) finishedRef unconsumedRef nil just =+ do+ finished <- readIORef finishedRef+ if finished+ then return nil+ else do+ unconsumed <- readIORef unconsumedRef+ if unconsumed == mempty+ then+ join $ inputFetchIO+ (return nil)+ (\ input -> do+ if input == mempty+ then return nil+ else matchResult (inputToResult input))+ else do+ writeIORef unconsumedRef mempty+ matchResult (inputToResult unconsumed)+ where+ matchResult =+ \ case+ C.Partial inputToResult ->+ consume inputToResult+ C.Done decoded unconsumed ->+ do+ writeIORef unconsumedRef unconsumed+ return (just (Right decoded))+ C.Fail message unconsumed ->+ do+ writeIORef unconsumedRef unconsumed+ writeIORef finishedRef True+ return (just (Left resultMessage))+ where+ resultMessage =+ fromString message+ consume inputToResult =+ join $ inputFetchIO+ (do+ writeIORef finishedRef True+ matchResult (inputToResult mempty))+ (\ input -> do+ when (input == mempty) (writeIORef finishedRef True)+ matchResult (inputToResult input))
+ potoki-cereal.cabal view
@@ -0,0 +1,52 @@+name:+ potoki-cereal+version:+ 0.1+synopsis:+ Streaming serialization+category:+ Potoki, Streaming+homepage:+ https://github.com/nikita-volkov/potoki-cereal +bug-reports:+ https://github.com/nikita-volkov/potoki-cereal/issues +author:+ Nikita Volkov <nikita.y.volkov@mail.ru>+maintainer:+ Nikita Volkov <nikita.y.volkov@mail.ru>+copyright:+ (c) 2017, Nikita Volkov+license:+ MIT+license-file:+ LICENSE+build-type:+ Simple+cabal-version:+ >=1.10++source-repository head+ type:+ git+ location:+ git://github.com/nikita-volkov/potoki-cereal.git++library+ hs-source-dirs:+ library+ default-extensions:+ Arrows, BangPatterns, ConstraintKinds, DataKinds, DefaultSignatures, DeriveDataTypeable, DeriveFoldable, DeriveFunctor, DeriveGeneric, DeriveTraversable, EmptyDataDecls, FlexibleContexts, FlexibleInstances, FunctionalDependencies, GADTs, GeneralizedNewtypeDeriving, LambdaCase, LiberalTypeSynonyms, MagicHash, MultiParamTypeClasses, MultiWayIf, NoImplicitPrelude, NoMonomorphismRestriction, OverloadedStrings, PatternGuards, ParallelListComp, QuasiQuotes, RankNTypes, RecordWildCards, ScopedTypeVariables, StandaloneDeriving, TemplateHaskell, TupleSections, TypeFamilies, TypeOperators, UnboxedTuples+ default-language:+ Haskell2010+ exposed-modules:+ Potoki.Cereal.Transform+ other-modules:+ Potoki.Cereal.Prelude+ build-depends:+ potoki-core >=1.2 && <1.3,+ base >=4.7 && <5,+ base-prelude >=1 && <2,+ cereal >=0.5 && <0.6,+ bytestring >=0.10 && <0.11,+ potoki >=0.6.1 && <0.7,+ text >=1 && <2