{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleContexts #-}

module DSV.ZipViews
  ( byteStringZipView, byteStringZipViewPosition
  , textZipViewUtf8, textZipViewUtf8'
  , entireRowZipView
  ) where

import DSV.ByteString
import DSV.IndexError
import DSV.LookupError
import DSV.Numbers
import DSV.Position
import DSV.Prelude
import DSV.Text
import DSV.UTF8
import DSV.Validation
import DSV.Vector
import DSV.ViewType
import DSV.ZipViewType

-- base
import qualified Data.List as List

byteStringZipView
    :: ByteString
    -> ZipView LookupError TooShort ByteString

byteStringZipView :: ByteString -> ZipView LookupError TooShort ByteString
byteStringZipView ByteString
name =
    (View
  LookupError
  (Vector ByteString)
  (View TooShort (Vector ByteString) ByteString)
-> ZipView LookupError TooShort ByteString
forall headerError rowError a.
View
  headerError
  (Vector ByteString)
  (View rowError (Vector ByteString) a)
-> ZipView headerError rowError a
ZipView (View
   LookupError
   (Vector ByteString)
   (View TooShort (Vector ByteString) ByteString)
 -> ZipView LookupError TooShort ByteString)
-> ((Vector ByteString
     -> Validation
          LookupError (View TooShort (Vector ByteString) ByteString))
    -> View
         LookupError
         (Vector ByteString)
         (View TooShort (Vector ByteString) ByteString))
-> (Vector ByteString
    -> Validation
         LookupError (View TooShort (Vector ByteString) ByteString))
-> ZipView LookupError TooShort ByteString
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Vector ByteString
 -> Validation
      LookupError (View TooShort (Vector ByteString) ByteString))
-> View
     LookupError
     (Vector ByteString)
     (View TooShort (Vector ByteString) ByteString)
forall e a b. (a -> Validation e b) -> View e a b
View) ((Vector ByteString
  -> Validation
       LookupError (View TooShort (Vector ByteString) ByteString))
 -> ZipView LookupError TooShort ByteString)
-> (Vector ByteString
    -> Validation
         LookupError (View TooShort (Vector ByteString) ByteString))
-> ZipView LookupError TooShort ByteString
forall a b. (a -> b) -> a -> b
$ \Vector ByteString
header ->
    case (ByteString -> Bool) -> [ByteString] -> [Int]
forall a. (a -> Bool) -> [a] -> [Int]
List.findIndices (ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
name) (Vector ByteString -> [ByteString]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList Vector ByteString
header) of
        []  -> LookupError
-> Validation
     LookupError (View TooShort (Vector ByteString) ByteString)
forall err a. err -> Validation err a
Failure LookupError
LookupError_Missing
        [Int
i] -> Int
-> Validation
     LookupError (View TooShort (Vector ByteString) ByteString)
forall err a. Int -> Validation err (View TooShort (Vector a) a)
withI Int
i
        [Int]
_   -> LookupError
-> Validation
     LookupError (View TooShort (Vector ByteString) ByteString)
forall err a. err -> Validation err a
Failure LookupError
LookupError_Duplicate
  where
    withI :: Int -> Validation err (View TooShort (Vector a) a)
withI Int
i =
        (View TooShort (Vector a) a
-> Validation err (View TooShort (Vector a) a)
forall err a. a -> Validation err a
Success (View TooShort (Vector a) a
 -> Validation err (View TooShort (Vector a) a))
-> ((Vector a -> Validation TooShort a)
    -> View TooShort (Vector a) a)
-> (Vector a -> Validation TooShort a)
-> Validation err (View TooShort (Vector a) a)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Vector a -> Validation TooShort a) -> View TooShort (Vector a) a
forall e a b. (a -> Validation e b) -> View e a b
View) ((Vector a -> Validation TooShort a)
 -> Validation err (View TooShort (Vector a) a))
-> (Vector a -> Validation TooShort a)
-> Validation err (View TooShort (Vector a) a)
forall a b. (a -> b) -> a -> b
$ \Vector a
row ->
        case Vector a -> Int -> Maybe a
forall a. Vector a -> Int -> Maybe a
vectorIndexInt Vector a
row Int
i of
            Maybe a
