-- SPDX-FileCopyrightText: 2026 Alexandra de Wit
--
-- SPDX-License-Identifier: MIT
{-# LANGUAGE TypeFamilies #-}

{- | A resumable walk over immutable native lexer cursors, with a body-bounded driver.
The primitives preserve json-stream's lenient token and skip rules. Tree reads stay pure.
Packed writers use 'ST' while consuming the same cursor interface.
-}
module Ecluse.Core.Registry.Json.Walk (
    -- * Driving a walk
    Steps (..),
    Step,
    Walk (..),
    readJsonWalk,
    readJsonWalkST,
    nestingLimit,
    Walked (..),
    FieldStep,
    pureStep,
    emit,

    -- * Tokens
    withElement,
    withScanned,
    skipFrom,
    skipRest,
    tooDeep,
    isString,
    readString,
    eachMember,
    memberName,
    eachItem,
) where

import Control.Monad.ST (ST)
import Data.Aeson qualified as Aeson
import Data.ByteString qualified as BS
import Data.JsonStream.Lexer (Cursor, Element (..), decodeString)
import Data.JsonStream.Lexer qualified as Lexer
import GHC.Exts (oneShot)

import Ecluse.Core.Registry.Json.Intern (InternTable, Name, decodedName, plainName)
import Ecluse.Core.Registry.JsonStream (Step, Steps (..), StreamResult, nestingLimit, readSteps)
import Ecluse.Core.Security (BodyLimit, LimitError)

-- | What a walk's continuations return: a read that needs input, has failed or been refused, or is done.
class Walk r where
    -- | What a finished walk returns.
    type Result r

    -- | Suspend until the next chunk of input.
    needData :: (ByteString -> r) -> r

    -- | Stop on a parse error.
    failWith :: Text -> r

    -- | Stop on a refused field.
    refuse :: LimitError -> r

    -- | Finish with the walk's result.
    finish :: Result r -> r

instance Walk (Steps Identity s) where
    type Result (Steps Identity s) = s
    needData :: (ByteString -> Steps Identity s) -> Steps Identity s
needData = (ByteString -> Identity (Steps Identity s)) -> Steps Identity s
forall (m :: * -> *) s. (ByteString -> m (Steps m s)) -> Steps m s
NeedData ((ByteString -> Identity (Steps Identity s)) -> Steps Identity s)
-> ((ByteString -> Steps Identity s)
    -> ByteString -> Identity (Steps Identity s))
-> (ByteString -> Steps Identity s)
-> Steps Identity s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString -> Steps Identity s)
-> ByteString -> Identity (Steps Identity s)
forall a b. Coercible a b => a -> b
coerce
    failWith :: Text -> Steps Identity s
failWith = Text -> Steps Identity s
forall (m :: * -> *) s. Text -> Steps m s
Failed
    refuse :: LimitError -> Steps Identity s
refuse = LimitError -> Steps Identity s
forall (m :: * -> *) s. LimitError -> Steps m s
Refused
    finish :: Result (Steps Identity s) -> Steps Identity s
finish = s -> Steps Identity s
Result (Steps Identity s) -> Steps Identity s
forall (m :: * -> *) s. s -> Steps m s
Finished

instance Walk (ST st (Steps (ST st) s)) where
    type Result (ST st (Steps (ST st) s)) = s
    needData :: (ByteString -> ST st (Steps (ST st) s)) -> ST st (Steps (ST st) s)
needData = Steps (ST st) s -> ST st (Steps (ST st) s)
forall a. a -> ST st a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Steps (ST st) s -> ST st (Steps (ST st) s))
-> ((ByteString -> ST st (Steps (ST st) s)) -> Steps (ST st) s)
-> (ByteString -> ST st (Steps (ST st) s))
-> ST st (Steps (ST st) s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString -> ST st (Steps (ST st) s)) -> Steps (ST st) s
forall (m :: * -> *) s. (ByteString -> m (Steps m s)) -> Steps m s
NeedData
    failWith :: Text -> ST st (Steps (ST st) s)
