{-# LANGUAGE OverloadedStrings #-}

module Network.Wai.Predicate.Utility (readValues, (&)) where

import Data.ByteString (ByteString)
import Data.ByteString.Conversion
import Data.List (foldl')
import Data.String (fromString)

readValues :: FromByteString a => [ByteString] -> Either ByteString a
readValues :: [ByteString] -> Either ByteString a
readValues = (Either ByteString a -> Either String a -> Either ByteString a)
-> Either ByteString a -> [Either String a] -> Either ByteString a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Either ByteString a -> Either String a -> Either ByteString a
forall a a b.
IsString a =>
Either a b -> Either String b -> Either a b
res (ByteString -> Either ByteString a
forall a b. a -> Either a b
Left ByteString
"no parse") ([Either String a] -> Either ByteString a)
-> ([ByteString] -> [Either String a])
-> [ByteString]
-> Either ByteString a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString -> Either String a)
-> [ByteString] -> [Either String a]
forall a b. (a -> b) -> [a] -> [b]
map (Parser a -> ByteString -> Either String a
forall a. Parser a -> ByteString -> Either String a
runParser Parser a
forall a. FromByteString a => Parser a
parser)
  where
    res :: Either a b -> Either String b -> Either a b
res (Left  a
_) (Right b
x) = b -> Either a b
forall a b. b -> Either a b
Right b
x
    res (Right b
x) Either String b
_         = b -> Either a b
forall a b. b -> Either a b
Right b
x
    res Either a b
_         (Left  String
x) = a -> Either a b
forall a b. a -> Either a b
Left (String -> a
forall a. IsString a => String -> a
fromString String
x)

infixl 1 &
(&) :: a -> (a -> b) -> b
a
a & :: a -> (a -> b) -> b
& a -> b
f = a -> b
f a
a
{-# INLINE (&) #-}