module Okapi.HTTP.RFC9651 (
    RFC9651,
    ParseError (..),
    parser,
    printer,
    item,
    list,
    dictionary,
) where

import Data.ByteString (ByteString)
import Data.Kind (Type)
import Okapi.Tree (Failure, Parser, Printer, Context)
import Okapi.Tree qualified as Tree
import Okapi.Tree (Tree (..))
import Okapi.Tree qualified as Tree
import Okapi.HTTP.RFC9651.Dictionary (Dictionary)
import Okapi.HTTP.RFC9651.Dictionary qualified as Dictionary
import Okapi.HTTP.RFC9651.Item (Item)
import Okapi.HTTP.RFC9651.Item qualified as Item
import Okapi.HTTP.RFC9651.Lexer (ParseError (..), strip, stripLead)
import Okapi.HTTP.RFC9651.List (List)
import Okapi.HTTP.RFC9651.List qualified as List

type RFC9651 :: Type -> Type
data RFC9651 a where
    SItem :: Tree Item a a -> RFC9651 a
    SList :: Tree List a a -> RFC9651 a
    SDict :: Tree Dictionary a a -> RFC9651 a

type instance Context RFC9651 = ByteString
type instance Failure RFC9651 = ParseError

item :: Tree Item a a -> Tree RFC9651 a a
item :: forall a. Tree Item a a -> Tree RFC9651 a a
item = RFC9651 a -> Tree RFC9651 a a
forall (t :: * -> *) i. t i -> Tree t i i
Node (RFC9651 a -> Tree RFC9651 a a)
-> (Tree Item a a -> RFC9651 a)
-> Tree Item a a
-> Tree RFC9651 a a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tree Item a a -> RFC9651 a
forall a. Tree Item a a -> RFC9651 a
SItem

list :: Tree List a a -> Tree RFC9651 a a
list :: forall a. Tree List a a -> Tree RFC9651 a a
list = RFC9651 a -> Tree RFC9651 a a
forall (t :: * -> *) i. t i -> Tree t i i
Node (RFC9651 a -> Tree RFC9651 a a)
-> (Tree List a a -> RFC9651 a)
-> Tree List a a
-> Tree RFC9651 a a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tree List a a -> RFC9651 a
forall a. Tree List a a -> RFC9651 a
SList

dictionary :: Tree Dictionary a a -> Tree RFC9651 a a
dictionary :: forall a. Tree Dictionary a a -> Tree RFC9651 a a
dictionary = RFC9651 a -> Tree RFC9651 a a
forall (t :: * -> *) i. t i -> Tree t i i
Node (RFC9651 a -> Tree RFC9651 a a)
-> (Tree Dictionary a a -> RFC9651 a)
-> Tree Dictionary a a
-> Tree RFC9651 a a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tree Dictionary a a -> RFC9651 a
forall a. Tree Dictionary a a -> RFC9651 a
SDict

parser :: Tree RFC9651 i o -> Parser RFC9651 o
parser :: forall i o. Tree RFC9651 i o -> Parser RFC9651 o
parser = (forall a. RFC9651 a -> Parser RFC9651 a)
-> Tree RFC9651 i o
-> Context RFC9651
-> (Either (Failure RFC9651) o, Context RFC9651)
forall (t :: * -> *) i o.
(forall a. t a -> Parser t a) -> Tree t i o -> Parser t o
Tree.parser RFC9651 a -> Parser RFC9651 a
forall a. RFC9651 a -> Parser RFC9651 a
alg
  where
    alg :: RFC9651 a -> Parser RFC9651 a
    alg :: forall a. RFC9651 a -> Parser RFC9651 a
alg (SItem Tree Item a a
c) Context RFC9651
s = ((Either ParseError a, ByteString) -> Either ParseError a
forall a b. (a, b) -> a
fst (Tree Item a a -> Parser Item a
forall i o. Tree Item i o -> Parser Item o
Item.parser Tree Item a a
c (ByteString -> ByteString
strip ByteString
Context RFC9651
s)), Context RFC9651
s)
    alg (SList Tree List a a
c) Context RFC9651
s = ((Either ParseError a, ByteString) -> Either ParseError a
forall a b. (a, b) -> a
fst (Tree List a a -> Parser List a
forall i o. Tree List i o -> Parser List o
List.parser Tree List a a
c (ByteString -> ByteString
strip ByteString
Context RFC9651
s)), Context RFC9651
s)
    alg (SDict Tree Dictionary a a
c) Context RFC9651
s = ((Either ParseError a, ByteString) -> Either ParseError a
forall a b. (a, b) -> a
fst (Tree Dictionary a a -> Parser Dictionary a
forall i o. Tree Dictionary i o -> Parser Dictionary o
Dictionary.parser Tree Dictionary a a
c (ByteString -> ByteString
strip ByteString
Context RFC9651
s)), Context RFC9651
s)

printer :: Tree RFC9651 i o -> Printer RFC9651 i
printer :: forall i o. Tree RFC9651 i o -> Printer RFC9651 i
printer = (forall a. RFC9651 a -> Printer RFC9651 a)
-> Tree RFC9651 i o -> i -> Context RFC9651
forall (t :: * -> *) i o.
Monoid (Context t) =>
(forall a. t a -> Printer t a) -> Tree t i o -> Printer t i
Tree.printer RFC9651 a -> Printer RFC9651 a
forall a. RFC9651 a -> Printer RFC9651 a
alg
  where
    alg :: RFC9651 a -> Printer RFC9651 a
    alg :: forall a. RFC9651 a -> Printer RFC9651 a
alg (SItem Tree Item a a
c) a
a = Tree Item a a -> Printer Item a
forall i o. Tree Item i o -> Printer Item i
Item.printer Tree Item a a
c a
a
    alg (SList Tree List a a
c) a
a = ByteString -> ByteString
stripLead (Tree List a a -> Printer List a
forall i o. Tree List i o -> Printer List i
List.printer Tree List a a
c a
a)
    alg (SDict Tree Dictionary a a
c) a
a = ByteString -> ByteString
stripLead (Tree Dictionary a a -> Printer Dictionary a
forall i o. Tree Dictionary i o -> Printer Dictionary i
Dictionary.printer Tree Dictionary a a
c a
a)