{-# LANGUAGE OverloadedStrings #-}

module Data.JsonStream.TokenParser (
    Element(..)
  , TokenResult(..)
) where

import qualified Data.Aeson            as AE
import qualified Data.ByteString.Char8 as BS
import           Foreign.C.Types

data Element = 
    ArrayBegin
  | ArrayEnd !BS.ByteString
  | ObjectBegin
  | ObjectEnd !BS.ByteString
  | StringContent !BS.ByteString
  | StringRaw !BS.ByteString !Bool -- Allow raw strings to go into parser as bytestring/ isAscii
  | StringEnd
  | JValue !AE.Value
  | JInteger !CLong
  deriving (Int -> Element -> ShowS
[Element] -> ShowS
Element -> String
(Int -> Element -> ShowS)
-> (Element -> String) -> ([Element] -> ShowS) -> Show Element
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Element] -> ShowS
$cshowList :: [Element] -> ShowS
show :: Element -> String
$cshow :: Element -> String
showsPrec :: Int -> Element -> ShowS
$cshowsPrec :: Int -> Element -> ShowS
Show, Element -> Element -> Bool
(Element -> Element -> Bool)
-> (Element -> Element -> Bool) -> Eq Element
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Element -> Element -> Bool
$c/= :: Element -> Element -> Bool
== :: Element -> Element -> Bool
$c== :: Element -> Element -> Bool
Eq)

-- | Public interface for parsing JSON tokens.
data TokenResult =  TokMoreData (BS.ByteString -> TokenResult)
                  | PartialResult Element TokenResult
                  -- ^ found element, continuation, actual parsing view - so that we can report the unparsed
                  -- data when the parsing finishes.
                  | TokFailed

-- For debugging purposes
instance Show TokenResult where
  show :: TokenResult -> String
show (TokMoreData ByteString -> TokenResult
_) = String
"TokMoreData"
  show TokenResult
TokFailed = String
"TokFailed"
  show (PartialResult Element
el TokenResult
_) = String
"(PartialResult' " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Element -> String
forall a. Show a => a -> String
show Element
el String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
")"