{-# LANGUAGE BangPatterns #-}
module Json.Context
( Context (..)
, builderUtf8
, toPath
) where
import Data.Bytes.Builder (Builder)
import Data.Text.Short (ShortText)
import Json.Path (Path)
import qualified Json.Path as Path
data Context
= Top
| Key !ShortText !Context
| Index !Int !Context
deriving (Context -> Context -> Bool
(Context -> Context -> Bool)
-> (Context -> Context -> Bool) -> Eq Context
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Context -> Context -> Bool
== :: Context -> Context -> Bool
$c/= :: Context -> Context -> Bool
/= :: Context -> Context -> Bool
Eq, Int -> Context -> ShowS
[Context] -> ShowS
Context -> String
(Int -> Context -> ShowS)
-> (Context -> String) -> ([Context] -> ShowS) -> Show Context
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Context -> ShowS
showsPrec :: Int -> Context -> ShowS
$cshow :: Context -> String
show :: Context -> String
$cshowList :: [Context] -> ShowS
showList :: [Context] -> ShowS
Show)
toPath :: Context -> Path
toPath :: Context -> Path
toPath = Path -> Context -> Path
go Path
Path.Nil
where
go :: Path -> Context -> Path
go !Path
acc Context
Top = Path
acc
go !Path
acc (Key ShortText
k Context
xs) = Path -> Context -> Path
go (ShortText -> Path -> Path
Path.Key ShortText
k Path
acc) Context
xs
go !Path
acc (Index Int
i Context
xs) = Path -> Context -> Path
go (Int -> Path -> Path
Path.Index Int
i Path
acc) Context
xs
builderUtf8 :: Context -> Builder
builderUtf8 :: Context -> Builder
builderUtf8 Context
ctx0 = Path -> Builder
Path.builderUtf8 (Context -> Path
toPath Context
ctx0)