{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleInstances  #-}
{-# LANGUAGE LambdaCase         #-}
{-# LANGUAGE RankNTypes         #-}

-- | This module provides internal utilities and it is likely
-- to be modified in backwards-incompatible ways in the future.
--
-- Use the stable API exported by the "Pipes.Aeson" module instead.
module Pipes.Aeson.Internal
  ( DecodingError(..)
  , consecutively
  , decodeL
  , loopL
  ) where
import           Control.Exception                (Exception)
import qualified Control.Monad.Trans.State.Strict as S
import qualified Data.Aeson                       as Ae
import qualified Data.Attoparsec.Types            as Attoparsec
import qualified Data.ByteString                  as B
import qualified Data.ByteString.Internal         as B (isSpaceWord8)
import           Data.Data                        (Data, Typeable)
import           Data.Function                    (fix)
import           Pipes
import qualified Pipes.Attoparsec                 as PA
import qualified Pipes.Parse                      as Pipes

--------------------------------------------------------------------------------

-- | An error while decoding a JSON value.
data DecodingError
  = AttoparsecError PA.ParsingError
    -- ^An @attoparsec@ error that happened while parsing the raw JSON string.
  | FromJSONError Ae.Value String
    -- ^An @aeson@ error that happened while trying to convert the given
    -- 'Data.Aeson.Value' to an 'Ae.FromJSON' instance, as reported by
    -- 'Data.Aeson.Error'.
  deriving (Int -> DecodingError -> ShowS
[DecodingError] -> ShowS
DecodingError -> String
(Int -> DecodingError -> ShowS)
-> (DecodingError -> String)
-> ([DecodingError] -> ShowS)
-> Show DecodingError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DecodingError -> ShowS
showsPrec :: Int -> DecodingError -> ShowS
$cshow :: DecodingError -> String
show :: DecodingError -> String
$cshowList :: [DecodingError] -> ShowS
showList :: [DecodingError] -> ShowS
Show, DecodingError -> DecodingError -> Bool
(DecodingError -> DecodingError -> Bool)
-> (DecodingError -> DecodingError -> Bool) -> Eq DecodingError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DecodingError -> DecodingError -> Bool
== :: DecodingError -> DecodingError -> Bool
$c/= :: DecodingError -> DecodingError -> Bool
/= :: DecodingError -> DecodingError -> Bool
Eq, Typeable DecodingError
Typeable DecodingError =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> DecodingError -> c DecodingError)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c DecodingError)
-> (DecodingError -> Constr)
-> (DecodingError -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c DecodingError))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c DecodingError))
-> ((forall b. Data b => b -> b) -> DecodingError -> DecodingError)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> DecodingError -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> DecodingError -> r)
-> (forall u. (forall d. Data d => d -> u) -> DecodingError -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> DecodingError -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> DecodingError -> m DecodingError)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> DecodingError -> m DecodingError)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> DecodingError -> m DecodingError)
-> Data DecodingError
DecodingError -> Constr
DecodingError -> DataType
(forall b. Data b => b -> b) -> DecodingError -> DecodingError
forall a.
Typeable a =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> DecodingError -> u
forall u. (forall d. Data d => d -> u) -> DecodingError -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> DecodingError -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> DecodingError -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> DecodingError -> m DecodingError
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> DecodingError -> m DecodingError
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c DecodingError
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> DecodingError -> c DecodingError
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c DecodingError)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c DecodingError)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> DecodingError -> c DecodingError
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> DecodingError -> c DecodingError
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c DecodingError
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c DecodingError
$ctoConstr :: DecodingError -> Constr
toConstr :: DecodingError -> Constr
$cdataTypeOf :: DecodingError -> DataType
dataTypeOf :: DecodingError -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c DecodingError)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c DecodingError)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c DecodingError)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c DecodingError)
$cgmapT :: (forall b. Data b => b -> b) -> DecodingError -> DecodingError
gmapT :: (forall b. Data b => b -> b) -> DecodingError -> DecodingError
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> DecodingError -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> DecodingError -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> DecodingError -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> DecodingError -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> DecodingError -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> DecodingError -> [u]
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> DecodingError -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> DecodingError -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> DecodingError -> m DecodingError
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> DecodingError -> m DecodingError
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> DecodingError -> m DecodingError
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> DecodingError -> m DecodingError
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> DecodingError -> m DecodingError
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> DecodingError -> m DecodingError
Data, Typeable)

instance Exception DecodingError

--------------------------------------------------------------------------------

