{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE LambdaCase #-}

module DSV.FileStrictZipView
  ( zipViewDsvFileStrict
  , zipViewDsvFileStrictIgnoringAllErrors
  , zipViewDsvFileStrictThrowFirstError
  ) where

import DSV.DelimiterType
import DSV.Fold
import DSV.IO
import DSV.ParseStop
import DSV.Parsing
import DSV.Pipes
import DSV.Prelude
import DSV.Validation
import DSV.Vector
import DSV.ZipViewPipe
import DSV.ZipViewStop
import DSV.ZipViewType

zipViewDsvFileStrict ::
    forall m headerError rowError row .
    MonadIO m
    => Delimiter
        -- ^ What character separates input values, e.g. 'comma' or 'tab'
    -> FilePath
        -- ^ The path of a DSV file to read
    -> ZipView headerError rowError row
        -- ^ How to interpret the rows
    -> m (ZipViewStop headerError, Vector (Validation rowError row))

zipViewDsvFileStrict :: Delimiter
-> FilePath
-> ZipView headerError rowError row
-> m (ZipViewStop headerError, Vector (Validation rowError row))
zipViewDsvFileStrict Delimiter
d FilePath
fp ZipView headerError rowError row
lu =
    IO (ZipViewStop headerError, Vector (Validation rowError row))
-> m (ZipViewStop headerError, Vector (Validation rowError row))
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (ZipViewStop headerError, Vector (Validation rowError row))
 -> m (ZipViewStop headerError, Vector (Validation rowError row)))
-> IO (ZipViewStop headerError, Vector (Validation rowError row))
-> m (ZipViewStop headerError, Vector (Validation rowError row))
forall a b. (a -> b) -> a -> b
$ SafeT
  IO (ZipViewStop headerError, Vector (Validation rowError row))
-> IO (ZipViewStop headerError, Vector (Validation rowError row))
forall (m :: * -> *) r.
(MonadMask m, MonadIO m) =>
SafeT m r -> m r
runSafeT (SafeT
   IO (ZipViewStop headerError, Vector (Validation rowError row))
 -> IO (ZipViewStop headerError, Vector (Validation rowError row)))
-> SafeT
     IO (ZipViewStop headerError, Vector (Validation rowError row))
-> IO (ZipViewStop headerError, Vector (Validation rowError row))
forall a b. (a -> b) -> a -> b
$
      do
        FoldM
  (SafeT IO)
  (Validation rowError row)
  (Vector (Validation rowError row))
-> Producer
     (Validation rowError row) (SafeT IO) (ZipViewStop headerError)
-> SafeT
     IO (ZipViewStop headerError, Vector (Validation rowError row))
forall a b (m :: * -> *) r.
Monad m =>
FoldM m a b -> Producer a m r -> m (r, b)
foldProducerM FoldM
  (SafeT IO)
  (Validation rowError row)
  (Vector (Validation rowError row))
forall (v :: * -> *) (m :: * -> *) a.
(PrimMonad m, Vector v a) =>
FoldM m a (v a)
foldVectorM (Producer
   (Validation rowError row) (SafeT IO) (ZipViewStop headerError)
 -> SafeT
      IO (ZipViewStop headerError, Vector (Validation rowError row)))
-> Producer
     (Validation rowError row) (SafeT IO) (ZipViewStop headerError)
-> SafeT
     IO (ZipViewStop headerError, Vector (Validation rowError row))
forall a b. (a -> b) -> a -> b
$
            FilePath
-> IOMode
-> (Handle
    -> Producer
         (Validation rowError row) (SafeT IO) (ZipViewStop headerError))
-> Producer
     (Validation rowError row) (SafeT IO) (ZipViewStop headerError)
forall (m :: * -> *) r.
MonadSafe m =>
FilePath -> IOMode -> (Handle -> m r) -> m r
withFile FilePath
fp IOMode
ReadMode ((Handle
  -> Producer
       (Validation rowError row) (SafeT IO) (ZipViewStop headerError))
 -> Producer
      (Validation rowError row) (SafeT IO) (ZipViewStop headerError))