failWith = Steps (ST st) s -> ST st (Steps (ST st) s)
forall a. a -> ST st a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Steps (ST st) s -> ST st (Steps (ST st) s))
-> (Text -> Steps (ST st) s) -> Text -> ST st (Steps (ST st) s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Steps (ST st) s
forall (m :: * -> *) s. Text -> Steps m s
Failed
    refuse :: LimitError -> ST st (Steps (ST st) s)
refuse = Steps (ST st) s -> ST st (Steps (ST st) s)
forall a. a -> ST st a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Steps (ST st) s -> ST st (Steps (ST st) s))
-> (LimitError -> Steps (ST st) s)
-> LimitError
-> ST st (Steps (ST st) s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LimitError -> Steps (ST st) s
forall (m :: * -> *) s. LimitError -> Steps m s
Refused
    finish :: Result (ST st (Steps (ST st) s)) -> ST st (Steps (ST st) s)
finish = Steps (ST st) s -> ST st (Steps (ST st) s)
forall a. a -> ST st a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Steps (ST st) s -> ST st (Steps (ST st) s))
-> (s -> Steps (ST st) s) -> s -> ST st (Steps (ST st) s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Steps (ST st) s
forall (m :: * -> *) s. s -> Steps m s
Finished

-- | Walk a body as 'Ecluse.Core.Registry.JsonStream.readJsonStream' reads one, from the lexer's first token.
readJsonWalk :: (Monad m) => BodyLimit -> (Cursor -> Step s) -> m ByteString -> m (Either LimitError (StreamResult s))
readJsonWalk :: forall (m :: * -> *) s.
Monad m =>
BodyLimit
-> (Cursor -> Step s)
-> m ByteString
-> m (Either LimitError (StreamResult s))
readJsonWalk BodyLimit
bound Cursor -> Step s
walk = (forall a. Identity a -> m a)
-> BodyLimit
-> Step s
-> m ByteString
-> m (Either LimitError (StreamResult s))
forall (n :: * -> *) (m :: * -> *) s.
Monad n =>
(forall a. m a -> n a)
-> BodyLimit
-> Steps m s
-> n ByteString
-> n (Either LimitError (StreamResult s))
readSteps (a -> m a
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> m a) -> (Identity a -> a) -> Identity a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Identity a -> a
forall a. Identity a -> a
runIdentity) BodyLimit
bound (Cursor -> Step s
walk (ByteString -> Cursor
Lexer.start ByteString
BS.empty))

-- | 'readJsonWalk' for a walk that writes in 'ST' as it reads, run in the reader's effect.
readJsonWalkST :: (Monad m) => (forall a. ST st a -> m a) -> BodyLimit -> (Cursor -> ST st (Steps (ST st) s)) -> m ByteString -> m (Either LimitError (StreamResult s))
readJsonWalkST :: forall (m :: * -> *) st s.
Monad m =>
(forall a. ST st a -> m a)
-> BodyLimit
-> (Cursor -> ST st (Steps (ST st) s))
-> m ByteString
-> m (Either LimitError (StreamResult s))
readJsonWalkST forall a. ST st a -> m a
run BodyLimit
bound Cursor -> ST st (Steps (ST st) s)
walk m ByteString
readChunk = ST st (Steps (ST st) s) -> m (Steps (ST st) s)
forall a. ST st a -> m a
run (Cursor -> ST st (Steps (ST st) s)
walk (ByteString -> Cursor
Lexer.start ByteString
BS.empty)) m (Steps (ST st) s)
-> (Steps (ST st) s -> m (Either LimitError (StreamResult s)))
-> m (Either LimitError (StreamResult s))
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Steps (ST st) s
start -> (forall a. ST st a -> m a)
-> BodyLimit
-> Steps (ST st) s
-> m ByteString
-> m (Either LimitError (StreamResult s))
forall (n :: * -> *) (m :: * -> *) s.
Monad n =>
(forall a. m a -> n a)
-> BodyLimit
-> Steps m s
-> n ByteString
-> n (Either LimitError (StreamResult s))
readSteps ST st a -> m a
forall a. ST st a -> m a
run BodyLimit
bound Steps (ST st) s
start m ByteString
readChunk

