module Okapi.HTTP.Response.SetCookies (
    SetCookie,
    ParseError (..),
) where

import Data.Bifunctor (first)
import Data.ByteString (ByteString)
import Data.Int (Int16, Int32, Int64)
import Data.Kind (Type)
import Data.Text (Text)
import Data.Time (Day)
import Data.UUID (UUID)
import Okapi.Tree (Failure, HasLeaf (..), Info (..), Leaf (..), Piece)
import Web.HttpApiData (parseHeader, toHeader)

type SetCookie :: Type -> Type
data SetCookie a

data ParseError = ParseError deriving (ParseError -> ParseError -> Bool
(ParseError -> ParseError -> Bool)
-> (ParseError -> ParseError -> Bool) -> Eq ParseError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ParseError -> ParseError -> Bool
== :: ParseError -> ParseError -> Bool
$c/= :: ParseError -> ParseError -> Bool
/= :: ParseError -> ParseError -> Bool
Eq, Int -> ParseError -> ShowS
[ParseError] -> ShowS
ParseError -> String
(Int -> ParseError -> ShowS)
-> (ParseError -> String)
-> ([ParseError] -> ShowS)
-> Show ParseError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ParseError -> ShowS
showsPrec :: Int -> ParseError -> ShowS
$cshow :: ParseError -> String
show :: ParseError -> String
$cshowList :: [ParseError] -> ShowS
showList :: [ParseError] -> ShowS
Show)

type instance Piece SetCookie = ByteString
type instance Failure SetCookie = ParseError

instance HasLeaf SetCookie Int where
    leaf :: Leaf SetCookie Int
leaf = (Piece SetCookie -> Either (Failure SetCookie) Int)
-> (Int -> Piece SetCookie) -> Info -> Leaf SetCookie Int
forall (t :: * -> *) a.
(Piece t -> Either (Failure t) a)
-> (a -> Piece t) -> Info -> Leaf t a
Leaf ((Text -> ParseError) -> Either Text Int -> Either ParseError Int
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (ParseError -> Text -> ParseError
forall a b. a -> b -> a
const ParseError
ParseError) (Either Text Int -> Either ParseError Int)
-> (ByteString -> Either Text Int)
-> ByteString
-> Either ParseError Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either Text Int
forall a. FromHttpApiData a => ByteString -> Either Text a
parseHeader) Int -> ByteString
Int -> Piece SetCookie
forall a. ToHttpApiData a => a -> ByteString
toHeader (Text -> Maybe Text -> Info
Info Text
"integer" Maybe Text
forall a. Maybe a
Nothing)
instance HasLeaf SetCookie Int16 where
    leaf :: Leaf SetCookie Int16
leaf = (Piece SetCookie -> Either (Failure SetCookie) Int16)
-> (Int16 -> Piece SetCookie) -> Info -> Leaf SetCookie Int16
forall (t :: * -> *) a.
(Piece t -> Either (Failure t) a)
-> (a -> Piece t) -> Info -> Leaf t a
Leaf ((Text -> ParseError)
-> Either Text Int16 -> Either ParseError Int16
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (ParseError -> Text -> ParseError
forall a b. a -> b -> a
const ParseError
ParseError) (Either Text Int16 -> Either ParseError Int16)
-> (ByteString -> Either Text Int16)
-> ByteString
-> Either ParseError Int16
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either Text Int16
forall a. FromHttpApiData a => ByteString -> Either Text a
parseHeader) Int16 -> ByteString
Int16 -> Piece SetCookie
forall a. ToHttpApiData a => a -> ByteString
toHeader (Text -> Maybe Text -> Info
Info Text
"integer" (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"int32"))
instance HasLeaf SetCookie Int32 where
    leaf :: Leaf SetCookie Int32
