{- |
Module    : Z.Data.MessagePack.Value
Description : MessagePack object definition and parser
Copyright : (c) Hideyuki Tanaka 2009-2015
          , (c) Dong Han 2020
License   : BSD3
-}
module Z.Data.MessagePack.Value(
  -- * MessagePack Value
    Value(..)
    -- * parse into MessagePack Value
  , parseValue
  , parseValue'
  , parseValueChunks
  , parseValueChunks'
    -- * Value Parsers
  , value
  ) where

import           Control.DeepSeq
import           Control.Monad
import           Data.Bits
import           Data.Int
import           Data.Word
import           GHC.Generics               (Generic)
import           Test.QuickCheck.Arbitrary  (Arbitrary, arbitrary)
import qualified Test.QuickCheck.Gen        as Gen
import           Prelude                    hiding (map)
import qualified Z.Data.Text                as T
import qualified Z.Data.Parser              as P
import qualified Z.Data.Vector              as V

-- | Representation of MessagePack data.
data Value
    = Bool                  !Bool                   -- ^ true or false
    | Int    {-# UNPACK #-} !Int64                  -- ^ an integer
    | Float  {-# UNPACK #-} !Float                  -- ^ a floating point number
    | Double {-# UNPACK #-} !Double                 -- ^ a floating point number
    | Str    {-# UNPACK #-} !T.Text                 -- ^ a UTF-8 string
    | Bin    {-# UNPACK #-} !V.Bytes                -- ^ a byte array
    | Array  {-# UNPACK #-} !(V.Vector Value)       -- ^ a sequence of objects
    | Map    {-# UNPACK #-} !(V.Vector (Value, Value)) -- ^ key-value pairs of objects
    | Ext    {-# UNPACK #-} !Word8                  -- ^ type tag
             {-# UNPACK #-} !V.Bytes                -- ^ data payload
    | Nil                                           -- ^ nil
  deriving (Int -> Value -> ShowS
[Value] -> ShowS
Value -> String
(Int -> Value -> ShowS)
-> (Value -> String) -> ([Value] -> ShowS) -> Show Value
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Value] -> ShowS
$cshowList :: [Value] -> ShowS
show :: Value -> String
$cshow :: Value -> String
showsPrec :: Int -> Value -> ShowS
$cshowsPrec :: Int -> Value -> ShowS
Show, Value -> Value -> Bool
(Value -> Value -> Bool) -> (Value -> Value -> Bool) -> Eq Value
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Value -> Value -> Bool
$c/= :: Value -> Value -> Bool
== :: Value -> Value -> Bool
$c== :: Value -> Value -> Bool
Eq, Eq Value
Eq Value
-> (Value -> Value -> Ordering)
-> (Value -> Value -> Bool)
-> (Value -> Value -> Bool)
-> (Value -> Value -> Bool)
-> (Value -> Value -> Bool)
-> (Value -> Value -> Value)
-> (Value -> Value -> Value)
-> Ord Value
Value -> Value -> Bool
Value -> Value -> Ordering
Value -> Value -> Value
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Value -> Value -> Value
$cmin :: Value -> Value -> Value
max :: Value -> Value -> Value
$cmax :: Value -> Value -> Value
>= :: Value -> Value -> Bool
$c>= :: Value -> Value -> Bool
> :: Value -> Value -> Bool
$c> :: Value -> Value -> Bool
<= :: Value -> Value -> Bool
$c<= :: Value -> Value -> Bool
< :: Value -> Value -> Bool
$c< :: Value -> Value -> Bool
compare :: Value -> Value -> Ordering
$ccompare :: Value -> Value -> Ordering
$cp1Ord :: Eq Value
Ord, (forall x. Value -> Rep Value x)
-> (forall x. Rep Value x -> Value) -> Generic Value
forall x. Rep Value x -> Value
forall x. Value -> Rep Value x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Value x -> Value
$cfrom :: forall x. Value -> Rep Value x
Generic)
  deriving anyclass Int -> Value -> Builder ()
(Int -> Value -> Builder ()) -> Print Value
forall a. (Int -> a -> Builder ()) -> Print a
toUTF8BuilderP :: Int -> Value -> Builder ()
$ctoUTF8BuilderP :: Int -> Value -> Builder ()
T.Print

instance NFData Value where
    rnf :: Value -> ()
rnf Value
obj = case Value
obj of
        Array Vector Value
a -> Vector Value -> ()
forall a. NFData a => a -> ()
rnf Vector Value
a
        Map   Vector (Value, Value)
m -> Vector (Value, Value) -> ()
forall a. NFData a => a -> ()
rnf Vector (Value, Value)
m
        Value
_             -> ()

instance Arbitrary Value where
    arbitrary :: Gen Value
arbitrary = (Int -> Gen Value) -> Gen Value
forall a. (Int -> Gen a) -> Gen a
Gen.sized ((Int -> Gen Value) -> Gen Value)
-> (Int -> Gen Value) -> Gen Value
forall a b. (a -> b) -> a -> b
$ \Int
n -> [Gen Value] -> Gen Value
forall a. [Gen a] -> Gen a
Gen.oneof
        [ Bool -> Value
Bool   (Bool -> Value) -> Gen Bool -> Gen Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen Bool
forall a. Arbitrary a => Gen a
arbitrary
        , Int64 -> Value
Int    (Int64 -> Value) -> Gen Int64 -> Gen Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen Int64
negatives
        , Float -> Value
Float  (Float -> Value) -> Gen Float -> Gen Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen Float
forall a. Arbitrary a => Gen a
arbitrary
        , Double -> Value
Double (Double -> Value) -> Gen Double -> Gen Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen Double
forall a. Arbitrary a => Gen a
arbitrary
        , Text -> Value
Str    (Text -> Value) -> Gen Text -> Gen Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen Text
forall a. Arbitrary a => Gen a
arbitrary
        , Bytes -> Value
Bin    (Bytes -> Value) -> Gen Bytes -> Gen Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen Bytes
forall a. Arbitrary a => Gen a
arbitrary
        , Vector Value -> Value
Array  (Vector Value -> Value) -> Gen (Vector Value) -> Gen Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Gen (Vector Value) -> Gen (Vector Value)
forall a. Int -> Gen a -> Gen a
Gen.resize (Int
n Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2) Gen (Vector Value)
forall a. Arbitrary a => Gen a
arbitrary
        , Vector (Value, Value) -> Value
Map    (Vector (Value, Value) -> Value)
-> Gen (Vector (Value, Value)) -> Gen Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Gen (Vector (Value, Value)) -> Gen (Vector (Value, Value))
forall a. Int -> Gen a -> Gen a
Gen.resize (Int
n Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
4) Gen (Vector (Value, Value))
forall a. Arbitrary a => Gen a
arbitrary
        , Word8 -> Bytes -> Value
Ext    (Word8 -> Bytes -> Value) -> Gen Word8 -> Gen (Bytes -> Value)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen Word8
forall a. Arbitrary a => Gen a
arbitrary Gen (Bytes -> Value) -> Gen Bytes -> Gen Value
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen Bytes
forall a. Arbitrary a => Gen a
arbitrary
        , Value -> Gen Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value
Nil
        ]
        where negatives :: Gen Int64
negatives = (Int64, Int64) -> Gen Int64
forall a. Random a => (a, a) -> Gen a
Gen.choose (Int64
forall a. Bounded a => a
minBound, -Int64
1)


value :: P.Parser Value
{-# INLINABLE value #-}
value :: Parser Value
value = do
    Word8
tag <- Parser Word8
P.anyWord8
    case Word8
tag of
        -- Nil
        Word8
0xC0 -> Value -> Parser Value
forall (m :: * -> *) a. Monad m => a -> m a
return Value
Nil

        -- Bool
        Word8
0xC2 -> Value -> Parser Value
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> Value
Bool Bool
False)
        Word8
0xC3 -> Value -> Parser Value
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> Value
Bool Bool
True)

        -- Integer
        Word8
c | Word8
c Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x80 Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0x00 -> Value -> Parser Value
forall (m :: * -> *) a. Monad m => a -> m a
return (Int64 -> Value
Int (Word8 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
c))
          | Word8
c Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0xE0 Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0xE0 -> Value -> Parser Value
forall (m :: * -> *) a. Monad m => a -> m a
return (Int64 -> Value
Int (Int8 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8 -> Int8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
c :: Int8)))

        Word8
0xCC -> Int64 -> Value
Int (Int64 -> Value) -> (Word8 -> Int64) -> Word8 -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8 -> Value) -> Parser Word8 -> Parser Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Word8
P.anyWord8
        Word8
0xCD -> Int64 -> Value
Int (Int64 -> Value) -> (Word16 -> Int64) -> Word16 -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word16 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word16 -> Value) -> Parser Word16 -> Parser Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Unaligned (BE Word16) => Parser Word16
forall a. Unaligned (BE a) => Parser a
P.decodePrimBE @Word16
        Word8
0xCE -> Int64 -> Value
Int (Int64 -> Value) -> (Word32 -> Int64) -> Word32 -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word32 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word32 -> Value) -> Parser Word32 -> Parser Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Unaligned (BE Word32) => Parser Word32
forall a. Unaligned (BE a) => Parser a
P.decodePrimBE @Word32
        Word8
0xCF -> Int64 -> Value
Int (Int64 -> Value) -> (Word64 -> Int64) -> Word64 -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Value) -> Parser Word64 -> Parser Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Unaligned (BE Word64) => Parser Word64
forall a. Unaligned (BE a) => Parser a
P.decodePrimBE @Word64

        Word8
0xD0 -> Int64 -> Value
Int (Int64 -> Value) -> (Int8 -> Int64) -> Int8 -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int8 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int8 -> Value) -> Parser Int8 -> Parser Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Unaligned Int8 => Parser Int8
forall a. Unaligned a => Parser a
P.decodePrim @Int8
        Word8
0xD1 -> Int64 -> Value
Int (Int64 -> Value) -> (Int16 -> Int64) -> Int16 -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int16 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int16 -> Value) -> Parser Int16 -> Parser Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Unaligned (BE Int16) => Parser Int16
forall a. Unaligned (BE a) => Parser a
P.decodePrimBE @Int16
        Word8