-> (Handle
    -> Producer
         (Validation rowError row) (SafeT IO) (ZipViewStop headerError))
-> Producer
     (Validation rowError row) (SafeT IO) (ZipViewStop headerError)
forall a b. (a -> b) -> a -> b
$ \Handle
h ->
                Handle
-> Proxy
     X () () (Vector ByteString) (SafeT IO) (ZipViewStop headerError)
forall (m :: * -> *) headerError.
MonadIO m =>
Handle
-> Proxy X () () (Vector ByteString) m (ZipViewStop headerError)
readRows Handle
h Proxy
  X () () (Vector ByteString) (SafeT IO) (ZipViewStop headerError)
-> Proxy
     ()
     (Vector ByteString)
     ()
     (Validation rowError row)
     (SafeT IO)
     (ZipViewStop headerError)
-> Producer
     (Validation rowError row) (SafeT IO) (ZipViewStop headerError)
forall (m :: * -> *) a' a b r c' c.
Monad m =>
Proxy a' a () b m r -> Proxy () b c' c m r -> Proxy a' a c' c m r
>-> Proxy
  ()
  (Vector ByteString)
  ()
  (Validation rowError row)
  (SafeT IO)
  (ZipViewStop headerError)
interpretRows

  where
    readRows :: Handle
-> Proxy X () () (Vector ByteString) m (ZipViewStop headerError)
readRows Handle
h =
      ((Natural, ParseStop) -> ZipViewStop headerError)
-> Proxy X () () (Vector ByteString) m (Natural, ParseStop)
-> Proxy X () () (Vector ByteString) m (ZipViewStop headerError)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
        (
          \case
            (Natural
_, ParseStop
ParseIncomplete) -> ZipViewStop headerError
forall headerError. ZipViewStop headerError
ZipViewParseError
            (Natural
0, ParseStop
ParseComplete) -> ZipViewStop headerError
forall headerError. ZipViewStop headerError
ZipViewEmpty
            (Natural
_, ParseStop
ParseComplete) -> ZipViewStop headerError
forall headerError. ZipViewStop headerError
ZipViewComplete
        )
        (Producer (Vector ByteString) m ParseStop
-> Proxy X () () (Vector ByteString) m (Natural, ParseStop)
forall a (m :: * -> *) r.
Monad m =>
Producer a m r -> Producer a m (Natural, r)
count (Delimiter -> Handle -> Producer (Vector ByteString) m ParseStop
forall (m :: * -> *).
MonadIO m =>
Delimiter -> Handle -> Producer (Vector ByteString) m ParseStop
handleDsvRowProducer Delimiter
d Handle
h))

    interpretRows :: Proxy
  ()
  (Vector ByteString)
  ()
  (Validation rowError row)
  (SafeT IO)
  (ZipViewStop headerError)
interpretRows =
      (headerError -> ZipViewStop headerError)
-> Proxy
     ()
     (Vector ByteString)
     ()
     (Validation rowError row)
     (SafeT IO)
     headerError
-> Proxy
     ()
     (Vector ByteString)
     ()
     (Validation rowError row)
     (SafeT IO)
     (ZipViewStop headerError)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap headerError -> ZipViewStop headerError
forall headerError. headerError -> ZipViewStop headerError
ZipViewHeaderError (ZipView headerError rowError row
-> Proxy
     ()
     (Vector ByteString)
     ()
     (Validation rowError row)
     (SafeT IO)
     headerError
forall (m :: * -> *) headerError rowError row.
Monad m =>
ZipView headerError rowError row
-> Pipe (Vector ByteString) (Validation rowError row) m headerError
zipViewPipe ZipView headerError rowError row
lu)

