{-# LANGUAGE CPP #-}
module Codec.Picture.InternalHelper ( runGet
                                    , runGetStrict
                                    , decode
                                    , getRemainingBytes
                                    , getRemainingLazyBytes ) where

import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as L
import Data.Binary( Binary( get ) )
import Data.Binary.Get( Get
                      , getRemainingLazyByteString
                      )
import qualified Data.Binary.Get as G

decode :: (Binary a) => B.ByteString -> Either String a
decode :: forall a. Binary a => ByteString -> Either String a
decode = Get a -> ByteString -> Either String a
forall a. Get a -> ByteString -> Either String a
runGetStrict Get a
forall t. Binary t => Get t
get

runGet :: Get a -> L.ByteString -> Either String a
runGet :: forall a. Get a -> ByteString -> Either String a
runGet Get a
act = Either (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
-> Either String a
forall {a} {b} {a} {a} {b} {b}.
Either (a, b, a) (a, b, b) -> Either a b
unpack (Either
   (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
 -> Either String a)
-> (ByteString
    -> Either
         (ByteString, ByteOffset, String) (ByteString, ByteOffset, a))
-> ByteString
-> Either String a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Get a
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
forall a.
Get a
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
G.runGetOrFail Get a
act
    where unpack :: Either (a, b, a) (a, b, b) -> Either a b
unpack (Left (a
_, b
_, a
str)) = a -> Either a b
forall a b. a -> Either a b
Left a
str
          unpack (Right (a
_, b
_, b
element)) = b -> Either a b
forall a b. b -> Either a b
Right b
element

runGetStrict :: Get a -> B.ByteString -> Either String a
runGetStrict :: forall a. Get a -> ByteString -> Either String a
runGetStrict Get a
act ByteString
buffer = Get a -> ByteString -> Either String a
forall a. Get a -> ByteString -> Either String a
runGet Get a
act (ByteString -> Either String a) -> ByteString -> Either String a
forall a b. (a -> b) -> a -> b
$ [ByteString] -> ByteString
L.fromChunks [ByteString
buffer]

getRemainingBytes :: Get B.ByteString
getRemainingBytes :: Get ByteString
getRemainingBytes = ByteString -> ByteString
L.toStrict (ByteString -> ByteString) -> Get ByteString -> Get ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get ByteString
getRemainingLazyByteString

getRemainingLazyBytes :: Get L.ByteString
getRemainingLazyBytes :: Get ByteString
getRemainingLazyBytes = Get ByteString
getRemainingLazyByteString