-- |
-- Module      : Streamly.Internal.Data.Array.Generic
-- Copyright   : (c) 2019 Composewell Technologies
--
-- License     : BSD-3-Clause
-- Maintainer  : streamly@composewell.com
-- Portability : GHC
--
module Streamly.Internal.Data.Array.Generic
    (
    module Streamly.Internal.Data.Array.Generic.Type

    -- * Parsing Stream of Arrays
    , parse
    , parsePos
    , parseBreak
    , parseBreakPos
    )
where

import Streamly.Internal.Data.Parser (ParseError(..), ParseErrorPos(..))
import Streamly.Internal.Data.StreamK.Type (StreamK)

import qualified Streamly.Internal.Data.ParserDrivers as Drivers
import qualified Streamly.Internal.Data.ParserK.Type as ParserK

import Prelude hiding (Foldable(..), read)
import Streamly.Internal.Data.Array.Generic.Type

-------------------------------------------------------------------------------
-- ParserK Chunked Generic
-------------------------------------------------------------------------------

{-# INLINE parseBreak #-}
parseBreak
    :: forall m a b. Monad m
    => ParserK.ParserK (Array a) m b
    -> StreamK m (Array a)
    -> m (Either ParseError b, StreamK m (Array a))
parseBreak :: forall (m :: * -> *) a b.
Monad m =>
ParserK (Array a) m b
-> StreamK m (Array a)
-> m (Either ParseError b, StreamK m (Array a))
parseBreak = ParserK (Array a) m b
-> StreamK m (Array a)
-> m (Either ParseError b, StreamK m (Array a))
forall (m :: * -> *) a b.
Monad m =>
ParserK (Array a) m b
-> StreamK m (Array a)
-> m (Either ParseError b, StreamK m (Array a))
Drivers.parseBreakChunksGeneric

-- | Like 'parseBreak' but includes stream position information in the error
-- messages.
--
{-# INLINE parseBreakPos #-}
parseBreakPos
    :: forall m a b. Monad m
    => ParserK.ParserK (Array a) m b
    -> StreamK m (Array a)
    -> m (Either ParseErrorPos b, StreamK m (Array a))
parseBreakPos :: forall (m :: * -> *) a b.
Monad m =>
ParserK (Array a) m b
-> StreamK m (Array a)
-> m (Either ParseErrorPos b, StreamK m (Array a))
parseBreakPos = ParserK (Array a) m b
-> StreamK m (Array a)
-> m (Either ParseErrorPos b, StreamK m (Array a))
forall (m :: * -> *) a b.
Monad m =>
ParserK (Array a) m b
-> StreamK m (Array a)
-> m (Either ParseErrorPos b, StreamK m (Array a))
Drivers.parseBreakChunksGenericPos

{-# INLINE parse #-}
parse ::
       (Monad m)
    => ParserK.ParserK (Array a) m b
    -> StreamK m (Array a)
    -> m (Either ParseError b)
parse :: forall (m :: * -> *) a b.
Monad m =>
ParserK (Array a) m b
-> StreamK m (Array a) -> m (Either ParseError b)
parse ParserK (Array a) m b
f = ((Either ParseError b, StreamK m (Array a)) -> Either ParseError b)
-> m (Either ParseError b, StreamK m (Array a))
-> m (Either ParseError b)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Either ParseError b, StreamK m (Array a)) -> Either ParseError b
forall a b. (a, b) -> a
fst (m (Either ParseError b, StreamK m (Array a))
 -> m (Either ParseError b))
-> (StreamK m (Array a)
    -> m (Either ParseError b, StreamK m (Array a)))
-> StreamK m (Array a)
-> m (Either ParseError b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParserK (Array a) m b
-> StreamK m (Array a)
-> m (Either ParseError b, StreamK m (Array a))
forall (m :: * -> *) a b.
Monad m =>
ParserK (Array a) m b
-> StreamK m (Array a)
-> m (Either ParseError b, StreamK m (Array a))
parseBreak ParserK (Array a) m b
f

{-# INLINE parsePos #-}
parsePos ::
       (Monad m)
    => ParserK.ParserK (Array a) m b
    -> StreamK m (Array a)
    -> m (Either ParseErrorPos b)
parsePos :: forall (m :: * -> *) a b.
Monad m =>
ParserK (Array a) m b
-> StreamK m (Array a) -> m (Either ParseErrorPos b)
parsePos ParserK (Array a) m b
f = ((Either ParseErrorPos b, StreamK m (Array a))
 -> Either ParseErrorPos b)
-> m (Either ParseErrorPos b, StreamK m (Array a))
-> m (Either ParseErrorPos b)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Either ParseErrorPos b, StreamK m (Array a))
-> Either ParseErrorPos b
forall a b. (a, b) -> a
fst (m (Either ParseErrorPos b, StreamK m (Array a))
 -> m (Either ParseErrorPos b))
-> (StreamK m (Array a)
    -> m (Either ParseErrorPos b, StreamK m (Array a)))
-> StreamK m (Array a)
-> m (Either ParseErrorPos b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParserK (Array a) m b
-> StreamK m (Array a)
-> m (Either ParseErrorPos b, StreamK m (Array a))
forall (m :: * -> *) a b.
Monad m =>
ParserK (Array a) m b
-> StreamK m (Array a)
-> m (Either ParseErrorPos b, StreamK m (Array a))
parseBreakPos ParserK (Array a) m b
f