Nothing -> TooShort -> Validation TooShort a
forall err a. err -> Validation err a
Failure TooShort
TooShort
            Just a
x  -> a -> Validation TooShort a
forall err a. a -> Validation err a
Success a
x

textZipViewUtf8 ::
    forall e a .
    Text
    -> View e ByteString a
    -> ZipView
        (At (ColumnName Text) LookupError)
        (At (ColumnName Text) (IndexError e))
        a

textZipViewUtf8 :: Text
-> View e ByteString a
-> ZipView
     (At (ColumnName Text) LookupError)
     (At (ColumnName Text) (IndexError e))
     a
textZipViewUtf8 Text
name View e ByteString a
fieldView =
    ((LookupError -> At (ColumnName Text) LookupError)
-> (IndexError e -> At (ColumnName Text) (IndexError e))
-> ZipView LookupError (IndexError e) a
-> ZipView
     (At (ColumnName Text) LookupError)
     (At (ColumnName Text) (IndexError e))
     a
forall headerError1 headerError2 rowError1 rowError2 a.
(headerError1 -> headerError2)
-> (rowError1 -> rowError2)
-> ZipView headerError1 rowError1 a
-> ZipView headerError2 rowError2 a
overZipViewError LookupError -> At (ColumnName Text) LookupError
forall a. a -> At (ColumnName Text) a
at IndexError e -> At (ColumnName Text) (IndexError e)
forall a. a -> At (ColumnName Text) a
at (ZipView LookupError (IndexError e) a
 -> ZipView
      (At (ColumnName Text) LookupError)
      (At (ColumnName Text) (IndexError e))
      a)
-> ((Vector ByteString
     -> Validation
          LookupError (View (IndexError e) (Vector ByteString) a))
    -> ZipView LookupError (IndexError e) a)
-> (Vector ByteString
    -> Validation
         LookupError (View (IndexError e) (Vector ByteString) a))
-> ZipView
     (At (ColumnName Text) LookupError)
     (At (ColumnName Text) (IndexError e))
     a
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. View
  LookupError
  (Vector ByteString)
  (View (IndexError e) (Vector ByteString) a)
-> ZipView LookupError (IndexError e) a
forall headerError rowError a.
View
  headerError
  (Vector ByteString)
  (View rowError (Vector ByteString) a)
-> ZipView headerError rowError a
ZipView (View
   LookupError
   (Vector ByteString)
   (View (IndexError e) (Vector ByteString) a)
 -> ZipView LookupError (IndexError e) a)
-> ((Vector ByteString
     -> Validation
          LookupError (View (IndexError e) (Vector ByteString) a))
    -> View
         LookupError
         (Vector ByteString)
         (View (IndexError e) (Vector ByteString) a))
-> (Vector ByteString
    -> Validation
         LookupError (View (IndexError e) (Vector ByteString) a))
-> ZipView LookupError (IndexError e) a
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Vector ByteString
 -> Validation
      LookupError (View (IndexError e) (Vector ByteString) a))
-> View
     LookupError
     (Vector ByteString)
     (View (IndexError e) (Vector ByteString) a)
forall e a b. (a -> Validation e b) -> View e a b
View) ((Vector ByteString
  -> Validation
       LookupError (View (IndexError e) (Vector ByteString) a))
 -> ZipView
      (At (ColumnName Text) LookupError)
      (At (ColumnName Text) (IndexError e))
      a)
-> (Vector ByteString
    -> Validation
         LookupError (View (IndexError e) (Vector ByteString) a))
-> ZipView
     (At (ColumnName Text) LookupError)
     (At (ColumnName Text) (IndexError e))
     a
forall a b. (a -> b) -> a -> b
$ \Vector ByteString
header ->
    case (ByteString -> Bool) -> [ByteString] -> [Int]
forall a. (a -> Bool) -> [a] -> [Int]
List.findIndices (ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== Text -> ByteString
forall a. EncodeUtf8 a => a -> ByteString
encodeUtf8 Text
name) (Vector ByteString -> [ByteString]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList Vector ByteString
header) of
        []  -> LookupError
-> Validation
     LookupError (View (IndexError e) (Vector ByteString) a)
forall err a. err -> Validation err a
Failure LookupError
LookupError_Missing
        [Int
i] -> Int
-> Validation
     LookupError (View (IndexError e) (Vector ByteString) a)