leaf = (Piece SetCookie -> Either (Failure SetCookie) Int32)
-> (Int32 -> Piece SetCookie) -> Info -> Leaf SetCookie Int32
forall (t :: * -> *) a.
(Piece t -> Either (Failure t) a)
-> (a -> Piece t) -> Info -> Leaf t a
Leaf ((Text -> ParseError)
-> Either Text Int32 -> Either ParseError Int32
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (ParseError -> Text -> ParseError
forall a b. a -> b -> a
const ParseError
ParseError) (Either Text Int32 -> Either ParseError Int32)
-> (ByteString -> Either Text Int32)
-> ByteString
-> Either ParseError Int32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either Text Int32
forall a. FromHttpApiData a => ByteString -> Either Text a
parseHeader) Int32 -> ByteString
Int32 -> Piece SetCookie
forall a. ToHttpApiData a => a -> ByteString
toHeader (Text -> Maybe Text -> Info
Info Text
"integer" (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"int32"))
instance HasLeaf SetCookie Int64 where
    leaf :: Leaf SetCookie Int64
leaf = (Piece SetCookie -> Either (Failure SetCookie) Int64)
-> (Int64 -> Piece SetCookie) -> Info -> Leaf SetCookie Int64
forall (t :: * -> *) a.
(Piece t -> Either (Failure t) a)
-> (a -> Piece t) -> Info -> Leaf t a
Leaf ((Text -> ParseError)
-> Either Text Int64 -> Either ParseError Int64
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (ParseError -> Text -> ParseError
forall a b. a -> b -> a
const ParseError
ParseError) (Either Text Int64 -> Either ParseError Int64)
-> (ByteString -> Either Text Int64)
-> ByteString
-> Either ParseError Int64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either Text Int64
forall a. FromHttpApiData a => ByteString -> Either Text a
parseHeader) Int64 -> ByteString
Int64 -> Piece SetCookie
forall a. ToHttpApiData a => a -> ByteString
toHeader (Text -> Maybe Text -> Info
Info Text
"integer" (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"int64"))
instance HasLeaf SetCookie Integer where
    leaf :: Leaf SetCookie Integer
leaf = (Piece SetCookie -> Either (Failure SetCookie) Integer)
-> (Integer -> Piece SetCookie) -> Info -> Leaf SetCookie Integer
forall (t :: * -> *) a.
(Piece t -> Either (Failure t) a)
-> (a -> Piece t) -> Info -> Leaf t a
Leaf ((Text -> ParseError)
-> Either Text Integer -> Either ParseError Integer
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (ParseError -> Text -> ParseError
forall a b. a -> b -> a
const ParseError
ParseError) (Either Text Integer -> Either ParseError Integer)
-> (ByteString -> Either Text Integer)
-> ByteString
-> Either ParseError Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either Text Integer
forall a. FromHttpApiData a => ByteString -> Either Text a
parseHeader) Integer -> ByteString
Integer -> Piece SetCookie
forall a. ToHttpApiData a => a -> ByteString
toHeader (Text -> Maybe Text -> Info
Info Text
"integer" Maybe Text
forall a. Maybe a
Nothing)
instance HasLeaf SetCookie Bool where
    leaf :: Leaf SetCookie Bool
leaf = (Piece SetCookie -> Either (Failure SetCookie) Bool)
-> (Bool -> Piece SetCookie) -> Info -> Leaf SetCookie Bool
forall (t :: * -> *) a.
(Piece t -> Either (Failure t) a)
-> (a -> Piece t) -> Info -> Leaf t a
Leaf ((Text -> ParseError) -> Either Text Bool -> Either ParseError Bool
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (ParseError -> Text -> ParseError
forall a b. a -> b -> a
const ParseError
ParseError) (Either Text Bool -> Either ParseError Bool)
-> (ByteString -> Either Text Bool)
-> ByteString
-> Either ParseError Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either Text Bool
forall a. FromHttpApiData a => ByteString -> Either Text a
parseHeader) Bool -> ByteString
Bool -> Piece SetCookie
forall a. ToHttpApiData a => a -> ByteString
toHeader (Text -> Maybe Text -> Info
Info Text
"boolean" Maybe Text
forall a. Maybe a
Nothing)
instance HasLeaf SetCookie Text where
    leaf :: Leaf SetCookie Text
