Skip to content

Conversation

imalsogreg
Copy link

Hi Renzo,
Gabriel gave me this function yesterday for recreating the old behaviour of decodeGetMany (from before the days of pipes-parse). He said it might be useful to have it in the library. What do you think?

@kvanbere
Copy link
Contributor

I've actually put together something like this for personal usage, because I prefer exceptions to Either (from decoded) -- it's easier for me to compose the smaller types.

data PacketParseException = PacketParseException DecodingError
    deriving (Typeable)

instance Show PacketParseException where
    show _ = "PacketParseException"

instance Exception PacketParseException


decoder :: Monad m
    => Get a
    -> Producer ByteString m r
    -> Producer a m r
decoder getter = decoder_
  where
    decoder_ p0 = do
        x <- lift (next p0)
        case x of
            Left r  -> return r
            Right (bs, p1) -> do
                (ea, p2) <- lift $ S.runStateT (decodeGet getter) (yield bs >> p1)
                case ea of
                    Left e  -> throw (PacketParseException e)
                    Right a -> yield a >> decoder_ p2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants