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

module DSV.ZipViewFold
  ( zipViewFold, zipViewFoldM
  ) where

import DSV.ByteString
import DSV.Fold
import DSV.Prelude
import DSV.Validation
import DSV.Vector
import DSV.ViewType
import DSV.ZipViewError
import DSV.ZipViewType

zipViewFold ::
    forall headerError rowError row result .
    ZipView headerError rowError row
    -> Fold (Validation rowError row) result
    -> Fold (Vector ByteString) (Validation (ZipViewError headerError) result)

zipViewFold :: ZipView headerError rowError row
-> Fold (Validation rowError row) result
-> Fold
     (Vector ByteString) (Validation (ZipViewError headerError) result)
zipViewFold (ZipView View
  headerError
  (Vector ByteString)
  (View rowError (Vector ByteString) row)
v1) (Fold x -> Validation rowError row -> x
step x
init x -> result
done) = (State headerError rowError row x
 -> Vector ByteString -> State headerError rowError row x)
-> State headerError rowError row x
-> (State headerError rowError row x
    -> Validation (ZipViewError headerError) result)
-> Fold
     (Vector ByteString) (Validation (ZipViewError headerError) result)
forall a b x. (x -> a -> x) -> x -> (x -> b) -> Fold a b
Fold State headerError rowError row x
-> Vector ByteString -> State headerError rowError row x
step' State headerError rowError row x
forall headerError rowError row acc.
State headerError rowError row acc
Init State headerError rowError row x
-> Validation (ZipViewError headerError) result
forall headerError rowError row.
State headerError rowError row x
-> Validation (ZipViewError headerError) result
done'
  where
    step' :: State headerError rowError row x
-> Vector ByteString -> State headerError rowError row x
step' = \case State headerError rowError row x
Init       ->  \Vector ByteString
x -> case View
  headerError
  (Vector ByteString)
  (View rowError (Vector ByteString) row)
-> Vector ByteString
-> Validation headerError (View rowError (Vector ByteString) row)
forall e a b. View e a b -> a -> Validation e b
applyView View
  headerError
  (Vector ByteString)
  (View rowError (Vector ByteString) row)
v1 Vector ByteString
x of Failure headerError
e  -> headerError -> State headerError rowError row x
forall headerError rowError row acc.
headerError -> State headerError rowError row acc
Fail headerError
e
                                                              Success View rowError (Vector ByteString) row
v2 -> View rowError (Vector ByteString) row
-> x -> State headerError rowError row x
forall headerError rowError row acc.
View rowError (Vector ByteString) row
-> acc -> State headerError rowError row acc
Go View rowError (Vector ByteString) row
v2 x
init
                  Fail headerError
e     ->  \Vector ByteString
_ -> headerError -> State headerError rowError row x
forall headerError rowError row acc.
headerError -> State headerError rowError row acc
Fail headerError
e
                  Go View rowError (Vector ByteString) row
v2 x
acc  ->  \Vector ByteString
x -> View rowError (Vector ByteString) row
-> x -> State headerError rowError row x
forall headerError rowError row acc.
View rowError (Vector ByteString) row
-> acc -> State headerError rowError row acc
Go View rowError (Vector ByteString) row
v2 (x -> Validation rowError row -> x
step x
acc (View rowError (Vector ByteString) row
-> Vector ByteString -> Validation rowError row
forall e a b. View e a b -> a -> Validation e b
applyView View rowError (Vector ByteString) row
v2 Vector ByteString
x))

    done' :: State headerError rowError row x
-> Validation (ZipViewError headerError) result
done' = \case State headerError rowError row x
Init       ->  ZipViewError headerError
-> Validation (ZipViewError headerError) result
forall err a. err -> Validation err a
Failure ZipViewError headerError
forall headerError. ZipViewError headerError
ZipViewError_Empty
                  Fail headerError
e     ->  ZipViewError headerError
-> Validation (ZipViewError headerError) result
forall err a. err -> Validation err a
Failure (headerError -> ZipViewError headerError
forall headerError. headerError -> ZipViewError headerError
ZipViewError_HeaderError headerError
e)
                  Go View rowError (Vector ByteString) row
_ x
acc   ->  result -> Validation (ZipViewError headerError) result
forall err a. a -> Validation err a
Success (x -> result
done x
acc)

zipViewFoldM ::
    forall m headerError rowError row result .
    Monad m =>
    ZipView headerError rowError row
    -> FoldM m (Validation rowError row) result
    -> FoldM m (Vector ByteString) (Validation (ZipViewError headerError) result)

zipViewFoldM :: ZipView headerError rowError row
-> FoldM m (Validation rowError row) result
-> FoldM
     m
     (Vector ByteString)
     (Validation (ZipViewError headerError) result)
