{-# LANGUAGE CPP #-}

module Streamly.Compat.Text.Lazy
  ( chunkReader
  , reader

  , toChunks
  , unsafeFromChunks
  , unsafeFromChunksIO
  )
where

import Data.Word (Word8)
import Streamly.Data.Array (Array)
import System.IO.Unsafe (unsafeInterleaveIO)
import Streamly.Data.Stream (Stream)

-- Internal imports
import Data.Text.Internal.Lazy (Text(..), chunk)
import Streamly.Internal.Data.Stream (Step(..))
import Streamly.Internal.Data.Unfold (Unfold(..))

import qualified Streamly.Compat.Text as Strict
import qualified Streamly.Data.Array as Array
import qualified Streamly.Data.Unfold as Unfold
import qualified Streamly.Data.Stream as Stream

import Prelude hiding (read)

#if MIN_VERSION_streamly_core(0,3,0)
#define UNFOLD_EACH Unfold.unfoldEach
#else
#define UNFOLD_EACH Unfold.many
#endif

-- | Unfold a lazy 'Text' to a stream of 'Array' 'Words'.
{-# INLINE  chunkReader #-}
chunkReader :: Monad m => Unfold m Text (Array Word8)
chunkReader :: forall (m :: * -> *). Monad m => Unfold m Text (Array Word8)
chunkReader = (Text -> m (Step Text (Array Word8)))
-> (Text -> m Text) -> Unfold m Text (Array Word8)
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold Text -> m (Step Text (Array Word8))
forall {m :: * -> *}.
Monad m =>
Text -> m (Step Text (Array Word8))
step Text -> m Text
forall {a}. a -> m a
seed
  where
    seed :: a -> m a
seed = a -> m a
forall {a}. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
    step :: Text -> m (Step Text (Array Word8))
step (Chunk Text
bs Text
bl) = Step Text (Array Word8) -> m (Step Text (Array Word8))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step Text (Array Word8) -> m (Step Text (Array Word8)))
-> Step Text (Array Word8) -> m (Step Text (Array Word8))
forall a b. (a -> b) -> a -> b
$ Array Word8 -> Text -> Step Text (Array Word8)
forall s a. a -> s -> Step s a
Yield (Text -> Array Word8
Strict.toArray Text
bs) Text
bl
    step Text
Empty = Step Text (Array Word8) -> m (Step Text (Array Word8))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step Text (Array Word8)
forall s a. Step s a
Stop

-- | Unfold a lazy 'Text' to a stream of Word8
{-# INLINE reader #-}
reader :: Monad m => Unfold m Text Word8
reader :: forall (m :: * -> *). Monad m => Unfold m Text Word8
reader = UNFOLD_EACH Array.reader chunkReader

-- XXX Should this be called readChunks?
-- | Convert a lazy 'Text' to a serial stream of 'Array' 'Word8'.
{-# INLINE toChunks #-}
toChunks :: Monad m => Text -> Stream m (Array Word8)
toChunks :: forall (m :: * -> *). Monad m => Text -> Stream m (Array Word8)
toChunks = Unfold m Text (Array Word8) -> Text -> Stream m (Array Word8)
forall (m :: * -> *) a b.
Applicative m =>
Unfold m a b -> a -> Stream m b
Stream.unfold Unfold m Text (Array Word8)
forall (m :: * -> *). Monad m => Unfold m Text (Array Word8)
chunkReader

-- | Convert a serial stream of 'Array' 'Word8' to a lazy 'Text'.
--
-- This function is unsafe: the caller must ensure that each 'Array' 'Word8'
-- element in the stream is a valid UTF-8 encoding.
--
-- IMPORTANT NOTE: This function is lazy only for lazy monads
-- (e.g. Identity). For strict monads (e.g. /IO/) it consumes the entire input
-- before generating the output. For /IO/ monad please use unsafeFromChunksIO
-- instead.
--
-- For strict monads like /IO/ you could create a newtype wrapper to make the
-- monad bind operation lazy and lift the stream to that type using hoist, then
-- you can use this function to generate the text lazily. For example you can
-- wrap the /IO/ type to make the bind lazy like this:
--
-- @
-- newtype LazyIO a = LazyIO { runLazy :: IO a } deriving (Functor, Applicative)
--
-- liftToLazy :: IO a -> LazyIO a
-- liftToLazy = LazyIO
--
-- instance Monad LazyIO where
--   return = pure
--   LazyIO a >>= f = LazyIO (unsafeInterleaveIO a >>= unsafeInterleaveIO . runLazy . f)
-- @
--
-- /unsafeFromChunks/ can then be used as,
-- @
-- {-# INLINE unsafeFromChunksIO #-}
-- unsafeFromChunksIO :: Stream IO (Array Word8) -> IO Text
-- unsafeFromChunksIO str = runLazy (unsafeFromChunks (Stream.hoist liftToLazy str))
-- @
{-# INLINE unsafeFromChunks #-}
unsafeFromChunks :: Monad m => Stream m (Array Word8) -> m Text
unsafeFromChunks :: forall (m :: * -> *). Monad m => Stream m (Array Word8) -> m Text
unsafeFromChunks = (Text -> Text -> Text) -> Text -> Stream m Text -> m Text
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Stream m a -> m b
Stream.foldr Text -> Text -> Text
chunk Text
Empty (Stream m Text -> m Text)
-> (Stream m (Array Word8) -> Stream m Text)
-> Stream m (Array Word8)
-> m Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Array Word8 -> Text) -> Stream m (Array Word8) -> Stream m Text
forall a b. (a -> b) -> Stream m a -> Stream m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Array Word8 -> Text
Strict.unsafeFromArray

-- | Convert a serial stream of 'Array' 'Word8' to a lazy 'Text' in the
-- /IO/ monad.
{-# INLINE unsafeFromChunksIO #-}
unsafeFromChunksIO :: Stream IO (Array Word8) -> IO Text
unsafeFromChunksIO :: Stream IO (Array Word8) -> IO Text
unsafeFromChunksIO =
    -- Although the /IO/ monad is strict in nature we emulate laziness using
    -- 'unsafeInterleaveIO'.
    (Text -> IO Text -> IO Text)
-> IO Text -> Stream IO Text -> IO Text
forall (m :: * -> *) a b.
Monad m =>
(a -> m b -> m b) -> m b -> Stream m a -> m b
Stream.foldrM (\Text
x IO Text
b -> Text -> Text -> Text
chunk Text
x (Text -> Text) -> IO Text -> IO Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO Text -> IO Text
forall a. IO a -> IO a
unsafeInterleaveIO IO Text
b) (Text -> IO Text
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Text
Empty)
        (Stream IO Text -> IO Text)
-> (Stream IO (Array Word8) -> Stream IO Text)
-> Stream IO (Array Word8)
-> IO Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Array Word8 -> Text) -> Stream IO (Array Word8) -> Stream IO Text
forall a b. (a -> b) -> Stream IO a -> Stream IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Array Word8 -> Text
Strict.unsafeFromArray