-- | A walk's state between tokens: the read's table and the consumer's accumulator.
data Walked s = Walked !InternTable s

-- | How a consumer takes each field: it continues with its next state, or refuses the field.
type FieldStep s field r = s -> field -> (Either LimitError s -> r) -> r

-- | A consumer that takes each field without an effect of its own.
pureStep :: (s -> field -> Either LimitError s) -> FieldStep s field r
pureStep :: forall s field r.
(s -> field -> Either LimitError s) -> FieldStep s field r
pureStep s -> field -> Either LimitError s
step s
acc field
field Either LimitError s -> r
next = Either LimitError s -> r
next (s -> field -> Either LimitError s
step s
acc field
field)
{-# INLINE pureStep #-}

-- | Pass a field to the consumer's step. A refused field ends the walk.
emit :: (Walk r) => FieldStep s field r -> s -> field -> (s -> r) -> r
emit :: forall r s field.
Walk r =>
FieldStep s field r -> s -> field -> (s -> r) -> r
emit FieldStep s field r
step s
acc field
field s -> r
next = FieldStep s field r
step s
acc field
field ((LimitError -> r) -> (s -> r) -> Either LimitError s -> r
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either LimitError -> r
forall r. Walk r => LimitError -> r
refuse s -> r
next)
{-# INLINE emit #-}

-- | The next element, suspending for input at a chunk boundary. The continuation runs once.
withElement :: (Walk r) => Cursor -> (Element -> Cursor -> r) -> r
withElement :: forall r. Walk r => Cursor -> (Element -> Cursor -> r) -> r
withElement Cursor
tokens Element -> Cursor -> r
next = case Cursor -> Scanned
Lexer.next Cursor
tokens of
    Lexer.Token Element
element Cursor
rest -> Element -> Cursor -> r
once Element
element Cursor
rest
    Scanned
result -> Scanned -> (Element -> Cursor -> r) -> r
forall r. Walk r => Scanned -> (Element -> Cursor -> r) -> r
withScanned Scanned
result Element -> Cursor -> r
once
  where
    once :: Element -> Cursor -> r
once = (Element -> Cursor -> r) -> Element -> Cursor -> r
forall a b. (a -> b) -> a -> b
oneShot ((Cursor -> r) -> Cursor -> r
forall a b. (a -> b) -> a -> b
oneShot ((Cursor -> r) -> Cursor -> r)
-> (Element -> Cursor -> r) -> Element -> Cursor -> r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Element -> Cursor -> r
next)
{-# INLINE withElement #-}

-- | Continue a scan result without scanning its input again.
withScanned :: (Walk r) => Lexer.Scanned -> (Element -> Cursor -> r) -> r
withScanned :: forall r. Walk r => Scanned -> (Element -> Cursor -> r) -> r
withScanned Scanned
result Element -> Cursor -> r
next = case Scanned
result of
    Lexer.Token Element
element Cursor
rest -> Element -> Cursor -> r
next Element
element Cursor
rest
    Lexer.More Cursor
waiting -> (ByteString -> r) -> r
forall r. Walk r => (ByteString -> r) -> r
needData (\ByteString
chunk -> Cursor -> (Element -> Cursor -> r) -> r
forall r. Walk r => Cursor -> (Element -> Cursor -> r) -> r
withElement (Cursor -> ByteString -> Cursor
Lexer.feed Cursor
waiting ByteString
chunk) Element -> Cursor -> r
next)
    Scanned
Lexer.Failed -> Text -> r
forall r. Walk r => Text -> r
failWith Text
"the JSON lexer failed"
{-# INLINEABLE withScanned #-}

-- | Skip the value starting at the element without decoding it, as json-stream's @ignoreVal@ does.
skipFrom :: (Walk r) => Element -> Cursor -> (Cursor -> r) -> r
skipFrom :: forall r. Walk r => Element -> Cursor -> (Cursor -> r) -> r
skipFrom Element
element Cursor
rest Cursor -> r
next = case Element
element of
    JValue Value
_ -> Cursor -> r
next Cursor
rest
    JInteger CLong
_ -> Cursor -> r
next Cursor
rest
    StringPlain{} -> Cursor -> r
next Cursor
rest
    StringEncoded{} -> Cursor -> r
next Cursor
rest
    StringContent ByteString
_ -> Cursor -> (Cursor -> r) -> r
forall r. Walk r => Cursor -> (Cursor -> r) -> r
skipStringThen Cursor
rest Cursor -> r
next
    Element
ArrayBegin -> Int -> Cursor -> (Cursor -> r) -> r
forall r. Walk r => Int -> Cursor -> (Cursor -> r) -> r
skipRest Int
1 Cursor
rest Cursor -> r
next
    Element
ObjectBegin -> Int -> Cursor -> (Cursor -> r) -> r
forall r. Walk r => Int -> Cursor -> (Cursor -> r) -> r
skipRest Int
1 Cursor
rest Cursor -> r
next
    Element
ArrayEnd -> Text -> r
forall r. Walk r => Text -> r
failWith Text
"unexpected end of array"
    Element
ObjectEnd -> Text -> r
forall r. Walk r => Text -> r
failWith Text
"unexpected end of object"
    Element
StringEnd -> Text -> r
forall r. Walk r => Text -> r
failWith Text
"unexpected end of string"
{-# INLINEABLE skipFrom #-}

-- | Skip to the end of the container the given number of levels up. Any closing token closes a level.
skipRest :: (Walk r) => Int -> Cursor -> (Cursor -> r) -> r
skipRest :: forall r. Walk r => Int -> Cursor -> (Cursor -> r) -> r
skipRest !Int
level Cursor
tokens Cursor -> r
next = case Int -> Cursor -> Skipped
Lexer.skipLevels Int
level Cursor
tokens of
    Lexer.Closed Cursor
rest -> Cursor -> r
next Cursor
rest
    Lexer.Unclosed Int
open Cursor
waiting -> (ByteString -> r) -> r
forall r. Walk r => (ByteString -> r) -> r
needData (\ByteString
chunk -> Int -> Cursor -> (Cursor -> r) -> r
forall r. Walk r => Int -> Cursor -> (Cursor -> r) -> r
skipRest Int
open (Cursor -> ByteString -> Cursor
Lexer.feed Cursor
waiting ByteString
chunk) Cursor -> r
next)
    Skipped
Lexer.SkipFailed -> Text -> r
forall r. Walk r => Text -> r
failWith Text
"the JSON lexer failed"
    Skipped
Lexer.StrayStringEnd -> Text -> r
forall r. Walk r => Text -> r
failWith Text
"unexpected end of string"
{-# INLINEABLE skipRest #-}

skipStringThen :: (Walk r) => Cursor -> (Cursor -> r) -> r
skipStringThen :: forall r. Walk r => Cursor -> (Cursor -> r) -> r
skipStringThen Cursor
tokens Cursor -> r
next = Cursor -> (Element -> Cursor -> r) -> r
forall r. Walk r => Cursor -> (Element -> Cursor -> r) -> r
withElement Cursor
tokens ((Element -> Cursor -> r) -> r) -> (Element -> Cursor -> r) -> r
forall a b. (a -> b) -> a -> b
$ \Element
element Cursor
rest -> case Element
element of
    StringContent ByteString
_ -> Cursor -> (Cursor -> r) -> r
forall r. Walk r => Cursor -> (Cursor -> r) -> r
skipStringThen Cursor
rest Cursor -> r
next
    Element
StringEnd -> Cursor -> r
next Cursor
rest
    Element
_ -> Text -> r
forall r. Walk r => Text -> r
failWith Text
"unexpected token in a string"
{-# INLINEABLE skipStringThen #-}

-- | Skip the value, then fail: a retained value with no structural budget left.
tooDeep :: (Walk r) => Element -> Cursor -> r
tooDeep :: forall r. Walk r => Element -> Cursor -> r
tooDeep Element
element Cursor
rest = Element -> Cursor -> (Cursor -> r) -> r
forall r. Walk r => Element -> Cursor -> (Cursor -> r) -> r
skipFrom Element
element Cursor
rest (r -> Cursor -> r
forall a b. a -> b -> a
const (Text -> r
forall r. Walk r => Text -> r
failWith Text
nestingLimit))
{-# INLINEABLE tooDeep #-}

-- | Whether the element starts a string.
isString :: Element -> Bool
isString :: Element -> Bool
isString = \case
    StringPlain{} -> Bool
True
    StringEncoded{} -> Bool
True
    StringContent ByteString
_ -> Bool
True
    JValue (Aeson.String Text
_) -> Bool
True
    Element
_ -> Bool
False

-- | Decode the string starting at the element, failing where json-stream's @string@ fails.
readString :: (Walk r) => Element -> Cursor -> (Name -> Cursor -> r) -> r
readString :: forall r. Walk r => Element -> Cursor -> (Name -> Cursor -> r) -> r
readString Element
element Cursor
rest Name -> Cursor -> r
next = case Element
element of
    StringPlain PlainString
bytes -> Name -> Cursor -> r
next (PlainString -> Name
plainName PlainString
bytes) Cursor
rest
    StringEncoded ByteString
bytes -> case ByteString -> Either UnicodeException Text
decodeString ByteString
bytes of
        Right Text
text -> Name -> Cursor -> r
next (Text -> Name
decodedName Text
text) Cursor
rest
        Left UnicodeException
err -> Text -> r
forall r. Walk r => Text -> r
failWith (UnicodeException -> Text
forall b a. (Show a, IsString b) => a -> b
show UnicodeException
err)
    StringContent ByteString
part -> [ByteString] -> Cursor -> (Name -> Cursor -> r) -> r
forall r.
Walk r =>
[ByteString] -> Cursor -> (Name -> Cursor -> r) -> r
longString [ByteString
part] Cursor
rest Name -> Cursor -> r
next
    JValue (Aeson.String Text
text) -> Name -> Cursor -> r
next (Text -> Name
decodedName Text
text) Cursor
rest
    Element
_ -> Text -> r
forall r. Walk r => Text -> r
failWith Text
"expected a string"
{-# INLINEABLE readString #-}

longString :: (Walk r) => [ByteString] -> Cursor -> (Name -> Cursor -> r) -> r
longString :: forall r.
Walk r =>
[ByteString] -> Cursor -> (Name -> Cursor -> r) -> r
longString [ByteString]
parts Cursor
tokens Name -> Cursor -> r
next = Cursor -> (Element -> Cursor -> r) -> r
forall r. Walk r => Cursor -> (Element -> Cursor -> r) -> r
withElement Cursor
tokens ((Element -> Cursor -> r) -> r) -> (Element -> Cursor -> r) -> r
forall a b. (a -> b) -> a -> b
$ \Element
element Cursor
rest -> case Element
element of
    StringContent ByteString
part -> [ByteString] -> Cursor -> (Name -> Cursor -> r) -> r
forall r.
Walk r =>
[ByteString] -> Cursor -> (Name -> Cursor -> r) -> r
longString (ByteString
part ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString]
parts) Cursor
rest Name -> Cursor -> r
next
    Element
StringEnd -> case ByteString -> Either UnicodeException Text
decodeString ([ByteString] -> ByteString
BS.concat ([ByteString] -> [ByteString]
forall a. [a] -> [a]
reverse [ByteString]
parts)) of
        Right Text
text -> Name -> Cursor -> r
next (Text -> Name
decodedName Text
text) Cursor
rest
        Left UnicodeException
_ -> Text -> r
forall r. Walk r => Text -> r
failWith Text
"Error decoding UTF8"
    Element
_ -> Text -> r
forall r. Walk r => Text -> r
failWith Text
"unexpected token in a string"
{-# INLINEABLE longString #-}

{- | Visit each member of an object whose opening brace was read, as json-stream's @objectKeyValues@
does: every key is decoded, and a key longer than 64 KiB across pieces drops its member unread.
-}
eachMember :: (Walk r) => (st -> Name -> Cursor -> (st -> Cursor -> r) -> r) -> (st -> Cursor -> r) -> st -> Cursor -> r
eachMember :: forall r st.
Walk r =>
(st -> Name -> Cursor -> (st -> Cursor -> r) -> r)
-> (st -> Cursor -> r) -> st -> Cursor -> r
eachMember st -> Name -> Cursor -> (st -> Cursor -> r) -> r
visit st -> Cursor -> r
done = st -> Cursor -> r
loop
  where
    loop :: st -> Cursor -> r
loop st
acc Cursor
tokens = Cursor -> (Element -> Cursor -> r) -> r
forall r. Walk r => Cursor -> (Element -> Cursor -> r) -> r
withElement Cursor
tokens ((Element -> Cursor -> r) -> r) -> (Element -> Cursor -> r) -> r
forall a b. (a -> b) -> a -> b
$ \Element
element Cursor
rest -> case Element
element of
        Element
ObjectEnd -> st -> Cursor -> r
done st
acc Cursor
rest
        Element
_ -> Element -> Cursor -> (Name -> Cursor -> r) -> (Cursor -> r) -> r
forall r.
Walk r =>
Element -> Cursor -> (Name -> Cursor -> r) -> (Cursor -> r) -> r
memberName Element
element Cursor
rest (\Name
key Cursor
after -> st -> Name -> Cursor -> (st -> Cursor -> r) -> r
visit st
acc Name
key Cursor
after st -> Cursor -> r
loop) (st -> Cursor -> r
loop st
acc)
{-# INLINE eachMember #-}

-- | Decode the object key at the element, or skip its member when json-stream drops it unread.
memberName :: (Walk r) => Element -> Cursor -> (Name -> Cursor -> r) -> (Cursor -> r) -> r
memberName :: forall r.
Walk r =>
Element -> Cursor -> (Name -> Cursor -> r) -> (Cursor -> r) -> r
memberName Element
element Cursor
rest Name -> Cursor -> r
named Cursor -> r
dropped = case Element
element of
    JValue (Aeson.String Text
key) -> Name -> Cursor -> r
named (Text -> Name
decodedName Text
key) Cursor
rest
    StringPlain PlainString
bytes -> Name -> Cursor -> r
named (PlainString -> Name
plainName PlainString
bytes) Cursor
rest
    StringEncoded ByteString
bytes -> case ByteString -> Either UnicodeException Text
decodeString ByteString
bytes of
        Right Text
key -> Name -> Cursor -> r
named (Text -> Name
decodedName Text
key) Cursor
rest
        Left UnicodeException
err -> Text -> r
forall r. Walk r => Text -> r
failWith (UnicodeException -> Text
forall b a. (Show a, IsString b) => a -> b
show UnicodeException
err)
    StringContent ByteString
part -> [ByteString]
-> Int -> Cursor -> (Name -> Cursor -> r) -> (Cursor -> r) -> r
forall r.
Walk r =>
[ByteString]
-> Int -> Cursor -> (Name -> Cursor -> r) -> (Cursor -> r) -> r
longKey [ByteString
part] (ByteString -> Int
BS.length ByteString
part) Cursor
rest Name -> Cursor -> r
named Cursor -> r
dropped
    Element
_ -> Text -> r
forall r. Walk r => Text -> r
failWith Text
"unexpected token where an object key belongs"
{-# INLINEABLE memberName #-}

-- json-stream's getLongKey: the limit applies from the third piece, and a dropped key skips its value.
longKey :: (Walk r) => [ByteString] -> Int -> Cursor -> (Name -> Cursor -> r) -> (Cursor -> r) -> r
longKey :: forall r.
Walk r =>
[ByteString]
-> Int -> Cursor -> (Name -> Cursor -> r) -> (Cursor -> r) -> r
longKey [ByteString]
parts !Int
size Cursor
tokens Name -> Cursor -> r
next Cursor -> r
dropped = Cursor -> (Element -> Cursor -> r) -> r
forall r. Walk r => Cursor -> (Element -> Cursor -> r) -> r
withElement Cursor
tokens ((Element -> Cursor -> r) -> r) -> (Element -> Cursor -> r) -> r
forall a b. (a -> b) -> a -> b
$ \Element
element Cursor
rest -> case Element
element of
    Element
StringEnd -> case ByteString -> Either UnicodeException Text
decodeString ([ByteString] -> ByteString
BS.concat ([ByteString] -> [ByteString]
forall a. [a] -> [a]
reverse [ByteString]
parts)) of
        Right Text
key -> Name -> Cursor -> r
next (Text -> Name
decodedName Text
key) Cursor
rest
        Left UnicodeException
_ -> Text -> r
forall r. Walk r => Text -> r
failWith Text
"Error decoding UTF8"
    StringContent ByteString
part
        | Int
size Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
65536 -> Cursor -> (Cursor -> r) -> r
forall r. Walk r => Cursor -> (Cursor -> r) -> r
skipStringThen Cursor
rest (\Cursor
after -> Cursor -> (Element -> Cursor -> r) -> r
forall r. Walk r => Cursor -> (Element -> Cursor -> r) -> r
withElement Cursor
after (\Element
value Cursor
afterValue -> Element -> Cursor -> (Cursor -> r) -> r
forall r. Walk r => Element -> Cursor -> (Cursor -> r) -> r
skipFrom Element
value Cursor
afterValue Cursor -> r
dropped))
        | Bool
otherwise -> [ByteString]
-> Int -> Cursor -> (Name -> Cursor -> r) -> (Cursor -> r) -> r
forall r.
Walk r =>
[ByteString]
-> Int -> Cursor -> (Name -> Cursor -> r) -> (Cursor -> r) -> r
longKey (ByteString
part ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString]
parts) (Int
size Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ByteString -> Int
BS.length ByteString
part) Cursor
rest Name -> Cursor -> r
next Cursor -> r
dropped
    Element
_ -> Text -> r
forall r. Walk r => Text -> r
failWith Text
"unexpected token in an object key"
{-# INLINEABLE longKey #-}

-- | Visit each item of an array whose opening bracket was read, with its position.
eachItem :: (Walk r) => (st -> Int -> Element -> Cursor -> (st -> Cursor -> r) -> r) -> (st -> Cursor -> r) -> st -> Cursor -> r
eachItem :: forall r st.
Walk r =>
(st -> Int -> Element -> Cursor -> (st -> Cursor -> r) -> r)
-> (st -> Cursor -> r) -> st -> Cursor -> r
eachItem st -> Int -> Element -> Cursor -> (st -> Cursor -> r) -> r
visit st -> Cursor -> r
done = Int -> st -> Cursor -> r
loop Int
0
  where
    loop :: Int -> st -> Cursor -> r
loop !Int
position st
acc Cursor
tokens = Cursor -> (Element -> Cursor -> r) -> r
forall r. Walk r => Cursor -> (Element -> Cursor -> r) -> r
withElement Cursor
tokens ((Element -> Cursor -> r) -> r) -> (Element -> Cursor -> r) -> r
forall a b. (a -> b) -> a -> b
$ \Element
element Cursor
rest -> case Element
element of
        Element
ArrayEnd -> st -> Cursor -> r
done st
acc Cursor
rest
        Element
_ -> st -> Int -> Element -> Cursor -> (st -> Cursor -> r) -> r
visit st
acc Int
position Element
element Cursor
rest (Int -> st -> Cursor -> r
loop (Int
position Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1))
{-# INLINE eachItem #-}