0xD2 -> Int64 -> Value
Int (Int64 -> Value) -> (Int32 -> Int64) -> Int32 -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int32 -> Value) -> Parser Int32 -> Parser Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Unaligned (BE Int32) => Parser Int32
forall a. Unaligned (BE a) => Parser a
P.decodePrimBE @Int32
        Word8
0xD3 -> Int64 -> Value
Int (Int64 -> Value) -> (Int64 -> Int64) -> Int64 -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int64 -> Value) -> Parser Int64 -> Parser Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Unaligned (BE Int64) => Parser Int64
forall a. Unaligned (BE a) => Parser a
P.decodePrimBE @Int64

        -- Float
        Word8
0xCA -> Float -> Value
Float (Float -> Value) -> Parser Float -> Parser Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Unaligned (BE Float) => Parser Float
forall a. Unaligned (BE a) => Parser a
P.decodePrimBE @Float
        -- Double
        Word8
0xCB -> Double -> Value
Double (Double -> Value) -> Parser Double -> Parser Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Unaligned (BE Double) => Parser Double
forall a. Unaligned (BE a) => Parser a
P.decodePrimBE @Double

        -- String
        Word8
t | Word8
t Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0xE0 Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0xA0 -> Word8 -> Parser Value
forall a. Integral a => a -> Parser Value
str (Word8
t Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x1F)
        Word8