leaf = (Piece SetCookie -> Either (Failure SetCookie) Text)
-> (Text -> Piece SetCookie) -> Info -> Leaf SetCookie Text
forall (t :: * -> *) a.
(Piece t -> Either (Failure t) a)
-> (a -> Piece t) -> Info -> Leaf t a
Leaf ((Text -> ParseError) -> Either Text Text -> Either ParseError Text
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (ParseError -> Text -> ParseError
forall a b. a -> b -> a
const ParseError
ParseError) (Either Text Text -> Either ParseError Text)
-> (ByteString -> Either Text Text)
-> ByteString
-> Either ParseError Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either Text Text
forall a. FromHttpApiData a => ByteString -> Either Text a
parseHeader) Text -> ByteString
Text -> Piece SetCookie
forall a. ToHttpApiData a => a -> ByteString
toHeader (Text -> Maybe Text -> Info
Info Text
"string" Maybe Text
forall a. Maybe a
Nothing)
instance HasLeaf SetCookie UUID where
    leaf :: Leaf SetCookie UUID
leaf = (Piece SetCookie -> Either (Failure SetCookie) UUID)
-> (UUID -> Piece SetCookie) -> Info -> Leaf SetCookie UUID
forall (t :: * -> *) a.
(Piece t -> Either (Failure t) a)
-> (a -> Piece t) -> Info -> Leaf t a
Leaf ((Text -> ParseError) -> Either Text UUID -> Either ParseError UUID
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (ParseError -> Text -> ParseError
forall a b. a -> b -> a
const ParseError
ParseError) (Either Text UUID -> Either ParseError UUID)
-> (ByteString -> Either Text UUID)
-> ByteString
-> Either ParseError UUID
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either Text UUID
forall a. FromHttpApiData a => ByteString -> Either Text a
parseHeader) UUID -> ByteString
UUID -> Piece SetCookie
forall a. ToHttpApiData a => a -> ByteString
toHeader (Text -> Maybe Text -> Info
Info Text
"string" (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"uuid"))
instance HasLeaf SetCookie Day where
    leaf :: Leaf SetCookie Day
leaf = (Piece SetCookie -> Either (Failure SetCookie) Day)
-> (Day -> Piece SetCookie) -> Info -> Leaf SetCookie Day
forall (t :: * -> *) a.
(Piece t -> Either (Failure t) a)
-> (a -> Piece t) -> Info -> Leaf t a
Leaf ((Text -> ParseError) -> Either Text Day -> Either ParseError Day
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (ParseError -> Text -> ParseError
forall a b. a -> b -> a
const ParseError
ParseError) (Either Text Day -> Either ParseError Day)
-> (ByteString -> Either Text Day)
-> ByteString
-> Either ParseError Day
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either Text Day
forall a. FromHttpApiData a => ByteString -> Either Text a
parseHeader) Day -> ByteString
Day -> Piece SetCookie
forall a. ToHttpApiData a => a -> ByteString
toHeader (Text -> Maybe Text -> Info
Info Text
"string" (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"date"))

instance HasLeaf SetCookie ByteString where
    leaf :: Leaf SetCookie ByteString
leaf = (Piece SetCookie -> Either (Failure SetCookie) ByteString)
-> (ByteString -> Piece SetCookie)
-> Info
-> Leaf SetCookie ByteString
forall (t :: * -> *) a.
(Piece t -> Either (Failure t) a)
-> (a -> Piece t) -> Info -> Leaf t a
Leaf ByteString -> Either ParseError ByteString
Piece SetCookie -> Either (Failure SetCookie) ByteString
forall a b. b -> Either a b
Right ByteString -> ByteString
ByteString -> Piece SetCookie
forall a. a -> a
id (Text -> Maybe Text -> Info
Info Text
"string" Maybe Text
forall a. Maybe a
Nothing)