forall err.
Int -> Validation err (View (IndexError e) (Vector ByteString) a)
withI Int
i
        [Int]
_   -> LookupError
-> Validation
     LookupError (View (IndexError e) (Vector ByteString) a)
forall err a. err -> Validation err a
Failure LookupError
LookupError_Duplicate

  where
    at :: a -> At (ColumnName Text) a
at = ColumnName Text -> a -> At (ColumnName Text) a
forall p a. p -> a -> At p a
At (Text -> ColumnName Text
forall str. str -> ColumnName str
ColumnName Text
name)
    withI :: Int -> Validation err (View (IndexError e) (Vector ByteString) a)
withI Int
i =
        (View (IndexError e) (Vector ByteString) a
-> Validation err (View (IndexError e) (Vector ByteString) a)
forall err a. a -> Validation err a
Success (View (IndexError e) (Vector ByteString) a
 -> Validation err (View (IndexError e) (Vector ByteString) a))
-> ((Vector ByteString -> Validation (IndexError e) a)
    -> View (IndexError e) (Vector ByteString) a)
-> (Vector ByteString -> Validation (IndexError e) a)
-> Validation err (View (IndexError e) (Vector ByteString) a)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Vector ByteString -> Validation (IndexError e) a)
-> View (IndexError e) (Vector ByteString) a
forall e a b. (a -> Validation e b) -> View e a b
View) ((Vector ByteString -> Validation (IndexError e) a)
 -> Validation err (View (IndexError e) (Vector ByteString) a))
-> (Vector ByteString -> Validation (IndexError e) a)
-> Validation err (View (IndexError e) (Vector ByteString) a)
forall a b. (a -> b) -> a -> b
$ \Vector ByteString
row ->
        case Vector ByteString -> Int -> Maybe ByteString
forall a. Vector a -> Int -> Maybe a
vectorIndexInt Vector ByteString
row Int
i of
            Maybe ByteString
Nothing -> IndexError e -> Validation (IndexError e) a
forall err a. err -> Validation err a
Failure IndexError e
forall error. IndexError error
IndexError_TooShort
            Just ByteString
x -> View (IndexError e) ByteString a
-> ByteString -> Validation (IndexError e) a
forall e a b. View e a b -> a -> Validation e b
applyView ((e -> IndexError e)
-> View e ByteString a -> View (IndexError e) ByteString a
forall e1 e2 a b. (e1 -> e2) -> View e1 a b -> View e2 a b
overViewError e -> IndexError e
forall error. error -> IndexError error
IndexError_FieldError View e ByteString a
fieldView) ByteString
x

textZipViewUtf8' ::
    Text
    -> ZipView
        (At (ColumnName Text) LookupError)
        (At (ColumnName Text) TooShort)
        ByteString

textZipViewUtf8' :: Text
-> ZipView
     (At (ColumnName Text) LookupError)
     (At (ColumnName Text) TooShort)
     ByteString
textZipViewUtf8' Text
name =
    ((LookupError -> At (ColumnName Text) LookupError)
-> (TooShort -> At (ColumnName Text) TooShort)
-> ZipView LookupError TooShort ByteString
-> ZipView
     (At (ColumnName Text) LookupError)
     (At (ColumnName Text) TooShort)
     ByteString
forall headerError1 headerError2 rowError1 rowError2 a.
(headerError1 -> headerError2)
-> (rowError1 -> rowError2)
-> ZipView headerError1 rowError1 a
-> ZipView headerError2 rowError2 a
overZipViewError LookupError -> At (ColumnName Text) LookupError
forall a. a -> At (ColumnName Text) a
at TooShort -> At (ColumnName Text) TooShort
forall a. a -> At (ColumnName Text) a
at (ZipView LookupError TooShort ByteString
 -> ZipView
      (At (ColumnName Text) LookupError)
      (At (ColumnName Text) TooShort)
      ByteString)
-> ((Vector ByteString
     -> Validation
          LookupError (View TooShort (Vector ByteString) ByteString))
    -> ZipView LookupError TooShort ByteString)
-> (Vector ByteString
    -> Validation
         LookupError (View TooShort (Vector ByteString) ByteString))
-> ZipView
     (At (ColumnName Text) LookupError)
     (At (ColumnName Text) TooShort)
     ByteString
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. View
  LookupError
  (Vector ByteString)
  (View TooShort (Vector ByteString) ByteString)
-> ZipView LookupError TooShort ByteString
forall headerError rowError a.
View
  headerError
  (Vector ByteString)
  (View rowError (Vector ByteString) a)
-> ZipView headerError rowError a
ZipView (View
   LookupError
   (Vector ByteString)
   (View TooShort (Vector ByteString) ByteString)
 -> ZipView LookupError TooShort ByteString)
-> ((Vector ByteString
     -> Validation
          LookupError (View TooShort (Vector ByteString) ByteString))
    -> View
         LookupError
         (Vector ByteString)
         (View TooShort (Vector ByteString) ByteString))
-> (Vector ByteString
    -> Validation
         LookupError (View TooShort (Vector ByteString) ByteString))
-> ZipView LookupError TooShort ByteString
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Vector ByteString
 -> Validation
      LookupError (View TooShort (Vector ByteString) ByteString))