0xD9 -> Word8 -> Parser Value
forall a. Integral a => a -> Parser Value
str (Word8 -> Parser Value) -> Parser Word8 -> Parser Value
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Parser Word8
P.anyWord8
        Word8
0xDA -> Word16 -> Parser Value
forall a. Integral a => a -> Parser Value
str (Word16 -> Parser Value) -> Parser Word16 -> Parser Value
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Unaligned (BE Word16) => Parser Word16
forall a. Unaligned (BE a) => Parser a
P.decodePrimBE @Word16
        Word8
0xDB -> Word32 -> Parser Value
forall a. Integral a => a -> Parser Value
str (Word32 -> Parser Value) -> Parser Word32 -> Parser Value
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Unaligned (BE Word32) => Parser Word32
forall a. Unaligned (BE a) => Parser a
P.decodePrimBE @Word32

        -- Binary
        Word8
0xC4 -> Word8 -> Parser Value
forall a. Integral a => a -> Parser Value
bin (Word8 -> Parser Value) -> Parser Word8 -> Parser Value
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Parser Word8
P.anyWord8
        Word8
0xC5 -> Word16 -> Parser Value
forall a. Integral a => a -> Parser Value
bin (Word16 -> Parser Value) -> Parser Word16 -> Parser Value
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Unaligned (BE Word16) => Parser Word16
forall a. Unaligned (BE a) => Parser a
P.decodePrimBE @Word16
        Word8