-- | Consecutively parse 'a' elements from the given 'Producer' using the given
-- parser (such as 'Pipes.Aeson.decode' or 'Pipes.Aeson.parseValue'), skipping
-- any leading whitespace each time.
--
-- This 'Producer' runs until it either runs out of input or until a decoding
-- failure occurs, in which case it returns 'Left' with a 'DecodingError' and
-- a 'Producer' with any leftovers. You can use 'Pipes.Lift.exceptP' to turn the
-- 'Either' return value into an 'Control.Monad.Trans.ExceptT'
-- monad transformer.
consecutively
  :: Monad m
  => Pipes.Parser B.ByteString m (Maybe (Either e a))
  -> Producer B.ByteString m r  -- ^Producer from which to draw raw input.
  -> Producer a m (Either (e, Producer B.ByteString m r) r)
consecutively :: forall (m :: * -> *) e a r.
Monad m =>
Parser ByteString m (Maybe (Either e a))
-> Producer ByteString m r
-> Producer a m (Either (e, Producer ByteString m r) r)
consecutively Parser ByteString m (Maybe (Either e a))
parser = Producer ByteString m r
-> Proxy X () () a m (Either (e, Producer ByteString m r) r)
forall {x} {x'} {x}.
Producer ByteString m x
-> Proxy x' x () a m (Either (e, Producer ByteString m x) x)
step where
    step :: Producer ByteString m x
-> Proxy x' x () a m (Either (e, Producer ByteString m x) x)
step Producer ByteString m x
p0 = do
      Either x (ByteString, Producer ByteString m x)
x <- m (Either x (ByteString, Producer ByteString m x))
-> Proxy
     x' x () a m (Either x (ByteString, Producer ByteString m x))
forall (m :: * -> *) a. Monad m => m a -> Proxy x' x () a m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either x (ByteString, Producer ByteString m x))
 -> Proxy
      x' x () a m (Either x (ByteString, Producer ByteString m x)))
-> m (Either x (ByteString, Producer ByteString m x))
-> Proxy
     x' x () a m (Either x (ByteString, Producer ByteString m x))
forall a b. (a -> b) -> a -> b
$ Producer ByteString m x
-> m (Either x (ByteString, Producer ByteString m x))
forall (m :: * -> *) r.
Monad m =>
Producer ByteString m r
-> m (Either r (ByteString, Producer ByteString m r))
nextSkipBlank Producer ByteString m x
p0
      case Either x (ByteString, Producer ByteString m x)
x of
         Left x
r -> Either (e, Producer ByteString m x) x
-> Proxy x' x () a m (Either (e, Producer ByteString m x) x)
forall a. a -> Proxy x' x () a m a
forall (m :: * -> *) a. Monad m => a -> m a
return (x -> Either (e, Producer ByteString m x) x
forall a b. b -> Either a b
Right x
r)
         Right (ByteString
bs, Producer ByteString m x
p1) -> do
            (Maybe (Either e a)
mea, Producer ByteString m x
p2) <- m (Maybe (Either e a), Producer ByteString m x)
-> Proxy x' x () a m (Maybe (Either e a), Producer ByteString m x)
forall (m :: * -> *) a. Monad m => m a -> Proxy x' x () a m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Maybe (Either e a), Producer ByteString m x)
 -> Proxy x' x () a m (Maybe (Either e a), Producer ByteString m x))
-> m (Maybe (Either e a), Producer ByteString m x)
-> Proxy x' x () a m (Maybe (Either e a), Producer ByteString m x)
forall a b. (a -> b) -> a -> b
$ StateT (Producer ByteString m x) m (Maybe (Either e a))
-> Producer ByteString m x
-> m (Maybe (Either e a), Producer ByteString m x)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
S.runStateT StateT (Producer ByteString m x) m (Maybe (Either e a))
Parser ByteString m (Maybe (Either e a))
parser (ByteString -> Proxy X () () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield ByteString
bs Proxy X () () ByteString m ()
-> Producer ByteString m x -> Producer ByteString m x
forall a b.
Proxy X () () ByteString m a
-> Proxy X () () ByteString m b -> Proxy X () () ByteString m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Producer ByteString m x
p1)
            case Maybe (Either e a)
mea of
               Just (Right a
a) -> a -> Proxy x' x () a m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield a
a Proxy x' x () a m ()
-> Proxy x' x () a m (Either (e, Producer ByteString m x) x)
-> Proxy x' x () a m (Either (e, Producer ByteString m x) x)
forall a b.
Proxy x' x () a m a -> Proxy x' x () a m b -> Proxy x' x () a m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Producer ByteString m x
-> Proxy x' x () a m (Either (e, Producer ByteString m x) x)
step Producer ByteString m x
p2
               Just (Left  e
e) -> Either (e, Producer ByteString m x) x
-> Proxy x' x () a m (Either (e, Producer ByteString m x) x)
forall a. a -> Proxy x' x () a m a
forall (m :: * -> *) a. Monad m => a -> m a
return ((e, Producer ByteString m x)
-> Either (e, Producer ByteString m x) x
forall a b. a -> Either a b
Left (e
e, Producer ByteString m x
p2))
               Maybe (Either e a)
Nothing -> String -> Proxy x' x () a m (Either (e, Producer ByteString m x) x)
forall a. HasCallStack => String -> a
error String
"Pipes.Aeson.Internal.consecutively: impossible"
{-# INLINABLE consecutively #-}


-- | Decodes a 'Ae.FromJSON' value from the underlying state using the given
-- 'Attoparsec.Parser' in order to obtain an 'Ae.Value' first.
--
-- It returns 'Nothing' if the underlying 'Producer' is exhausted, otherwise
-- it returns either the decoded entity or a 'I.DecodingError' in case of error.
decodeL
  :: (Monad m, Ae.FromJSON a)
  => Attoparsec.Parser B.ByteString Ae.Value
  -> Pipes.Parser B.ByteString m (Maybe (Either DecodingError (Int, a))) -- ^
decodeL :: forall (m :: * -> *) a.
(Monad m, FromJSON a) =>
Parser ByteString Value
-> Parser ByteString m (Maybe (Either DecodingError (Int, a)))
decodeL Parser ByteString Value
parser = do
    Maybe (Either ParsingError (Int, Value))
mev <- Parser ByteString Value
-> Parser ByteString m (Maybe (Either ParsingError (Int, Value)))
forall (m :: * -> *) a b.
(Monad m, ParserInput a) =>
Parser a b -> Parser a m (Maybe (Either ParsingError (Int, b)))
PA.parseL Parser ByteString Value
parser
    Maybe (Either DecodingError (Int, a))
-> StateT
     (Producer ByteString m x) m (Maybe (Either DecodingError (Int, a)))
forall a. a -> StateT (Producer ByteString m x) m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Either DecodingError (Int, a))
 -> StateT
      (Producer ByteString m x)
      m
      (Maybe (Either DecodingError (Int, a))))