zipViewDsvFileStrictIgnoringAllErrors ::
    forall m headerError rowError row .
    MonadIO m
    => Delimiter
        -- ^ What character separates input values, e.g. 'comma' or 'tab'
    -> FilePath
        -- ^ The path of a DSV file to read
    -> ZipView headerError rowError row
        -- ^ How to interpret the rows
    -> m (Vector row)

zipViewDsvFileStrictIgnoringAllErrors :: Delimiter
-> FilePath -> ZipView headerError rowError row -> m (Vector row)
zipViewDsvFileStrictIgnoringAllErrors Delimiter
d FilePath
fp ZipView headerError rowError row
lu =
    (((), Vector row) -> Vector row)
-> m ((), Vector row) -> m (Vector row)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((), Vector row) -> Vector row
forall a b. (a, b) -> b
snd (m ((), Vector row) -> m (Vector row))
-> m ((), Vector row) -> m (Vector row)
forall a b. (a -> b) -> a -> b
$ IO ((), Vector row) -> m ((), Vector row)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO ((), Vector row) -> m ((), Vector row))
-> IO ((), Vector row) -> m ((), Vector row)
forall a b. (a -> b) -> a -> b
$ SafeT IO ((), Vector row) -> IO ((), Vector row)
forall (m :: * -> *) r.
(MonadMask m, MonadIO m) =>
SafeT m r -> m r
runSafeT (SafeT IO ((), Vector row) -> IO ((), Vector row))
-> SafeT IO ((), Vector row) -> IO ((), Vector row)
forall a b. (a -> b) -> a -> b
$
      do
        FoldM (SafeT IO) row (Vector row)
-> Producer row (SafeT IO) () -> SafeT IO ((), Vector row)
forall a b (m :: * -> *) r.
Monad m =>
FoldM m a b -> Producer a m r -> m (r, b)
foldProducerM FoldM (SafeT IO) row (Vector row)
forall (v :: * -> *) (m :: * -> *) a.
(PrimMonad m, Vector v a) =>
FoldM m a (v a)
foldVectorM (Producer row (SafeT IO) () -> SafeT IO ((), Vector row))
-> Producer row (SafeT IO) () -> SafeT IO ((), Vector row)
forall a b. (a -> b) -> a -> b
$
            FilePath
-> IOMode
-> (Handle -> Producer row (SafeT IO) ())
-> Producer row (SafeT IO) ()
forall (m :: * -> *) r.
MonadSafe m =>
FilePath -> IOMode -> (Handle -> m r) -> m r
withFile FilePath
fp IOMode
ReadMode ((Handle -> Producer row (SafeT IO) ())
 -> Producer row (SafeT IO) ())
-> (Handle -> Producer row (SafeT IO) ())
-> Producer row (SafeT IO) ()
forall a b. (a -> b) -> a -> b
$ \Handle
h ->
                Proxy X () () (Vector ByteString) (SafeT IO) ParseStop
-> Proxy X () () (Vector ByteString) (SafeT IO) ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Delimiter
-> Handle -> Proxy X () () (Vector ByteString) (SafeT IO) ParseStop
forall (m :: * -> *).
MonadIO m =>
Delimiter -> Handle -> Producer (Vector ByteString) m ParseStop
handleDsvRowProducer Delimiter
d Handle
h) Proxy X () () (Vector ByteString) (SafeT IO) ()
-> Proxy () (Vector ByteString) () row (SafeT IO) ()
-> Producer row (SafeT IO) ()
forall (m :: * -> *) a' a b r c' c.
Monad m =>
Proxy a' a () b m r -> Proxy () b c' c m r -> Proxy a' a c' c m r
>->
                ZipView headerError rowError row
-> Proxy () (Vector ByteString) () row (SafeT IO) ()
forall (m :: * -> *) headerError rowError row.
Monad m =>
ZipView headerError rowError row
-> Pipe (Vector ByteString) row m ()
zipViewPipeIgnoringAllErrors ZipView headerError rowError row
lu