-> View
     LookupError
     (Vector ByteString)
     (View TooShort (Vector ByteString) ByteString)
forall e a b. (a -> Validation e b) -> View e a b
View) ((Vector ByteString
  -> Validation
       LookupError (View TooShort (Vector ByteString) ByteString))
 -> ZipView
      (At (ColumnName Text) LookupError)
      (At (ColumnName Text) TooShort)
      ByteString)
-> (Vector ByteString
    -> Validation
         LookupError (View TooShort (Vector ByteString) ByteString))
-> ZipView
     (At (ColumnName Text) LookupError)
     (At (ColumnName Text) TooShort)
     ByteString
forall a b. (a -> b) -> a -> b
$ \Vector ByteString
header ->
    case (ByteString -> Bool) -> [ByteString] -> [Int]
forall a. (a -> Bool) -> [a] -> [Int]
List.findIndices (ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== Text -> ByteString
forall a. EncodeUtf8 a => a -> ByteString
encodeUtf8 Text
name) (Vector ByteString -> [ByteString]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList Vector ByteString
header) of
        []  -> LookupError
-> Validation
     LookupError (View TooShort (Vector ByteString) ByteString)
forall err a. err -> Validation err a
Failure LookupError
LookupError_Missing
        [Int
i] -> Int
-> Validation
     LookupError (View TooShort (Vector ByteString) ByteString)
forall err a. Int -> Validation err (View TooShort (Vector a) a)
withI Int
i
        [Int]
_   -> LookupError
-> Validation
     LookupError (View TooShort (Vector ByteString) ByteString)
forall err a. err -> Validation err a
Failure LookupError
LookupError_Duplicate
  where
    at :: a -> At (ColumnName Text) a
at = ColumnName Text -> a -> At (ColumnName Text) a
forall p a. p -> a -> At p a
At (Text -> ColumnName Text
forall str. str -> ColumnName str
ColumnName Text
name)
    withI :: Int -> Validation err (View TooShort (Vector a) a)
withI Int
i =
        (View TooShort (Vector a) a
-> Validation err (View TooShort (Vector a) a)
forall err a. a -> Validation err a
Success (View TooShort (Vector a) a
 -> Validation err (View TooShort (Vector a) a))
-> ((Vector a -> Validation TooShort a)
    -> View TooShort (Vector a) a)
-> (Vector a -> Validation TooShort a)
-> Validation err (View TooShort (Vector a) a)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Vector a -> Validation TooShort a) -> View TooShort (Vector a) a
forall e a b. (a -> Validation e b) -> View e a b
View) ((Vector a -> Validation TooShort a)
 -> Validation err (View TooShort (Vector a) a))
-> (Vector a -> Validation TooShort a)
-> Validation err (View TooShort (Vector a) a)
forall a b. (a -> b) -> a -> b
$ \Vector a
row ->
        case Vector a -> Int -> Maybe a
forall a. Vector a -> Int -> Maybe a
vectorIndexInt Vector a
row Int
i of
            Maybe a
Nothing -> TooShort -> Validation TooShort a
forall err a. err -> Validation err a
Failure TooShort
TooShort
            Just a
x  -> a -> Validation TooShort a
forall err a. a -> Validation err a
Success a
x