-> Maybe (Either DecodingError (Int, a))
-> StateT
     (Producer ByteString m x) m (Maybe (Either DecodingError (Int, a)))
forall a b. (a -> b) -> a -> b
$ case Maybe (Either ParsingError (Int, Value))
mev of
       Maybe (Either ParsingError (Int, Value))
Nothing             -> Maybe (Either DecodingError (Int, a))
forall a. Maybe a
Nothing
       Just (Left ParsingError
l)       -> Either DecodingError (Int, a)
-> Maybe (Either DecodingError (Int, a))
forall a. a -> Maybe a
Just (DecodingError -> Either DecodingError (Int, a)
forall a b. a -> Either a b
Left (ParsingError -> DecodingError
AttoparsecError ParsingError
l))
       Just (Right (Int
n, Value
v)) -> case Value -> Result a
forall a. FromJSON a => Value -> Result a
Ae.fromJSON Value
v of
          Ae.Error String
e   -> Either DecodingError (Int, a)
-> Maybe (Either DecodingError (Int, a))
forall a. a -> Maybe a
Just (DecodingError -> Either DecodingError (Int, a)
forall a b. a -> Either a b
Left (Value -> String -> DecodingError
FromJSONError Value
v String
e))
          Ae.Success a
a -> Either DecodingError (Int, a)
-> Maybe (Either DecodingError (Int, a))
forall a. a -> Maybe a
Just ((Int, a) -> Either DecodingError (Int, a)
forall a b. b -> Either a b
Right (Int
n, a
a))
{-# INLINABLE decodeL #-}

--------------------------------------------------------------------------------

-- | Repeteadly try to parse raw JSON bytes into @a@ values, reporting any
-- 'I.DecodingError's downstream as they happen.
loopL
  :: (Monad m, Ae.FromJSON a)
  => Attoparsec.Parser B.ByteString Ae.Value
  -> (Pipes.Producer B.ByteString m r -> Pipes.Producer B.ByteString m r)
  -- ^ In case of 'AttoparsecError', this function will be called to modify
  -- the leftovers 'Pipes.Producer' before using it.
  --
  -- Ideally you will want to drop everything until the beginning of the next
  -- JSON element. This is easy to accomplish if there is a clear whitespace
  -- delimiter between the JSON elements, such as a newline (i.e.,
  -- @'Pipes.ByteString.drop' 1 . 'Pipes.ByteString.dropWhile' (/= 0xA)@).
  -- However, it can be hard to do correctly is there is no such delimiter.
  -- Skipping the first character (i.e., @'Pipes.ByteString.drop' 1@) should be
  -- sufficient in most cases, but not when parsing recursive data structures
  -- because you can accidentally parse a child in its parent's stead.
  --
  -- Notice that unless you advance the 'Pipes.Producer' somehow, 'loopL' will
  -- never terminate.
  -> Pipes.Producer B.ByteString m r
  -- ^ Raw JSON input.
  -> Pipes.Proxy x' x () (Either DecodingError (Int, a)) m r
{-# INLINABLE loopL #-}
loopL :: forall (m :: * -> *) a r x' x.
(Monad m, FromJSON a) =>
Parser ByteString Value
-> (Producer ByteString m r -> Producer ByteString m r)
-> Producer ByteString m r
-> Proxy x' x () (Either DecodingError (Int, a)) m r
loopL Parser ByteString Value
parser Producer ByteString m r -> Producer ByteString m r
fp = ((Producer ByteString m r
  -> Proxy x' x () (Either DecodingError (Int, a)) m r)
 -> Producer ByteString m r
 -> Proxy x' x () (Either DecodingError (Int, a)) m r)
-> Producer ByteString m r
-> Proxy x' x () (Either DecodingError (Int, a)) m r
forall a. (a -> a) -> a
fix (((Producer ByteString m r
   -> Proxy x' x () (Either DecodingError (Int, a)) m r)
  -> Producer ByteString m r
  -> Proxy x' x () (Either DecodingError (Int, a)) m r)
 -> Producer ByteString m r
 -> Proxy x' x () (Either DecodingError (Int, a)) m r)
-> ((Producer ByteString m r
     -> Proxy x' x () (Either DecodingError (Int, a)) m r)
    -> Producer ByteString m r
    -> Proxy x' x () (Either DecodingError (Int, a)) m r)
-> Producer ByteString m r
-> Proxy x' x () (Either DecodingError (Int, a)) m r
forall a b. (a -> b) -> a -> b
$ \Producer ByteString m r
-> Proxy x' x () (Either DecodingError (Int, a)) m r
k Producer ByteString m r
p0 -> do
   (Maybe (Either DecodingError (Int, a))
ye, Producer ByteString m r
p1) <- m (Maybe (Either DecodingError (Int, a)), Producer ByteString m r)
-> Proxy
     x'
     x
     ()
     (Either DecodingError (Int, a))
     m
     (Maybe (Either DecodingError (Int, a)), Producer ByteString m r)
forall (m :: * -> *) a.
Monad m =>
m a -> Proxy x' x () (Either DecodingError (Int, a)) m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (StateT
  (Producer ByteString m r) m (Maybe (Either DecodingError (Int, a)))
-> Producer ByteString m r
-> m (Maybe (Either DecodingError (Int, a)),
      Producer ByteString m r)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
S.runStateT (Parser ByteString Value
-> Parser ByteString m (Maybe (Either DecodingError (Int, a)))
forall (m :: * -> *) a.
(Monad m, FromJSON a) =>
Parser ByteString Value
-> Parser ByteString m (Maybe (Either DecodingError (Int, a)))
decodeL Parser ByteString Value
parser) Producer ByteString m r
p0)
   case Maybe (Either DecodingError (Int, a))
ye of
      Just (Left e :: DecodingError
e@AttoparsecError{}) -> do
         Either DecodingError (Int, a)
-> Proxy x' x () (Either DecodingError (Int, a)) m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
Pipes.yield (DecodingError -> Either DecodingError (Int, a)
forall a b. a -> Either a b
Left DecodingError
e)
         Producer ByteString m r
-> Proxy x' x () (Either DecodingError (Int, a)) m r
k (Producer ByteString m r -> Producer ByteString m r
fp Producer ByteString m r
p1)
      Just Either DecodingError (Int, a)
ea -> Either DecodingError (Int, a)
-> Proxy x' x () (Either DecodingError (Int, a)) m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
Pipes.yield Either DecodingError (Int, a)
ea Proxy x' x () (Either DecodingError (Int, a)) m ()
-> Proxy x' x () (Either DecodingError (Int, a)) m r
-> Proxy x' x () (Either DecodingError (Int, a)) m r
forall a b.
Proxy x' x () (Either DecodingError (Int, a)) m a
-> Proxy x' x () (Either DecodingError (Int, a)) m b
-> Proxy x' x () (Either DecodingError (Int, a)) m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Producer ByteString m r
-> Proxy x' x () (Either DecodingError (Int, a)) m r
k Producer ByteString m r
p1
      Maybe (Either DecodingError (Int, a))
Nothing -> m (Either r (ByteString, Producer ByteString m r))
-> Proxy
     x'
     x
     ()
     (Either DecodingError (Int, a))
     m
     (Either r (ByteString, Producer ByteString m r))
forall (m :: * -> *) a.
Monad m =>
m a -> Proxy x' x () (Either DecodingError (Int, a)) m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (Producer ByteString m r
-> m (Either r (ByteString, Producer ByteString m r))
forall (m :: * -> *) a r.
Monad m =>
Producer a m r -> m (Either r (a, Producer a m r))
Pipes.next Producer ByteString m r
p1) Proxy
  x'
  x
  ()
  (Either DecodingError (Int, a))
  m
  (Either r (ByteString, Producer ByteString m r))
-> (Either r (ByteString, Producer ByteString m r)
    -> Proxy x' x () (Either DecodingError (Int, a)) m r)
-> Proxy x' x () (Either DecodingError (Int, a)) m r
forall a b.
Proxy x' x () (Either DecodingError (Int, a)) m a
-> (a -> Proxy x' x () (Either DecodingError (Int, a)) m b)
-> Proxy x' x () (Either DecodingError (Int, a)) m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
         Left r
r -> r -> Proxy x' x () (Either DecodingError (Int, a)) m r
forall a. a -> Proxy x' x () (Either DecodingError (Int, a)) m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure r
r
         Right (ByteString, Producer ByteString m r)
_ -> String -> Proxy x' x () (Either DecodingError (Int, a)) m r
forall a. HasCallStack => String -> a
error String
"Pipes.Aeson.Internal.loopL: impossible"

--------------------------------------------------------------------------------
-- Internal stuff

-- | Like 'Pipes.next', except it skips leading whitespace and 'B.null' chunks.
nextSkipBlank
  :: (Monad m)
  => Producer B.ByteString m r
  -> m (Either r (B.ByteString, Producer B.ByteString m r))
nextSkipBlank :: forall (m :: * -> *) r.
Monad m =>
Producer ByteString m r
-> m (Either r (ByteString, Producer ByteString m r))
nextSkipBlank = Producer ByteString m r
-> m (Either r (ByteString, Producer ByteString m r))
forall (m :: * -> *) r.
Monad m =>
Producer ByteString m r
-> m (Either r (ByteString, Producer ByteString m r))
go where
    go :: Producer ByteString m r
-> m (Either r (ByteString, Producer ByteString m r))
go Producer ByteString m r
p0 = do
      Either r (ByteString, Producer ByteString m r)
x <- Producer ByteString m r
-> m (Either r (ByteString, Producer ByteString m r))
forall (m :: * -> *) a r.
Monad m =>
Producer a m r -> m (Either r (a, Producer a m r))
next Producer ByteString m r
p0
      case Either r (ByteString, Producer ByteString m r)
x of
         Left  r
_      -> Either r (ByteString, Producer ByteString m r)
-> m (Either r (ByteString, Producer ByteString m r))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Either r (ByteString, Producer ByteString m r)
x
         Right (ByteString
a,Producer ByteString m r
p1) -> do
            let a' :: ByteString
a' = (Word8 -> Bool) -> ByteString -> ByteString
B.dropWhile Word8 -> Bool
B.isSpaceWord8 ByteString
a
            if ByteString -> Bool
B.null ByteString
a' then Producer ByteString m r
-> m (Either r (ByteString, Producer ByteString m r))
go Producer ByteString m r
p1
                         else Either r (ByteString, Producer ByteString m r)
-> m (Either r (ByteString, Producer ByteString m r))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ((ByteString, Producer ByteString m r)
-> Either r (ByteString, Producer ByteString m r)
forall a b. b -> Either a b
Right (ByteString
a', Producer ByteString m r
p1))
{-# INLINABLE nextSkipBlank #-}