0xC6 -> Word32 -> Parser Value
forall a. Integral a => a -> Parser Value
bin (Word32 -> Parser Value) -> Parser Word32 -> Parser Value
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Unaligned (BE Word32) => Parser Word32
forall a. Unaligned (BE a) => Parser a
P.decodePrimBE @Word32

        -- Array
        Word8
t | Word8
t Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0xF0 Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0x90 -> Word8 -> Parser Value
forall a. Integral a => a -> Parser Value
array (Word8
t Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x0F)
        Word8
0xDC -> Word16 -> Parser Value
forall a. Integral a => a -> Parser Value
array (Word16 -> Parser Value) -> Parser Word16 -> Parser Value
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Unaligned (BE Word16) => Parser Word16
forall a. Unaligned (BE a) => Parser a
P.decodePrimBE @Word16
        Word8
0xDD -> Word32 -> Parser Value
forall a. Integral a => a -> Parser Value
array (Word32 -> Parser Value) -> Parser Word32 -> Parser Value
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Unaligned (BE Word32) => Parser Word32
forall a. Unaligned (BE a) => Parser a
P.decodePrimBE @Word32

        -- Map
        Word8
t | Word8
t Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0xF0 Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0x80 -> Word8 -> Parser Value
forall a. Integral a => a -> Parser Value
map (Word8
t Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x0F)
        Word8
0xDE -> Word16 -> Parser Value
forall a. Integral a => a -> Parser Value
map (Word16 -> Parser Value) -> Parser Word16 -> Parser Value
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Unaligned (BE Word16) => Parser Word16
forall a. Unaligned (BE a) => Parser a
P.decodePrimBE @Word16
        Word8
0xDF -> Word32 -> Parser Value
forall a. Integral a => a -> Parser Value
map (Word32 -> Parser Value) -> Parser Word32 -> Parser Value
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Unaligned (BE Word32) => Parser Word32
forall a. Unaligned (BE a) => Parser a
P.decodePrimBE @Word32

        -- Ext
        Word8
0xD4 -> Int -> Parser Value
forall a. Integral a => a -> Parser Value
ext (Int
1  :: Int)
        Word8
0xD5 -> Int -> Parser Value
forall a. Integral a => a -> Parser Value
ext (Int
2  :: Int)
        Word8
0xD6 -> Int -> Parser Value
forall a. Integral a => a -> Parser Value
ext (Int
4  :: Int)
        Word8
0xD7 -> Int -> Parser Value
forall a. Integral a => a -> Parser Value
ext (Int
8  :: Int)
        Word8
0xD8 -> Int -> Parser Value
forall a. Integral a => a -> Parser Value
ext (Int
16 :: Int)
        Word8
0xC7 -> Word8 -> Parser Value
forall a. Integral a => a -> Parser Value
ext (Word8 -> Parser Value) -> Parser Word8 -> Parser Value
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Parser Word8
P.anyWord8
        Word8
0xC8 -> Word16 -> Parser Value
forall a. Integral a => a -> Parser Value
ext (Word16 -> Parser Value) -> Parser Word16 -> Parser Value
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Unaligned (BE Word16) => Parser Word16
forall a. Unaligned (BE a) => Parser a
P.decodePrimBE @Word16
        Word8
0xC9 -> Word32 -> Parser Value
forall a. Integral a => a -> Parser Value
ext (Word32 -> Parser Value) -> Parser Word32 -> Parser Value
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Unaligned (BE Word32) => Parser Word32
forall a. Unaligned (BE a) => Parser a
P.decodePrimBE @Word32

        -- impossible
        Word8
x -> Text -> Parser Value
forall a. Text -> Parser a
P.fail' (Text
"Z.Data.MessagePack: unknown tag " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Word8 -> Text
forall a. Print a => a -> Text
T.toText Word8
x)

  where
    str :: a -> Parser Value
str !a
l = do
        Bytes
bs <- Int -> Parser Bytes
P.take (a -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
l)
        case Bytes -> Maybe Text
T.validateMaybe Bytes
bs of
            Just Text
t -> Value -> Parser Value
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> Value
Str Text
t)
            Maybe Text
_  -> Text -> Parser Value
forall a. Text -> Parser a
P.fail' Text
"Z.Data.MessagePack: illegal UTF8 Bytes"
    bin :: a -> Parser Value