zipViewDsvFileStrictThrowFirstError ::
    forall m headerError rowError row .
    ( MonadIO m
    , Exception headerError
    , Show rowError, Typeable rowError
    )
    => Delimiter
        -- ^ What character separates input values, e.g. 'comma' or 'tab'
    -> FilePath
        -- ^ The path of a DSV file to read
    -> ZipView headerError rowError row
        -- ^ How to interpret the rows
    -> m (Vector row)

zipViewDsvFileStrictThrowFirstError :: Delimiter
-> FilePath -> ZipView headerError rowError row -> m (Vector row)
zipViewDsvFileStrictThrowFirstError Delimiter
d FilePath
fp ZipView headerError rowError row
lu =
    (((), Vector row) -> Vector row)
-> m ((), Vector row) -> m (Vector row)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((), Vector row) -> Vector row
forall a b. (a, b) -> b
snd (m ((), Vector row) -> m (Vector row))
-> m ((), Vector row) -> m (Vector row)
forall a b. (a -> b) -> a -> b
$ IO ((), Vector row) -> m ((), Vector row)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO ((), Vector row) -> m ((), Vector row))
-> IO ((), Vector row) -> m ((), Vector row)
forall a b. (a -> b) -> a -> b
$ SafeT IO ((), Vector row) -> IO ((), Vector row)
forall (m :: * -> *) r.
(MonadMask m, MonadIO m) =>
SafeT m r -> m r
runSafeT (SafeT IO ((), Vector row) -> IO ((), Vector row))
-> SafeT IO ((), Vector row) -> IO ((), Vector row)
forall a b. (a -> b) -> a -> b
$
      do
        FoldM (SafeT IO) row (Vector row)
-> Producer row (SafeT IO) () -> SafeT IO ((), Vector row)
forall a b (m :: * -> *) r.
Monad m =>
FoldM m a b -> Producer a m r -> m (r, b)
foldProducerM FoldM (SafeT IO) row (Vector row)
forall (v :: * -> *) (m :: * -> *) a.
(PrimMonad m, Vector v a) =>
FoldM m a (v a)
foldVectorM (Producer row (SafeT IO) () -> SafeT IO ((), Vector row))
-> Producer row (SafeT IO) () -> SafeT IO ((), Vector row)
forall a b. (a -> b) -> a -> b
$
            FilePath
-> IOMode
-> (Handle -> Producer row (SafeT IO) ())
-> Producer row (SafeT IO) ()
forall (m :: * -> *) r.
MonadSafe m =>
FilePath -> IOMode -> (Handle -> m r) -> m r
withFile FilePath
fp IOMode
ReadMode ((Handle -> Producer row (SafeT IO) ())
 -> Producer row (SafeT IO) ())
-> (Handle -> Producer row (SafeT IO) ())
-> Producer row (SafeT IO) ()
forall a b. (a -> b) -> a -> b
$ \Handle
h ->
                Proxy X () () (Vector ByteString) (SafeT IO) ParseStop
-> Proxy X () () (Vector ByteString) (SafeT IO) ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Delimiter
-> Handle -> Proxy X () () (Vector ByteString) (SafeT IO) ParseStop
forall (m :: * -> *).
MonadIO m =>
Delimiter -> Handle -> Producer (Vector ByteString) m ParseStop
handleDsvRowProducer Delimiter
d Handle
h) Proxy X () () (Vector ByteString) (SafeT IO) ()
-> Proxy () (Vector ByteString) () row (SafeT IO) ()
-> Producer row (SafeT IO) ()
forall (m :: * -> *) a' a b r c' c.
Monad m =>
Proxy a' a () b m r -> Proxy () b c' c m r -> Proxy a' a c' c m r
>->
                ZipView headerError rowError row
-> Proxy () (Vector ByteString) () row (SafeT IO) ()
forall (m :: * -> *) headerError rowError row r.
(Monad m, MonadThrow m, Exception headerError, Show rowError,
 Typeable rowError) =>
ZipView headerError rowError row
-> Pipe (Vector ByteString) row m r
zipViewPipeThrowFirstError ZipView headerError rowError row
lu