byteStringZipViewPosition ::
    forall headerError .
    ColumnNumber
    -> ZipView headerError TooShort ByteString

byteStringZipViewPosition :: ColumnNumber -> ZipView headerError TooShort ByteString
byteStringZipViewPosition (ColumnNumber (Positive Natural
n)) =
    (View
  headerError
  (Vector ByteString)
  (View TooShort (Vector ByteString) ByteString)
-> ZipView headerError TooShort ByteString
forall headerError rowError a.
View
  headerError
  (Vector ByteString)
  (View rowError (Vector ByteString) a)
-> ZipView headerError rowError a
ZipView (View
   headerError
   (Vector ByteString)
   (View TooShort (Vector ByteString) ByteString)
 -> ZipView headerError TooShort ByteString)
-> ((Vector ByteString
     -> Validation
          headerError (View TooShort (Vector ByteString) ByteString))
    -> View
         headerError
         (Vector ByteString)
         (View TooShort (Vector ByteString) ByteString))
-> (Vector ByteString
    -> Validation
         headerError (View TooShort (Vector ByteString) ByteString))
-> ZipView headerError TooShort ByteString
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Vector ByteString
 -> Validation
      headerError (View TooShort (Vector ByteString) ByteString))
-> View
     headerError
     (Vector ByteString)
     (View TooShort (Vector ByteString) ByteString)
forall e a b. (a -> Validation e b) -> View e a b
View) ((Vector ByteString
  -> Validation
       headerError (View TooShort (Vector ByteString) ByteString))
 -> ZipView headerError TooShort ByteString)
-> (Vector ByteString
    -> Validation
         headerError (View TooShort (Vector ByteString) ByteString))
-> ZipView headerError TooShort ByteString
forall a b. (a -> b) -> a -> b
$ \Vector ByteString
_header ->
    (View TooShort (Vector ByteString) ByteString
-> Validation
     headerError (View TooShort (Vector ByteString) ByteString)
forall err a. a -> Validation err a
Success (View TooShort (Vector ByteString) ByteString
 -> Validation
      headerError (View TooShort (Vector ByteString) ByteString))
-> ((Vector ByteString -> Validation TooShort ByteString)
    -> View TooShort (Vector ByteString) ByteString)
-> (Vector ByteString -> Validation TooShort ByteString)
-> Validation
     headerError (View TooShort (Vector ByteString) ByteString)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Vector ByteString -> Validation TooShort ByteString)
-> View TooShort (Vector ByteString) ByteString
forall e a b. (a -> Validation e b) -> View e a b
View) ((Vector ByteString -> Validation TooShort ByteString)
 -> Validation
      headerError (View TooShort (Vector ByteString) ByteString))
-> (Vector ByteString -> Validation TooShort ByteString)
-> Validation
     headerError (View TooShort (Vector ByteString) ByteString)
forall a b. (a -> b) -> a -> b
$ \Vector ByteString
row     ->
    case Vector ByteString -> Natural -> Maybe ByteString
forall a. Vector a -> Natural -> Maybe a
vectorIndexNat Vector ByteString
row (Natural
n Natural -> Natural -> Natural
forall a. Num a => a -> a -> a
- Natural
1) of
        Maybe ByteString
Nothing -> TooShort -> Validation TooShort ByteString
forall err a. err -> Validation err a
Failure TooShort
TooShort
        Just ByteString
x  -> ByteString -> Validation TooShort ByteString
forall err a. a -> Validation err a
Success ByteString
x

entireRowZipView :: forall he re . ZipView he re (Vector ByteString)
entireRowZipView :: ZipView he re (Vector ByteString)
entireRowZipView = View
  he
  (Vector ByteString)
  (View re (Vector ByteString) (Vector ByteString))
-> ZipView he re (Vector ByteString)
forall headerError rowError a.
View
  headerError
  (Vector ByteString)
  (View rowError (Vector ByteString) a)
-> ZipView headerError rowError a
ZipView (View re (Vector ByteString) (Vector ByteString)
-> View
     he
     (Vector ByteString)
     (View re (Vector ByteString) (Vector ByteString))
forall e a b. b -> View e a b
constView View re (Vector ByteString) (Vector ByteString)
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id)