zipViewFoldM (ZipView View
  headerError
  (Vector ByteString)
  (View rowError (Vector ByteString) row)
v1) (FoldM x -> Validation rowError row -> m x
step m x
init x -> m result
done) = (State headerError rowError row x
 -> Vector ByteString -> m (State headerError rowError row x))
-> m (State headerError rowError row x)
-> (State headerError rowError row x
    -> m (Validation (ZipViewError headerError) result))
-> FoldM
     m
     (Vector ByteString)
     (Validation (ZipViewError headerError) result)
forall (m :: * -> *) a b x.
(x -> a -> m x) -> m x -> (x -> m b) -> FoldM m a b
FoldM State headerError rowError row x
-> Vector ByteString -> m (State headerError rowError row x)
step' (State headerError rowError row x
-> m (State headerError rowError row x)
forall (m :: * -> *) a. Monad m => a -> m a
return State headerError rowError row x
forall headerError rowError row acc.
State headerError rowError row acc
Init) State headerError rowError row x
-> m (Validation (ZipViewError headerError) result)
forall headerError rowError row.
State headerError rowError row x
-> m (Validation (ZipViewError headerError) result)
done'
  where
    step' :: State headerError rowError row x
-> Vector ByteString -> m (State headerError rowError row x)
step' = \case State headerError rowError row x
Init       ->  \Vector ByteString
x -> case View
  headerError
  (Vector ByteString)
  (View rowError (Vector ByteString) row)
-> Vector ByteString
-> Validation headerError (View rowError (Vector ByteString) row)
forall e a b. View e a b -> a -> Validation e b
applyView View
  headerError
  (Vector ByteString)
  (View rowError (Vector ByteString) row)
v1 Vector ByteString
x of Failure headerError
e  -> State headerError rowError row x
-> m (State headerError rowError row x)
forall (m :: * -> *) a. Monad m => a -> m a
return (headerError -> State headerError rowError row x
forall headerError rowError row acc.
headerError -> State headerError rowError row acc
Fail headerError
e)
                                                              Success View rowError (Vector ByteString) row
v2 -> View rowError (Vector ByteString) row
-> x -> State headerError rowError row x
forall headerError rowError row acc.
View rowError (Vector ByteString) row
-> acc -> State headerError rowError row acc
Go View rowError (Vector ByteString) row
v2 (x -> State headerError rowError row x)
-> m x -> m (State headerError rowError row x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m x
init
                  Fail headerError
e     ->  \Vector ByteString
_ -> State headerError rowError row x
-> m (State headerError rowError row x)
forall (m :: * -> *) a. Monad m => a -> m a
return (headerError -> State headerError rowError row x
forall headerError rowError row acc.
headerError -> State headerError rowError row acc
Fail headerError
e)
                  Go View rowError (Vector ByteString) row
v2 x
acc  ->  \Vector ByteString
x -> View rowError (Vector ByteString) row
-> x -> State headerError rowError row x
forall headerError rowError row acc.
View rowError (Vector ByteString) row
-> acc -> State headerError rowError row acc
Go View rowError (Vector ByteString) row
v2 (x -> State headerError rowError row x)
-> m x -> m (State headerError rowError row x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> x -> Validation rowError row -> m x
step x
acc (View rowError (Vector ByteString) row
-> Vector ByteString -> Validation rowError row
forall e a b. View e a b -> a -> Validation e b
applyView View rowError (Vector ByteString) row
v2 Vector ByteString
x)

    done' :: State headerError rowError row x
-> m (Validation (ZipViewError headerError) result)
done' = \case State headerError rowError row x
Init       ->  Validation (ZipViewError headerError) result
-> m (Validation (ZipViewError headerError) result)
forall (m :: * -> *) a. Monad m => a -> m a
return (ZipViewError headerError
-> Validation (ZipViewError headerError) result
forall err a. err -> Validation err a
Failure ZipViewError headerError
forall headerError. ZipViewError headerError
ZipViewError_Empty)
                  Fail headerError
e     ->  Validation (ZipViewError headerError) result
-> m (Validation (ZipViewError headerError) result)
forall (m :: * -> *) a. Monad m => a -> m a
return (ZipViewError headerError
-> Validation (ZipViewError headerError) result
forall err a. err -> Validation err a
Failure (headerError -> ZipViewError headerError
forall headerError. headerError -> ZipViewError headerError
ZipViewError_HeaderError headerError
e))
                  Go View rowError (Vector ByteString) row
_ x
acc   ->  result -> Validation (ZipViewError headerError) result
forall err a. a -> Validation err a
Success (result -> Validation (ZipViewError headerError) result)
-> m result -> m (Validation (ZipViewError headerError) result)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> x -> m result
done x
acc

data State headerError rowError row acc =
    Init
  | Fail headerError
  | Go (View rowError (Vector ByteString) row) acc