bin !a
l   = Bytes -> Value
Bin (Bytes -> Value) -> Parser Bytes -> Parser Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Parser Bytes
P.take (a -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
l)
    array :: a -> Parser Value
array !a
l = Vector Value -> Value
Array (Vector Value -> Value)
-> ([Value] -> Vector Value) -> [Value] -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [Value] -> Vector Value
forall (v :: * -> *) a. Vec v a => Int -> [a] -> v a
V.packN (a -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
l) ([Value] -> Value) -> Parser [Value] -> Parser Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Parser Value -> Parser [Value]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM (a -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
l) Parser Value
value
    map :: a -> Parser Value
map !a
l   = Vector (Value, Value) -> Value
Map (Vector (Value, Value) -> Value)
-> ([(Value, Value)] -> Vector (Value, Value))
-> [(Value, Value)]
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [(Value, Value)] -> Vector (Value, Value)
forall (v :: * -> *) a. Vec v a => Int -> [a] -> v a
V.packN (a -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
l) ([(Value, Value)] -> Value)
-> Parser [(Value, Value)] -> Parser Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Parser (Value, Value) -> Parser [(Value, Value)]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM (a -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
l) ((,) (Value -> Value -> (Value, Value))
-> Parser Value -> Parser (Value -> (Value, Value))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Value
value Parser (Value -> (Value, Value))
-> Parser Value -> Parser (Value, Value)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Value
value)
    ext :: a -> Parser Value
ext !a
l   = Word8 -> Bytes -> Value
Ext (Word8 -> Bytes -> Value)
-> Parser Word8 -> Parser (Bytes -> Value)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Word8
forall a. Unaligned a => Parser a
P.decodePrim Parser (Bytes -> Value) -> Parser Bytes -> Parser Value
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Int -> Parser Bytes
P.take (a -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
l)

-- | Parse 'Value' without consuming trailing bytes.
parseValue :: V.Bytes -> (V.Bytes, Either P.ParseError Value)
{-# INLINE parseValue #-}
parseValue :: Bytes -> (Bytes, Either ParseError Value)
parseValue = Parser Value -> Bytes -> (Bytes, Either ParseError Value)
forall a. Parser a -> Bytes -> (Bytes, Either ParseError a)
P.parse Parser Value
value

-- | Parse 'Value', if there're bytes left, parsing will fail.
parseValue' :: V.Bytes -> Either P.ParseError Value
{-# INLINE parseValue' #-}
parseValue' :: Bytes -> Either ParseError Value
parseValue' = Parser Value -> Bytes -> Either ParseError Value
forall a. Parser a -> Bytes -> Either ParseError a
P.parse' (Parser Value
value Parser Value -> Parser () -> Parser Value
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser ()
P.endOfInput)

-- | Increamental parse 'Value' without consuming trailing bytes.
parseValueChunks :: Monad m => m V.Bytes -> V.Bytes -> m (V.Bytes, Either P.ParseError Value)
{-# INLINE parseValueChunks #-}
parseValueChunks :: m Bytes -> Bytes -> m (Bytes, Either ParseError Value)
parseValueChunks = Parser Value
-> m Bytes -> Bytes -> m (Bytes, Either ParseError Value)
forall (m :: * -> *) a.
Monad m =>
Parser a -> ParseChunks m Bytes ParseError a
P.parseChunks Parser Value
value

-- | Increamental parse 'Value', if there're bytes left, parsing will fail.
parseValueChunks' :: Monad m => m V.Bytes -> V.Bytes -> m (Either P.ParseError Value)
{-# INLINE parseValueChunks' #-}
parseValueChunks' :: m Bytes -> Bytes -> m (Either ParseError Value)
parseValueChunks' m Bytes
mi Bytes
inp = (Bytes, Either ParseError Value) -> Either ParseError Value
forall a b. (a, b) -> b
snd ((Bytes, Either ParseError Value) -> Either ParseError Value)
-> m (Bytes, Either ParseError Value)
-> m (Either ParseError Value)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Value -> ParseChunks m Bytes ParseError Value
forall (m :: * -> *) a.
Monad m =>
Parser a -> ParseChunks m Bytes ParseError a
P.parseChunks (Parser Value
value Parser Value -> Parser () -> Parser Value
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser ()
P.endOfInput) m Bytes
mi Bytes
inp