haskell: add Iteratee interface

This commit is contained in:
tanakh
2010-09-06 17:00:22 +09:00
parent 9e50ba6ec6
commit b75db110dc
3 changed files with 72 additions and 20 deletions

View File

@@ -0,0 +1,46 @@
--------------------------------------------------------------------
-- |
-- Module : Data.MessagePack.Iteratee
-- Copyright : (c) Hideyuki Tanaka, 2009-2010
-- License : BSD3
--
-- Maintainer: tanaka.hideyuki@gmail.com
-- Stability : experimental
-- Portability: portable
--
-- MessagePack Deserializer interface to @Data.Iteratee@
--
--------------------------------------------------------------------
module Data.MessagePack.Iteratee(
-- * Iteratee version of deserializer
getI,
-- * Convert Parser to Iteratee
parserToIteratee,
) where
import qualified Data.Attoparsec as A
import qualified Data.ByteString as B
import qualified Data.Iteratee as I
import Data.MessagePack.Parser
-- | Deserialize a value
getI :: (Monad m, ObjectGet a) => I.Iteratee B.ByteString m a
getI = parserToIteratee get
-- | Convert Parser to Iteratee
parserToIteratee :: Monad m => A.Parser a -> I.Iteratee B.ByteString m a
parserToIteratee p = I.icont (itr (A.parse p)) Nothing
where
itr pcont s = case s of
I.EOF _ ->
I.throwErr (I.setEOF s)
I.Chunk bs ->
case pcont bs of
A.Fail _ _ msg ->
I.throwErr (I.iterStrExc msg)
A.Partial cont ->
I.icont (itr cont) Nothing
A.Done remain ret ->
I.idone ret (I.Chunk remain)