module Okapi.HTTP.Request.Cookies (
    Cookie,
    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 Cookie :: Type -> Type
data Cookie 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 Cookie = ByteString
type instance Failure Cookie = ParseError

instance HasLeaf Cookie Int where
    leaf :: Leaf Cookie Int
leaf = (Piece Cookie -> Either (Failure Cookie) Int)
-> (Int -> Piece Cookie) -> Info -> Leaf Cookie 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 Cookie
forall a. ToHttpApiData a => a -> ByteString
toHeader (Text -> Maybe Text -> Info
Info Text
"integer" Maybe Text
forall a. Maybe a
Nothing)
instance HasLeaf Cookie Int16 where
    leaf :: Leaf Cookie Int16
leaf = (Piece Cookie -> Either (Failure Cookie) Int16)
-> (Int16 -> Piece Cookie) -> Info -> Leaf Cookie 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 Cookie
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 Cookie Int32 where
    leaf :: Leaf Cookie Int32
leaf = (Piece Cookie -> Either (Failure Cookie) Int32)
-> (Int32 -> Piece Cookie) -> Info -> Leaf Cookie 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 Cookie
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 Cookie Int64 where
    leaf :: Leaf Cookie Int64
leaf = (Piece Cookie -> Either (Failure Cookie) Int64)
-> (Int64 -> Piece Cookie) -> Info -> Leaf Cookie 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 Cookie
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 Cookie Integer where
    leaf :: Leaf Cookie Integer
leaf = (Piece Cookie -> Either (Failure Cookie) Integer)
-> (Integer -> Piece Cookie) -> Info -> Leaf Cookie 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 Cookie
forall a. ToHttpApiData a => a -> ByteString
toHeader (Text -> Maybe Text -> Info
Info Text
"integer" Maybe Text
forall a. Maybe a
Nothing)
instance HasLeaf Cookie Bool where
    leaf :: Leaf Cookie Bool
leaf = (Piece Cookie -> Either (Failure Cookie) Bool)
-> (Bool -> Piece Cookie) -> Info -> Leaf Cookie 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 Cookie
forall a. ToHttpApiData a => a -> ByteString
toHeader (Text -> Maybe Text -> Info
Info Text
"boolean" Maybe Text
forall a. Maybe a
Nothing)
instance HasLeaf Cookie Text where
    leaf :: Leaf Cookie Text
leaf = (Piece Cookie -> Either (Failure Cookie) Text)
-> (Text -> Piece Cookie) -> Info -> Leaf Cookie 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 Cookie
forall a. ToHttpApiData a => a -> ByteString
toHeader (Text -> Maybe Text -> Info
Info Text
"string" Maybe Text
forall a. Maybe a
Nothing)
instance HasLeaf Cookie UUID where
    leaf :: Leaf Cookie UUID
leaf = (Piece Cookie -> Either (Failure Cookie) UUID)
-> (UUID -> Piece Cookie) -> Info -> Leaf Cookie 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 Cookie
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 Cookie Day where
    leaf :: Leaf Cookie Day
leaf = (Piece Cookie -> Either (Failure Cookie) Day)
-> (Day -> Piece Cookie) -> Info -> Leaf Cookie 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 Cookie
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 Cookie ByteString where
    leaf :: Leaf Cookie ByteString
leaf = (Piece Cookie -> Either (Failure Cookie) ByteString)
-> (ByteString -> Piece Cookie) -> Info -> Leaf Cookie ByteString
forall (t :: * -> *) a.
(Piece t -> Either (Failure t) a)
-> (a -> Piece t) -> Info -> Leaf t a
Leaf ByteString -> Either ParseError ByteString
Piece Cookie -> Either (Failure Cookie) ByteString
forall a b. b -> Either a b
Right ByteString -> ByteString
ByteString -> Piece Cookie
forall a. a -> a
id (Text -> Maybe Text -> Info
Info Text
"string" Maybe Text
forall a. Maybe a
Nothing)